获取user_id而不呈现任何路由 [英] get user_id without rendering any route

查看:119
本文介绍了获取user_id而不呈现任何路由的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果没有向任何路由(端点)发出任何请求,如何登录用户数据?

How do I get logged in user data without making any request to any route (endpoint) ?

//how about I want to get logged in user's id here?

router.get('/',function(req,res,next){
  //usually we get data here
}

我知道我们可以使用 req.user.id 将用户的对象存储到 res.locals.userId = req.user.id

I know we can the user's object using req.user.id or store it to res.locals.userId = req.user.id

推荐答案

您的服务器可以托管数十万用户(这是多用户),所以当你不在特定用户的特定请求中时,没有一个当前或登录用户。

Your server can host zillions of users (it is multi-user). So, when you're not in a specific request from a specific user, there is no single "current" or "logged in" user.

只有在处理请求时,您才能代表特定用户执行某些操作,因此存在与该请求关联的特定用户。

Only when processing a request are you doing something on behalf of a specific user and thus there is a specific user associated with that request.

根据登录的工作原理,您可能会有一个具有所有当前登录用户的会话存储。当前登录的用户通常通过查看会话cookie(针对该请求)在请求中找出),然后在本地会话存储中查找该会话cookie以获取用户信息从会议商店。

Depending upon how your login works, you may have a session store of some kind that has all the currently logged in users in it. The currently logged in user is typically figured out in a request by looking at a session cookie (for that request) and then looking up that session cookie in a local session store to get the user info from the session store.

如果您希望用户信息的点在建立一个socket.io连接,那么您可以通过 client.request.headers.cookie 建立socket.io连接时访问Cookie。使用cookies,您应该可以获得登录会话,然后访问您想要的任何其他会话信息(与正常http请求相同)。

If the point that you want the user info is in the establishment of a socket.io connection, then you can get access to the cookies at the time the socket.io connection is established via client.request.headers.cookie. Using the cookies, you should be able to get your login session and then get access to any other session info you want (just the same as a normal http request).

io.on('connection', function(client) {
    var cookie = client.request.headers.cookie;
    // using the cookie, you should then be able to get to your login session
    // using the session, you should be able to get to any other login data
});

需要记住的一个概念是创建一个socket.io连接(基于webSocket)始终以正常的HTTP请求(然后协议升级到webSocket连接)启动。因此,所有正常的HTTP事件在当时可用(如Cookie,标题等)。

A conceptual thing to remember is that the creation of a socket.io connection (which is based on a webSocket) always starts with a normal HTTP request (which is then protocol upgraded to a webSocket connection). As such, all the normal HTTP things are available at that time (such as cookies, headers, etc...).

如果您还没有实现任何类型用户认证(设置上述cookie),那么您需要在创建socket.io连接之前使用户进行身份验证(因此,当您访问上面的cookie时,cookie才会出现),否则您将需要将用户身份验证作为socket.io连接过程是通过socket.io中间件支持的。

If you have not yet implemented any sort of user authentication (that sets the above cookie), then you will need to either have the user authenticate before creating the socket.io connection (so the cookie is there when you access it above) or you will need to implement user authentication as part of the socket.io connection process which is something that is supported via socket.io middleware.

如果您无法从护照访问cookie,请看: a href =https://stackoverflow.com/questions/12258795/how-to-access-cookie-set-with-passport-js>如何使用Passport.js访问Cookie集。

If you're having trouble accessing the cookie from passport, then see this: How to access Cookie set with Passport.js.

这篇关于获取user_id而不呈现任何路由的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆