如何通过在本地存储中存储令牌来保持用户登录使用



我遵循本教程来实现身份验证:https://medium.com/technest/implement-user-auth-in-a-django-react-app-with-knox-fc56cdc9211c

当用户转到我的应用程序的基本url时,如果他们没有经过身份验证,它会将他们重定向到登录页面。这就是我的app.js的样子:

class App extends Component {
componentDidMount() {
store.dispatch(loadUser());
}
render() {
return (
<Provider store={store}>
<Router>
<Sidebar />
<Switch>
<PrivateRoute exact path='/' component={Dashboard} />
<Route exact path='/feed' component={Feed} />
<Route exact path='/register' component={RegisterForm} />
<Route exact path='/login' component={LoginForm} />

</Switch>
</Router>
</Provider>
);
}
}
export default App;

我的私人路线.js:

const PrivateRoute = ({ component: Component, auth, ...rest }) => (
<Route
{...rest}
render={props => {
if (auth.isLoading) {
return <div>Loading...</div>;
} else if (!auth.isAuthenticated) {
return <Redirect to='/login' />;
} else {
return <Component {...props} />;
}
}}
/>
);
const mapStateToProps = state => ({
auth: state.auth
});
export default connect(mapStateToProps)(PrivateRoute);

在这里,身份验证工作正常,并且正确地链接到我的Dashboard.js组件。在我的Dashboard.js中,我只是在渲染我拥有的另一个组件Profile.js,它显示用户的配置文件。

在我展示的第一个代码(App.js(中,在标签之外,我有一个Sidebar.js组件,无论用户是否登录,它都会被呈现

class Sidebar extends Component {
// Button event to switch to feed page.
render() {
const { user, isAuthenticated } = this.props.auth;
return (
<>
<div id="sidebar">
<a onClick={this.props.logout} id="logout_btn">
<span className="fa fa-lock"></span></a>
<NavLink id="profile_btn" to="/">
<span className="fa fa-user-circle"></span></NavLink>
<NavLink id="feed_btn" to="/feed">
<span className="fa fa-address-book"></span></NavLink>
</div>
<div id="brand">
<h1>Cook Simple</h1>
<p id="username-goes-here"></p>
<a id="add_btn" href="{% url 'recipe:recipe_new' %}">
<span class="fa fa-plus"></span> </a>
</div>
</>
);
}
}
const mapStateToProps = state => ({
auth: state.auth
});
export default connect(mapStateToProps, { logout })(Sidebar);

在这个侧边栏上,我有一个按钮链接到前面提到的Profile,另一个按钮连接到另一个名为Feed的组件。单击Profile按钮可以很好地工作——它会转到我的基本url,该url会转到PrivateRoute,查看用户是否已通过身份验证,并呈现Profile。但是,单击Feed的按钮会显示道具"auth"未定义。Feed:的代码

class Feed extends Component {
componentDidMount() {
this.props.getAllRecipes();
}
setGrid() {
try {
document.getElementById("grid-container-PROFILE").id = "grid-container-FEED"
console.log("Feed Loaded")
}
catch {
console.log("Feed Already Loaded")
}
}
render() {
this.setGrid()
return (
<>
<div id="brand">
<h1>Title Goes Here</h1>
</div>
<div className="title-FEED" id="recipecards-FEED">
<div id="recipecard-container-FEED">
<RecipeList />
</div>
</div>
</>
);
}
}
const mapStateToProps = (state) => {
return {
all_recipes: state.all_recipes,
auth: state.auth
}
}
const mapDispatchToProps = () => {
return {
getAllRecipes,
}
}
export default connect(mapStateToProps, mapDispatchToProps())(Feed)

在这个组件中,我希望能够访问auth.user来呈现用户对象中的一些数据。但是,auth似乎是未定义的。

我用于身份验证的操作和减速器与链接的教程相同。以下是他们的注册表:

身份验证操作:https://gist.github.com/kjmczk/2c287f1d615824c627580bd2a8067fcb#file-auth-js

授权还原器:https://gist.github.com/kjmczk/64f302546e4cd38ff922cf5d59192b8e#file-auth-js

似乎有语法错误。尝试更换:

connect(mapStateToProps, mapDispatchToProps())(Feed);

带有:

connect(mapStateToProps, mapDispatchToProps)(Feed);

删除括号。

我发现了这个问题。当使用React DevTools查看我的道具时,组件似乎都有auth道具,但每当我尝试实际使用它时,我都会出错。我只在检查用户是否通过身份验证后才访问道具,从而解决了这个问题。

最新更新