简体   繁体   English

如果已经使用 Reactjs 登录,如何限制用户显示登录页面

[英]How to restrict user to display login page if already logged in using Reactjs

I am working on Reactjs and using nextjs framework, I am working on "Login Logout" module and i just want that if user already logged in ( email set in cookie) then he should redirect to "dashboard" page,How can i do this?我正在研究 Reactjs 并使用 nextjs 框架,我正在研究“登录注销”模块,我只希望如果用户已经登录(cookie 中设置了 email),那么他应该重定向到“仪表板”页面,我该怎么做? I tried with following code but not working for me, giving me error ",Here is my current code我尝试使用以下代码但不适合我,给我错误“,这是我当前的代码

cookies returned from getServerSideProps Reason: undefined cannot be serialized as JSON

export async function getServerSideProps(context: { req: { headers: { cookies: any; }; }; }) {
    
  const cookies = context.req.headers.cookies;
  if (cookies && cookies.email) {
    // email is present in cookies, so redirect to /dashboard
    return {
      redirect: {
        destination: "/dashboard",
        statusCode: 302, // temporary redirect
      },
      props: {},
    };
  }
  
  return {
    props: {
      cookies,
    },
  };
}

When returning props.cookies from getServerSideProps , you are potentially returning the cookies as undefined .getServerSideProps返回props.cookies时,您可能会将 cookies 返回为undefined Use '' as the default value if missing.如果缺少,请使用''作为默认值。

You can do this:你可以这样做:

  return {
    props: {
      cookies: cookies || '',
    },
  };

You need to parse the cookies and you can get the cookies on the request you don't need the header您需要解析 cookies 并且可以根据不需要 header 的请求获得 cookies

const cookies = JSON.parse(context.req.cookies);
if(cookies.email !== undefined)... 

暂无
暂无

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

相关问题 Angular 2/4:如果用户已经登录,如何限制对登录路由的访问? - Angular 2/4: How to restrict access to Login Route if user is already logged in? 限制用户登录后重定向到登录页面,即使他按角度按浏览器的后退按钮? - restrict the redirection to login page once the user has already logged in even if he press the back button of browser in angular? 如何限制登录用户在angular js的登录页面或注册页面中移动? - how to restrict logged user to move on login page or signup page in angular js? 如果用户已登录,如何限制后退按钮,或直接在angularjs中访问登录页面 - if user is logged in, how to restrict the back button, or accessing the login page directly in angularjs 如果用户已经登录,则将用户重定向到受保护的路由会导致登录页面在重定向前重新呈现并显示一段时间 - Redirect user to protected route if user already logged in causes login page to re-render and display for sometime before redirection 如果用户已经使用 JWT 登录 React,则限制对登录页面的访问 - Restrict access to login page if the user is already signin in React with JWT 如果用户尚未登录Java servlet,如何重定向到jsp登录页面? - How to redirect to jsp login page, if user does not logged in already, Java servlet? 如何显示使用javascript登录html页面的用户数据? - How to display data of user logged in in an html page using javascript? 如何防止登录用户看到登录页面 - How to prevent logged in user to see login page 如果已经使用angularjs登录,如何避免显示登录页面? - How to avoid showing login page if already logged on with angularjs?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM