Loopback - Implementing custom authentication

人盡茶涼 提交于 2019-12-05 23:37:43

Please check some of the following examples to see if it fits your use case:

My example is using a bootscript in express but you could easily change it into a remote method.

module.exports = function(app) {
    //get User model from the express app
    var UserModel = app.models.User;

    app.post('/login', function(req, res) {

        console.log(req.body);
        //parse user credentials from request body
        const userCredentials = {
            "username": req.body.username,
            "password": req.body.password
        }

        UserModel.findOne({
            "where": {
                "username": userCredentials.username
            }
        }, function(err, user) {

            // Custom Login - Put the stored procedure call here 

            if (err) {
                //custom logger
                console.error(err);
                res.status(401).json({
                    "error": "login failed"
                });
                return;
            }

            // Create the accesstoken and return the Token
            user.createAccessToken(5000, function(err, token) {
                console.log(token)
                res.json({
                    "token": result.id,
                    "ttl": result.ttl
                });
            })
        })
    });
}

Now you can use that Token for Loopbacks authorization mechanism.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!