Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- app.post('/authenticate', function(req, res, next){
- middleware.login(req, res, next);
- });
- exports.login = function(req, res, next){
- var username = req.body.username;
- var password = req.body.password;
- User.findByUsername(username,function(err, user){
- if(err){
- res.send({ success: false, message: 'Authentication failed.' });
- }
- if (!user) {
- res.send({ success: false, message: 'Authentication failed. User not found.' });
- }
- if(user && !user.isuserenabled){
- res.send({ success: false, message: 'Authentication failed. User not found.' });
- }
- if (!UserSchema.comparePassword(password,user.user_password )) {
- res.send({ success: false, message: 'Authentication failed. User not found.' });
- }
- res.cookie('yummyCookie', jwt.sign(
- //payload, secret, options, [callback]
- {
- id: user.user_id,
- email: user.email,
- name: user.firstname + " " + user.lastname,
- role: user.role
- },
- config.secret, // DO NOT KEEP YOUR SECRET IN THE CODE
- {expiresIn: "1h"}, {secure: true, httpOnly: true}));
- return next();
- });
- };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement