Loopback 2.8:在每次请求api之前检查用户状态

时间:2014-12-24 17:20:18

标签: loopbackjs strongloop

我需要在每次请求api之前检查用户状态,并返回403,如果它是非活动用户。

我尝试在通配符beforeRemote中执行检查,但是我必须为我想要限制访问的每个模型注册它。

也可以注册快速中间件,但req.accessToken在那一刻不存在。

是否有更好的方法来限制用户访问?

3 个答案:

答案 0 :(得分:0)

作为一个选项,您可以尝试引入新角色ACTIVE_USER并使用ACL来控制访问。一些文档链接:rolesaccess control concepts

答案 1 :(得分:0)

如何使用简单的快速中间件呢?

app.use(function checkIfUserIsActive(req, res, next) {
if (!req.accessToken) {
    // User not logged in
    return next();
}
app.models.UserModel.findById(req.accessToken.userId, function (err, user) {
    if (err) {
        // Error with finding the user
        return next(err);
    }
    if (!user) {
        return next(new Error('No user with this access token was found.'));
    }
    if (!user.active) {
        return next(new Error('User is inactive.'));
    } else {
        return next();
    }
});
});

答案 2 :(得分:-1)

相关问题