Jérôme
Jérôme

Reputation: 1080

Redirect after Basic Http Auth Express.js

I want to make a private access to an application using express.js like .htaccess and .htpasswd do in my php project.

I'm using http-auth

Here is my code :

app.get('/', function(req, res) {
  basic.apply(req, res, function(username) {
    res.redirect(routes.index);
  });
});

The problem is I get an error :

500 TypeError: Object function (req, res){ res.render('index', { title: 'Express' }); } has no method 'indexOf'

Without authentication,

app.get('/', routes.index);

works without any troubles.

I think about a middleware to read my index and then use res.send()... May it works? or what did I miss?

Upvotes: 3

Views: 1249

Answers (2)

gevorg
gevorg

Reputation: 5055

I think usage pattern now is different

// Authentication module.
var auth = require('http-auth');
var basic = auth.basic({
    realm: "Simon Area.",
    file: __dirname + "/../data/users.htpasswd" // gevorg:gpass, Sarah:testpass ...
});

// Application setup.
var app = express();

// Setup route.
app.get('/', auth.connect(basic), routes.index);

Upvotes: 1

lmjohns3
lmjohns3

Reputation: 7602

I think the argument to res.redirect needs to be a URL string, right ? You seem to be passing in a request handler (function (req, res) {...}).

Upvotes: 2

Related Questions