-1

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?

aynber
  • 22,380
  • 8
  • 50
  • 63
mrappleton
  • 372
  • 3
  • 10

1 Answers1

4

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

Community
  • 1
  • 1
alessioalex
  • 62,577
  • 16
  • 155
  • 122
  • Thanks for the response - I'm playing with this but how would I implement the auth in my middleware function? The page you linked to shows a check for a user session but I just want basic HTTP auth to happen in there. Here's what I have: `basicAuth = function(req, res, next) { if (settings.auth.enabled) { connect.basicAuth(settings.auth.user, settings.auth.pass); next(); } else { next(); } };` – mrappleton Nov 08 '11 at 14:27
  • Like this: app.get('/secret', connect.basicAuth('username', 'password'), function (req, res) { ... }); – alessioalex Nov 08 '11 at 15:07