gpt4 book ai didi

authentication using middleware.ts in NEXT.js 13(使用NEXT.js 13中的midleware.ts进行身份验证)

转载 作者:bug小助手 更新时间:2023-10-25 22:48:32 27 4
gpt4 key购买 nike



I am currently coding an authentication system for my next.js app. My app has a login and user dashboard route. I have used post request method using Async and await to call API and store the JWT token in Session storage when the user logs in successfully(with the correct username and password). So when a user tries to go to the dashboard page, I want to check if the user is logged in (data exists in session storage) and check whether the token is stored in session storage or not. I think it is better to check it in middleware. So how can I access session storage in middleware to check user is logged in or not? Allow users to access the route or not. I tried to access session storage to check the user authentication status but it only sends a null value. I think it is because the middleware process is on the server side and session storage is on the client side. So how can I do this? Any idea?

我目前正在为我的next.js应用程序编写一个身份验证系统。我的应用程序有一个登录和用户仪表板路线。我使用了使用异步的POST请求方法,并等待当用户成功登录时(使用正确的用户名和密码)调用API并将JWT令牌存储在会话存储中。因此,当用户尝试转到仪表板页面时,我想检查用户是否已登录(数据存在于会话存储中),并检查令牌是否存储在会话存储中。我觉得还是放在中间件里比较好。那么,我如何访问中间件中的会话存储来检查用户是否登录?是否允许用户访问该路由。我尝试访问会话存储以检查用户身份验证状态,但它只发送空值。我认为这是因为中间件进程在服务器端,而会话存储在客户端。那么我该怎么做呢?有什么主意吗?


const userToken = typeof window !== 'undefined' ? sessionStorage.getItem('userToken') : null;
console.log("User Token:", userToken);

I tried this code to call a token from Session storage but it keeps sending me a Null value

我尝试使用此代码从会话存储中调用令牌,但它一直向我发送空值


更多回答
优秀答案推荐
更多回答

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com