Skip to content

Instantly share code, notes, and snippets.

@balupton balupton/
Last active Dec 11, 2015

What would you like to do?
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


This comment has been minimized.

Copy link

commented Jan 31, 2013

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
You can’t perform that action at this time.