Reputation: 18939
I'm implementing a little aunthentication routes on my Node app, as simple as this:
app.get('/admin', function(req, res) {
res.render('login', { error: false });
});
app.post('/admin', function(req, res) {
if((adminAccount.username === req.body.username) &&
(adminAccount.password === req.body.password)) {
res.redirect('/admin/books');
}
else {
res.redirect('/admin', { error: true });
}
});
If I login with the right credentials it works flawless, how ever if I failed, it should make a GET request to /admin
by redirect.
However I get: undefined. Redirecting to //127.0.0.1:3000/admin
That makes me thing that res.redirect
is making a POST
request to /admin
intead of a GET
one.
What can I do? How do I specify res.redirect
to redirect the right way?
Upvotes: 2
Views: 2218
Reputation: 1174
My experience. If you always get
undefined. Redirecting to
replace
res.redirect('/admin', { error: true });
to
res.redirect('/admin');
If no error, keep trying what you want to do.
Upvotes: 0
Reputation: 75854
It looks ok to me, Can you try doing just this to rule out the error: false being the problem:
res.redirect('/admin');
If you want to flash a message, I would use connect-flash:
app.js:
var flash = require('connect-flash');
app.use(flash());
route:
req.flash('error', 'Woops, looks like that username and password are incorrect.');
res.redirect('/login');
middleware:
res.locals.message = req.flash();
view:
<% if ( message.error ) { %>
<div class="message error"><%= message.error %></div>
<% } %>
Upvotes: 2