简体   繁体   English

Next.js SSR 存储访问令牌而不使用 redux

[英]Next.js SSR store access token without using redux

I've implemented a server-side rendering application using NextJS and ReactJS.我已经使用 NextJS 和 ReactJS 实现了一个服务器端渲染应用程序。

When the user login into the application, I want to store some of the user details like access token, refresh token, email address and use it throughout the application.当用户登录应用程序时,我想存储一些用户详细信息,如访问令牌、刷新令牌、电子邮件地址,并在整个应用程序中使用它。 (Kind of storing it globally like how we use local storage on Client side rendering applications) (有点像我们在客户端渲染应用程序上使用本地存储那样全局存储它)

I'm not using REDUX, so I would like to know the best way of storing those user details without making use of REDUX.我没有使用 REDUX,所以我想知道在不使用 REDUX 的情况下存储这些用户详细信息的最佳方式。

What are the different ways to achieve this while working with SSR?使用 SSR 时有哪些不同的方法可以实现这一目标?

I'd recommend to use a session cookie. 我建议使用会话cookie。 This is available in next.js getInitialProps method. 在next.js getInitialProps方法中可用。

static async getInitialProps ({req}) {
  console.log(req.headers.cookies) // depending on your server middlewares / config, this is available
}

LocalStorage or SessionStorage is not available in SSR, whereas the cookies are always attached to the request. SSR中不提供LocalStorage或SessionStorage,而cookie始终附加在请求中。 I'd further recommend to make the cookie secure (only served via https) and protect from JS client / browser access with http-only . 我进一步建议确保cookie的secure (仅通过https提供),并使用http-only保护其不受JS客户端/浏览器访问。

When using express-session using cookies/sessions gets more convenient. 使用快速会话时,使用cookie /会话会更加方便。

static async getInitialProps ({req}) {
  console.log(req.session) // with express session
}
> import cookie from 'cookie'
> 
> export function parseCookies(req) {   return cookie.parse(req ?
> req.headers.cookie || "" : document.cookie); }

I recently worked on something similar.我最近在做类似的事情。 Typically, you can store it in storage or cookies.通常,您可以将其存储在存储或 cookie 中。 But with nextJS , you would want to make use of it in both client and server side methods such as getServerSideProps .但是对于nextJS ,您可能希望在客户端和服务器端方法(例如getServerSideProps使用它。 That way you can early detect if user is logged in, make API calls before page is rendered (making use of nextJS SSR).这样你就可以及早检测到用户是否登录,在页面呈现之前进行 API 调用(利用 nextJS SSR)。

Refer this discussion which clearly mentions that cookie is the way.请参阅此讨论,其中清楚地提到 cookie 是方式。 This is what the collaborator at NextJS says:这是 NextJS 的合作者所说的:

If you want to have data available on both the client and server you could use cookies.如果您希望在客户端和服务器上都有可用的数据,您可以使用 cookie。

This makes sense.这是有道理的。 Remember , your methods like getServerSideProps do not run on the client and do not have access to these objects : document , window etc..请记住,像getServerSideProps这样的方法不会在客户端上运行,也无法访问这些对象: documentwindow等。

When login is done in client you can set cookie like : document.cookie = "cookiename=hello" .在客户端完成登录后,您可以设置 cookie 如下: document.cookie = "cookiename=hello" Here is an SO answer这是一个SO答案

In getServerSideProps(context), make use of the param context, it gives you access to the request like context.req .在 getServerSideProps(context) 中,使用 param 上下文,它使您可以访问像context.req一样的请求。 Then you can get cookie from the request object, using a cookie parser module or context.req.headers.cookie .然后,您可以使用 cookie解析器模块context.req.headers.cookie从请求对象中获取 cookie。

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

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