Skip to content

Instantly share code, notes, and snippets.

@balupton
Last active December 11, 2015 06:08
Show Gist options
  • Save balupton/4557006 to your computer and use it in GitHub Desktop.
Save balupton/4557006 to your computer and use it in GitHub Desktop.
DocPad: Protecting documents with basic http authentication
# Import
express = require('express')
# Create our auth middleware
authMiddleware = express.basicAuth (user, pass) ->
return user is 'super' and pass is 'secret'
# Configure DocPad
docpadConfig =
serverExtend: (opts) ->
# Prepare
docpad = @
# Define our own custom middleware to handle protected pages
opts.serverExpress.use (req,res,next) =>
# Prepare
cleanUrl = req.url.replace(/\?.*/,'')
file = docpad.getFileByUrl(req.url) or docpad.getFileByUrl(cleanUrl)
# Continue with the next middleware if the file doesn't exist or isn't protected
return next() unless file?.get('protected')
# The file is protected so lets forward the request onto our auth middleware
return authMiddleware(req,res,next)
# Export
module.exports = docpadConfig
title: "My protected document"
protected: true

Only make me available if the user has authetnicated themselve

@sergeylukin
Copy link

You most likely meant

docpad = @docpad

instead of

docpad = @

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment