I am working on a NodeJS Application Login. Four routes that relates to this issue are
GET /admin/login -- Used to get the Login Page (1)
POST /admin/login -- Used to go to Controller (2)
POST /api/auth/login requireLogin -- Actual login happens here (3)
GET /admin/dashboard requireAuth-- Route to go after successful login (4)
Route Definition:
(1) adminRoutes.get('/admin/login', HomeController.getLoginPage);
(2) adminRoutes.post('/admin/login', AdminController.postLoginPage);
(3) authRoutes.post('/api/auth/login', requireLogin, AuthenticationController.login);
(4) adminRoutes.get('/admin/dashboard', requireAuth, HomeController.index);
Values for requireLogin
and requireAuth
var requireAuth = passport.authenticate('jwt', {session: false, failureRedirect: '/admin/login'}),
requireLogin = passport.authenticate('local', {session: false});
From (1) I fill up login details and hit submit, form action takes it to (2). For (2), I have a method written like this;
Method for (2)
exports.postLoginPage = function(req, res, next){
request.post({url: 'http://localhost:3006/api/auth/login', form: req.body}, function(err, response, body){
console.log('outside if');
if(response.statusCode == '200'){
console.log('inside if 200');
var wegot = JSON.parse(response.body);
console.log(wegot.token);
res.header('Authorization', wegot.token);
res.redirect('/admin/dashboard');
}else{
console.log('if else err');
req.flash('error', body);
// return res.redirect('/admin/login');
return 'Unauthorized';
}
})
}
Method for (3)
function generateToken(user){
return jwt.sign(user, authConfig.secret, {
expiresIn: 10080
});
}
function setUserInfo(request){
return {
_id: request._id,
email: request.email,
role: request.role
};
}
exports.login = function(req, res, next){
var userInfo = setUserInfo(req.body);
res.status(200).json({
token: 'JWT ' + generateToken(userInfo),
user: userInfo
});
}
After successful login from API (3), I need to be redirected to (4). But I am going back to (1). I know login is successful and I am also getting JWT Token on log.
Log:
POST /api/auth/login 200 32.445 ms - 330
outside if
inside if 200
JWT eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJfaWQiOiI1YTI5MDQ3MGU4Nzc3YTM1OTQ2Mjk5YWEiLCJlbWFpbCI6InNwb25zb3JAZ21haWwuY29tIiwicm9sZSI6Im5pZ2h0IiwiaWF0IjoxNTEyNjQzODM0LCJleHAiOjE1MTI2NTM5MTR9.J5LqkfeSrRU4ukY9rIsFNUBArTLloynRsle4mkwTgpA
POST /admin/login 302 57.531 ms - 76
GET /admin/dashboard 302 2.690 ms - 68
GET /admin/login 200 1.581 ms - -
What might be the issue here? How can I solve this. Let me know if you need more info on this.
POST /admin/login 302 57.531 ms - 76 GET /admin/dashboard 302 2.690 ms - 68 GET /admin/login 200 1.581 ms - -
Your response status code is 302, which means you have been redirected. Obviously /admin/dashboard
doesn't accept your authorization token.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.