1. Engineering
  2. Computer Science
  3. when a user logs in they are redirected to homepage...

Question: when a user logs in they are redirected to homepage...

Question details

When a user logs in they are redirected to homepage.

How can I get my web app to also say 'welcome username' with the username they provided after login?  

I am using JWT tokens, mongoose and express.

Here is my code:

users.js

/*API to login user*/
router.post('/login', function(req, res, next){
var username = req.body.user_name;
var password = req.body.password;
User.findOne({'user_name': username}, function (err, user) {

//if there are any errors, return the error
if (err)
res.send(err);

//if user account found then check the password
if (user) {

//compare passwords
if (user.validPassword(password)) {

//Success:Assign new access token for the session
user.access_token = createJwt({user_name: username});
user.save();
res.cookie('Authorization', 'Bearer ' + user.access_token);
res.json({'success' : 'loggedIn'});
}
else {
res.status(401).send({
"status": "error",
"body": "Incorrect username/password combination"
});
}
}
else
{
res.status(401).send({
"status": "error",
"body": "Username not found"
});
} }); });

index.js

router.get('/homepage', function(req, res, next) {
try {
var jwtString = req.cookies.Authorization.split(" ");
var profile = verifyJwt(jwtString[1]);
if (profile) {
res.render('homepage');
}
}catch (err) {
res.json({
"status": "error",
"body": [
"You are not logged in."
]
});
}
});

Solution by an expert tutor
Blurred Solution
This question has been solved
Subscribe to see this solution