简体   繁体   English

如何在客户端读取Passport.js用户信息?

[英]How do I read Passport.js user info on client side?

My app is using Angular2 for the front-end, served by a separate (cross domain) backend server running express and using passport.js for Google Oauth authentication. 我的应用程序使用Angular2作为前端,由运行express的单独的(跨域)后端服务器提供服务,并使用password.js进行Google Oauth身份验证。

When a user is authenticated by the server using Passport (through google oauth), their user data is loaded from the database and included in the credentials, which is used to determine which backend API routes they are authorized to use. 服务器使用Passport(通过google oauth)对用户进行身份验证时,将从数据库中加载其用户数据并将其包含在凭据中,凭据用于确定他们有权使用哪些后端API路由。 (It's based off this tutorial on scotch.io that I'm sure everyone has seen: https://scotch.io/tutorials/easy-node-authentication-setup-and-local ) (它基于scotch.io上的本教程,我敢肯定每个人都看过: https ://scotch.io/tutorials/easy-node-authentication-setup-and-local)

I want to access this user object in my front-end as well to enable route-guards that depend on a user's access level (defined in their user object on the server). 我也想在前端访问该用户对象,以启用取决于用户访问级别(在服务器上其用户对象中定义)的路由保护。

From this question it seems the data is sent via a JWT and is readable on the front-end, just not changeable, which is fine: https://www.reddit.com/r/Angular2/comments/4ud0ac/ng2_secure_connection_front_to_back/ 从这个问题来看,数据似乎是通过JWT发送的,并且在前端是可读的,只是不可更改,这很好: https//www.reddit.com/r/Angular2/comments/4ud0ac/ng2_secure_connection_front_to_back/

How do I access and read that token on the client? 如何在客户端上访问和读取该令牌? All I can find is the 'connect.sid' session cookie set by express. 我只能找到express设置的“ connect.sid”会话cookie。 The payload of the cookie doesn't fit a standard JWT as it only has 2 sections, not 3. Cookie的有效负载不适合标准JWT,因为它只有2个部分,而不是3个部分。

You are probably not using JWT but cookie-based sessions if you followed the tutorial. 如果您遵循本教程,则可能不是使用JWT,而是使用基于cookie的会话。 The cookie only contains a session ID which your server uses to identify the session from the session store, and using this information you probably dig up something from the database in deserializeUser . Cookie仅包含一个会话ID,您的服务器将使用该ID来标识会话存储中的会话,并且您可能会使用此信息从deserializeUser的数据库中挖掘出一些东西。 That is then available to you in req.user in the backend. 然后,您可以在后端的req.user中使用该功能。

You could of course add the user data to the response of every request but if you really are using cookie-based sessions sending the user object with every response likely makes little sense. 您当然可以将用户数据添加到每个请求的响应中,但是如果您确实在使用基于cookie的会话,则将用户对象与每个响应一起发送可能毫无意义。 You could eg. 你可以例如。 just add a route that will return the relevant parts of req.user : 只需添加一条路由即可返回req.user的相关部分:

app.get('/users', function(req, res) {
  res.json({ username : req.user.username });
);

暂无
暂无

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

相关问题 passport.js req.user 正在返回:&#39;Promise { false }&#39; - 如何获取当前会话的用户名? - passport.js req.user is returning: 'Promise { false }' - How do I get the current sessions' username? 如何查看刚刚使用passport.js登录的用户ID? - How do I check the ID of the user who just logged in with passport.js? 如何使用已经登录用户的accessToken使用passport.js来获取JSON? - How do I use passport.js to GET JSON using accessToken of an already logged in user? 如何知道用户是否使用passport.js 登录? - How to know if user is logged in with passport.js? 如何使用passport.js登录用户 - How to login user with passport.js 如何在客户端javascript中访问Passport的req.user变量? - How do I access Passport's req.user variable in client side javascript? Passport.js:认证后如何访问用户object? - Passport.js: how to access user object after authentication? "<i>How do I render a pug template on success and failure of authentication(passport.js) with variables from the pug template?<\/i>如何使用哈巴狗模板中的变量在身份验证(passport.js)成功和失败时呈现哈巴狗模板?<\/b> <i>(express, passport.js, pug)<\/i> (快递,passport.js,哈巴狗)<\/b>" - How do I render a pug template on success and failure of authentication(passport.js) with variables from the pug template? (express, passport.js, pug) 如何使用快速路由器正确调用passport.js功能? - How do I correctly call passport.js functions with express router? 如何在使用 passport.js 本地身份验证登录后的请求中呈现带有变量的不同页面?(Express) - How do I render different pages with variables on post request of login with passport.js local authentication?(Express)
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM