Search code examples
javascriptnode.jsexpresshttp-authenticationnode.js-connect

Skip basicAuth for one route


I'm using node, express and connect for a simple app and have basic HTTP auth implemented as follows (lots of code left out for brevity):

var express = require('express'),
connect = require('connect');

app.configure = function(){
  // other stuff...
  app.use(connect.basicAuth('username', 'password'));
  // other stuff...
};

I've tried googling and even attempted implementing my own auth, but I can't figure out how to skip this auth for just one route.

I'd really appreciate it if anyone offer any help with this?


Solution

  • If you don't want to use authentication for all routes, you should add the auth function as middleware to each individual route like so:

    app.get('/mysecretpage', basicAuth, function (req, res) {
      console.log('you have to be auth to see this page');
    }); 
    

    Here is a regular route without auth:

    app.get('/sample', function (req, res) {
      console.log('everybody see this page');
    }); 
    

    This link may be useful for you also: how to implement login auth in node.js