Omitir basicAuth para una ruta

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?

preguntado el 08 de noviembre de 11 a las 12:11

1 Respuestas

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: cómo implementar la autenticación de inicio de sesión en node.js

contestado el 23 de mayo de 17 a las 14:05

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

Like this: app.get('/secret', connect.basicAuth('username', 'password'), function (req, res) { ... }); - alessioalex

No es la respuesta que estás buscando? Examinar otras preguntas etiquetadas or haz tu propia pregunta.