Posted onEdited onInFrontendViews: Symbols count in article: 422Reading time ≈2 mins.
problem description
nextjs get token from another site(usually oauth server) and send the token to the next js server(the server which host the nextjs generate the web page and send to your browser) to let it use in getserverprops, then we can protect our route or fetch some auth-need data before render
solution
You can use an HTTP-only cookie to store the token and pass it between the client and server. This approach works with serverless deployments and multiple instances.
Here’s a solution using TypeScript and the next-iron-session package to store the token in an encrypted cookie:
Install the required dependencies:
1
npm install next-iron-session cookie
Configure next-iron-session: Create a file iron-session-config.ts in the root folder of your project (my_project/src/iron-session-config.ts): notice: the sessionOption password field need to at least 32 words