我正在构建一个全栈的React应用程序。我已经实现了React Router v4保护的路由。一切正常,但问题在于,用户在刷新链接上登录后,它将重定向到登录页面。
class Auth {
constructor() {
this.authenticated = false;
}
login(cb) {
this.authenticated = true;
cb();
console.log("login status"+this.authenticated)
}
logout(cb) {
this.authenticated = false;
cb();
}
isAuthenticated() {
console.log("check status"+this.authenticated)
return this.authenticated;
}
}
export default new Auth();
import React from "react";
import { Route, Redirect,withRouter } from "react-router-dom";
import Auth from "./Auth";
const ProtectedRoute = ({component: Component,...rest}) => {
return (
<Route
{...rest}
render={props => {
if (Auth.isAuthenticated()) {
return <Component {...props} />;
} else {
return (
<Redirect
to={{
pathname: "/admin/login",
state: {
from: props.location
}
}}
/>
);
}
}}
/>
);
};
export default withRouter(ProtectedRoute);
import React, { Component } from 'react';
import { BrowserRouter, Switch, Route} from 'react-router-dom';
import './css/style.css'
import './css/font-awesome.css'
import './css/jquery-ui.css'
import './App.css';
import './css/bootstrap.css'
import 'bootstrap'
import 'jquery'
import Login from './components/Login';
import Dashboard from './components/Dashboard'
import Users from './components/Users'
import UserProfile from './components/UserProfile';
import Categories from './components/Categories'
import ContactUs from './components/ContactUs'
import QueryDetails from './components/QueryDetails'
import EditProfile from './components/EditProfile'
import AdminProfile from './components/AdminProfile'
import ChangePassword from './components/ChangePassword'
import SaleOrPurchaseList from './components/SaleOrPurchaseList'
import ProductDetails from './components/ProductDetails'
import ReportList from './components/ReportList'
import NotFound from './components/NotFound'
import ProtectedRoute from './components/ProtectedRoute';
class App extends Component {
render() {
return (
<BrowserRouter>
<Switch>
<Route exact path="/admin/login" component={Login}/>
<ProtectedRoute path='/admin/dashboard' component={Dashboard}/>
<ProtectedRoute path='/admin/users' component={Users}/>
<ProtectedRoute path='/admin/userProfile' component={UserProfile}/>
<ProtectedRoute path='/admin/saleOrPurchaseList' component={SaleOrPurchaseList}/>
<ProtectedRoute path='/admin/ProductDetails' component={ProductDetails}/>
<ProtectedRoute path='/admin/categories' component={Categories}/>
<ProtectedRoute path='/admin/reportList' component={ReportList}/>
<ProtectedRoute path='/admin/queries' component={ContactUs}/>
<ProtectedRoute path='/admin/query_details' component={QueryDetails}/>
<ProtectedRoute path='/admin/profile' component={AdminProfile}/>
<ProtectedRoute path='/admin/edit_profile' component={EditProfile}/>
<ProtectedRoute path='/admin/change_password' component={ChangePassword}/>
<Route path="*" component={NotFound} />
</Switch>
</BrowserRouter>
);
}
}
export default App;
答案 0 :(得分:1)
您可以使用localStorage
来处理身份验证。
在登录功能中,您可以设置auth状态的值,例如locallocal.setItem('isAuth',authenticated)。在注销功能localStorage.removeItem('isAuth')
中。根据{{1}}
localStorage
答案 1 :(得分:0)
this.authenticated
默认为false
,因此只要ProtectedRoute
在调用Auth.login
之前呈现,它将重定向到登录页面。您需要确保this.authenticated
在渲染ProtectedRoute
之前获得正确的值。
请注意,调用ProtectedRoute
或Auth.login
之后,您的代码很可能不会触发Auth.logout
的重新呈现。因此ProtectedRoute
将不会对this.authenticated
的更改做出反应。