[英]How do i use cookies on the client side to display user information after passport js local user auth on the server side?
I am using passport js and express session for local user auth, and i want to use cookies to display user information such as first name or email on the client side thats running on vue js, after the user has successfully signed up using passport js on the server side. 我正在使用护照js并为本地用户auth进行快速会话,我想在用户成功使用护照js进行注册后,使用cookie在vue js上运行的客户端上显示用户信息,例如名字或电子邮件。服务器端。 How do i accomplish this? 我该如何完成?
There are several ways to do this: 做这件事有很多种方法:
Use an API endpoint, like /me
, that returns the logged-in user's user information. 使用API端点,例如/me
,它返回已登录用户的用户信息。 Whenever a user's information needs to be displayed, make a call to /me
and parse what's returned. 每当需要显示用户信息时,请调用/me
并解析返回的内容。
Use a JWT as your cookie and include user information in it. 将JWT用作cookie,并在其中包含用户信息。 However, once user data changes, you'll need to update the user's cookie. 但是,一旦用户数据更改,您将需要更新用户的cookie。 This might be okay for relatively static data like names that do not change often. 对于名称相对不经常更改的静态数据,这可能是可以的。 However, for data that changes frequently this might not be the most convenient option. 但是,对于经常更改的数据,这可能不是最方便的选择。 Also keep in mind that whatever is in a cookie is sent by the user on each request and can take up extra bandwidth or even exceed the maximum header length if too much is loaded into the cookie. 还请记住,cookie中的任何内容都是由用户根据每个请求发送的,并且如果cookie中加载的内容过多,则会占用额外的带宽甚至超过最大标头长度。
You can save cookies to the user's pc from client side using 您可以使用以下命令从客户端将Cookie保存到用户的PC中
document.cookie="email:loremipsum@dolor.com"
and you can read it from client side as 您可以从客户端阅读为
let x = document.cookie
if you want to read it from the express server, you can use cookie-parser so npm install cookie-parser and then require it into your express server and do app.use 如果您想从Express服务器读取它,则可以使用cookie-parser,因此npm安装cookie-parser,然后将其要求到您的express服务器中并执行app.use。
var express = require('express')
var cookieParser = require('cookie-parser')
var app = express()
app.use(cookieParser())
to read the cookies at express server, 在快递服务器上读取Cookie,
app.get('/',(req,res)=>{
console.log(req.cookies.email)
});
this would display the cookie at server side. 这将在服务器端显示cookie。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.