Really sorry if this is the wrong place.So at the moment I have a login setup that will return the success/failure of the login, a JWT and some user data. I'd like it to store the JWT and redirect to the page '/' when successful. When unsuccessful full it should redirect to '/users/login' and show a flash message explaining the error. Here is my current post request:// Authenticate router.post('/login', (req, res, next) => { const username = req.body.username; const password = req.body.password; User.getUserByEmail(username, function(err, user){ if(err) throw err; if(!user){ console.log('not user'); // req.flash('error_msg', 'User not found'); // // res.redirect('/users/login'); return res.json({success: false, msg: 'User not found'}); } User.comparePassword(password, user.password, (err, isMatch) => { if(err) throw err; if(isMatch){ const token = jwt.sign(user, config.secret, { expiresIn: 604800 // 1 week }); res.json({ success: true, token: 'JWT '+token, user: { id: user._id, name: user.name, username: user.username, email: user.email } }); } else { return res.json({success: false, msg: 'Wrong password'}); // req.flash('error_msg', 'Wrong Password'); // // res.redirect('/users/login'); } }); }); }); The commented code will give the right result, kinda. They achieve the flash message and redirect but will do not return a value so the code will continue to run leading to errors(like trying to check the password associated with a user that doesn't exist.I'm not sure of the best way to store a JWT, I assume cookies or something.So what is the best way to get the desired effect?
Submitted June 01, 2017 at 11:22AM by thelynched
No comments:
Post a Comment