首页 > 解决方案 > 如何在节点登录 API 中添加角色或映射电子邮件、密码和角色?

问题描述

当我提供正确的凭据时,我的登录 API 正在生成令牌,现在我想添加角色并通过将电子邮件、密码与角色(即)用户或管理员映射来检查它是否生成令牌。我将如何实现这一点。下面是我的代码.我从员工表中获取电子邮件和密码,并希望从角色表中映射角色。期待帮助

 getUserByUserEmail  : (email , callBack) =>{
        pool.query(
            `select * from employee inner join roles on employee.internal_id = roles.internal_id where email = ?`,
            [email],
            (error, results, fields) => {
                if(error){
                    return callBack(error);
                }
                return callBack(null, results);
            }
        )
    }

这是我调用 getUserByUserEmail 服务的登录功能。

 login: (req, res ) =>{
    const body = req.body;
    getUserByUserEmail(body.email, (err, results) => {
        if(err) {
            console.log(err);
        }
        if(!results) {
            return res.json({
                success : 0 ,
                message : "Invalid Email or Password"
            });
        }
        const result = compareSync( body.password, results[0].password);
        if (result) {
            results[0].password = "";
            const jsontoken = sign({ result: results }, "qwe123", {expiresIn : "3h"});
            return res.json({
                success : 1 ,
                message : "You are logged in Successfully!",
                token : jsontoken
            });
            console.log(results)
        }
        else{
            return res.json({
                success : 0 ,
                message : "Invalid Email or Password"
            });
        }
    });
}

标签: javascriptnode.jsangular

解决方案


推荐阅读