简体   繁体   English

您如何重定向未使用护照身份验证登录的用户

[英]how do you redirect a user who is not logged in using passport authentication

trying to stop a user from accessing my /mainpage route if they arent logged and to redirect them to login.... currently using passport and react router and components... here is my app js试图阻止用户访问我的 /mainpage 路由,如果他们没有登录并将他们重定向到登录....当前使用护照和反应路由器和组件...这是我的应用程序 js

    return (
      <Router>
      <div>
          <Route exact path="/" component={Login} />
          <Route exact path="/login" component={Login} />
          <Route exact path="/signup" component={Signup} />
          <Route exact path="/mainpage" component={MainPage} />
          <Footer />
      </div>
    </Router>
    );
  }
export default App;

this is the html-routes.js part im trying to make work这是我试图做的 html-routes.js 部分

app.get("/mainpage", isAuthenticated, (req, res) => {
    res.sendFile(path.join(__dirname, "/signup.html"));
  });

If you had an endpoint to validate their authentication status, as well as some persisted value (such as isAuthenticated ) stored in the UI you can set up an auth gate of sorts.如果您有一个端点来验证其身份验证状态,以及存储在 UI 中的一些持久值(例如isAuthenticated ),您可以设置各种身份验证门。

function AuthGate({ children ]} {
   
   return(
       <>
           {isAuthenticated && validationCheck() ? children : <Login />}
       </>
   )
}

and then pass render your protected routes as such然后通过渲染您的受保护路线

function App() {
    return(
        <Router>
             <div>
                 <Route exact path="/" component={Login} />
                 <Route exact path="/login" component={Login} />
                 <Route exact path="/signup" component={Signup} />
                 <AuthGate>
                     <Route exact path="/mainpage" component={MainPage} />
                 </AuthGate>
                 <Footer />
        </Router>
    )
}

this would ensure that as long as your checks to see if a user was authenticated were true, you would render the children of the AuthGate, otherwise return the Login page.这将确保只要您检查用户是否已通过身份验证是真的,您将呈现 AuthGate 的孩子,否则返回登录页面。 Once a user logs in, as long as there is no redirect logic, they will already be on the Mainpage route and view the content as desired.一旦用户登录,只要没有重定向逻辑,他们就已经在主页路由上并根据需要查看内容。 Any routes not wrapped in the AuthGate would be accessible regardless of authentication status.无论身份验证状态如何,任何未包含在 AuthGate 中的路由都可以访问。

You can try你可以试试

app.get('/mainpage', passport.authenticate('local', { successRedirect: '/', failureRedirect: '/login' })); app.get('/mainpage', passport.authenticate('local', { successRedirect: '/', failureRedirect: '/login' }));

1. declare Private Route and add auth logic 1.声明Private Route并添加auth逻辑

 // declare Private Route type PrivateRouteProps = { children: React.ReactNode path: string exact?: boolean rest?: never } const PrivateRoute: React.FC<PrivateRouteProps> = ( { children, path, exact, ...rest }: PrivateRouteProps) => { const store = useGlobalStore() const loggedIn = store.loggedIn console.log(store.loggedIn, '??') return ( <Route path={path} exact={exact} {...rest} render={(props) => loggedIn? ( children ): ( <Redirect to={{ pathname: '/login', // eslint-disable-next-line react/prop-types state: { from: props.location }, }} /> ) } /> ) }

2. use Private Router 2.使用私有路由器

 // use Router const Router: React.FC = () => ( <BrowserRouter> <Suspense fallback={<PageLoading />}> <Switch> <PrivateRoute path="/" exact> <Redirect to={'/welcome'} /> </PrivateRoute> <Route path="/login" exact> <Login /> </Route> <Route path="/403" exact> <Error403 /> </Route> <Route path="/404" exact> <Error404 /> </Route> <Route path="/500" exact> <Error500 /> </Route> <PrivateRoute path="*"> <LayoutRoutes /> </PrivateRoute> </Switch> </Suspense> </BrowserRouter> )

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

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