Friday, 12 May 2017

How can we set the user information for Jwt by using `setInfo()`?

How can we set the user's information for Jwt that is used for Jwt? Below is the code snippet that I have not understood, Please anyone help me out.

Code:

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.user);

    res.status(200).json({
        token: 'JWT ' + generateToken(userInfo),
        user: userInfo
    });

1.) The first function is generateToken this is clear to me.

2.) The second function is setUserInfo() which I have not understood.Please explain this.

3.)The third is export login, this function will give the JWT back to the user in ionic 2, which also I have not understood.Please explain.I ave used a passport-login which has a login function in a separate class.



via Aditya Jain

No comments:

Post a Comment