Node.js 在环回中访问自定义路由中的当前登录用户id

Node.js 在环回中访问自定义路由中的当前登录用户id,node.js,loopbackjs,loopback,Node.js,Loopbackjs,Loopback,我正在尝试使用context.options通过自定义路由访问当前登录的用户,但发现它为空。在“保存前”尝试在操作挂钩中访问,如下所示: 'use strict'; module.exports = function (logs) { logs.observe('before save', async (ctx, next) => { console.log(ctx.options) //this is empty i.e {} }); 这是我的自定义路由(启动脚本-rou

我正在尝试使用context.options通过自定义路由访问当前登录的用户,但发现它为空。在“保存前”尝试在操作挂钩中访问,如下所示:

'use strict';
module.exports = function (logs) {
  logs.observe('before save', async (ctx, next) => {
    console.log(ctx.options) //this is empty i.e {} 
});
这是我的自定义路由(启动脚本-routes.js):


我需要访问令牌并最终登录用户。我相信问题是因为rest ctx的自定义路由。选项已填充。在这种情况下,它是空的

操作hook
context
接受您提交给它的值,以及一些与模型相关的默认值,默认情况下它永远不会有用户ID

Loopback不在自定义路由上使用其中间件,因此您需要手动调用它(或在每个路由上使用它)。下面是通过按请求调用中间件来实现的方法

app.start = function() {

    var AccessToken = app.registry.getModelByType('AccessToken');

    app.get('/logs/dl', (req, res) => {

        // loopback.token will get your accessToken from the request
        // It takes an options object, and a callback function
        loopback.token({model: AccessToken, app: app}) (req, res, () => {
            // Once we're here the request's accessToken has been read
            const userId = req.accessToken && req.accessToken.userId;

            //  If you want to give context to a operation hook you use the second parameter
            logs.create({color: 'red'}, {contextValue: 'contextValue', userId: userId}, (err, obj) => {

                if (!err) {
                    res.status(200);
                    res.send(obj);
                }
                else {
                    res.status(500);
                    res.send('Some problem occurred. Please try again later')
                }
            });
        })
    });



    // the rest of app.start...
}

`

我的请求没有访问令牌…它是一个公共url。我需要有当前登录的用户!如何识别没有访问令牌的当前登录用户?必须在服务器端维护一些正在进行的会话?有,它在
access\u令牌
表中。每个记录对应于用户的登录会话,当您提交请求时,它会在该表中查找令牌,以判断会话是否结束。否则它将如何跟踪请求之间的会话?
app.start = function() {

    var AccessToken = app.registry.getModelByType('AccessToken');

    app.get('/logs/dl', (req, res) => {

        // loopback.token will get your accessToken from the request
        // It takes an options object, and a callback function
        loopback.token({model: AccessToken, app: app}) (req, res, () => {
            // Once we're here the request's accessToken has been read
            const userId = req.accessToken && req.accessToken.userId;

            //  If you want to give context to a operation hook you use the second parameter
            logs.create({color: 'red'}, {contextValue: 'contextValue', userId: userId}, (err, obj) => {

                if (!err) {
                    res.status(200);
                    res.send(obj);
                }
                else {
                    res.status(500);
                    res.send('Some problem occurred. Please try again later')
                }
            });
        })
    });



    // the rest of app.start...
}