简体   繁体   English

如何在nodejs中为应用程序全局保留每个请求的用户信息

[英]How to keep userinfo for each request globally for the application in nodejs

I am working on nodejs (expressjs) application. 我正在开发nodejs(expressjs)应用程序。 After authentication a random generated token is return to app. 认证后,将随机生成的令牌返回给应用程序。 That token is used for authenticating further requests. 该令牌用于验证其他请求。 After authenticating how do we keep user info till completing request. 验证身份后,我们将保留用户信息,直到完成请求为止。 Can we use session? 我们可以使用会议吗?

Option 1 (as mentioned in comments) 选项1 (如评论中所述)

You can store your user data manually in the request object req.user = userData 您可以将用户数据手动存储在request对象中req.user = userData

Option 2 选项2

Using an external library like passport.js allows you to handle the whole authentication part and automatically saves the authenticated user into the request object ( req.user ) for every single request (as long as the user authenticated) 使用像passport.js这样的外部库,您可以处理整个身份验证部分,并针对每个单个请求(只要该用户经过身份验证)将经过身份验证的用户自动保存到request对象( req.user )中

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM