phortonssf
8/3/2018 - 5:03 AM

loopback add token on user register

loopback add token on user register

module.exports = function(appUser) {
    
    /* hook that creates a token and logs in a new user . appUser matches parameter from line 1 */
    appUser.observe('after save', function(ctx, next) {
        if (ctx.isNewInstance === true) {
            var instance = ctx.instance;
            
            instance.createAccessToken(1209600000, function(err, response) {
                if (err === null) {
                    ctx.instance["token"] = response.id;
                    ctx.intance["userId"] = response.userId
                    next();
                }
            });
            
        } else {
            next();
        }
    });
}