简体   繁体   English

在服务器端使用护照js本地用户身份验证后,如何在客户端使用Cookie来显示用户信息?

[英]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: 做这件事有很多种方法:

  1. 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并解析返回的内容。

  2. 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.

相关问题 Firebase:如果我使用服务器端 session cookies,如何在客户端获取登录用户的 ID 令牌? - Firebase: How do I get the ID Token for a signed-in user on the client side if I am using server-side session cookies? 如何通过Passport和node.js在客户端区分已通过身份验证的用户? - How to distinguish an authenticated user at client side with Passport and node.js? 如何在客户端javascript中访问Passport的req.user变量? - How do I access Passport's req.user variable in client side javascript? 如何将Google API用作特定的用户服务器端? Node.js - How do I use Google APIs as a specific user server-side? Node.js 服务器端文件更改后,未定义Passport.js req.user对象(节点,快速) - Passport.js req.user object is undefined after server side file changes (node, express) 客户端和服务器端 cookie - client side and server side cookies 如何在客户端管理当前用户会话? - How do I manage the current user session on the client side? 如何将Segment.io服务器和客户端事件连接到同一个匿名用户? - How do I connect Segment.io server and client side events to the same anonymous user? 如果用户在客户端弹出窗口中单击“是”,如何在服务器端查找 - How to find in server side if user clicked yes in client side popup 如何从服务器(Node JS和Express)的客户端上接收和使用JSON对象? - How do I receive and use a JSON object on the client-side from the server (Node JS and Express)?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM