Reactjs React中的身份验证
当用户成功登录时,必须将用户重新定向到另一个页面。如果未登录,则无法访问此页面 这是登录表单代码Reactjs React中的身份验证,reactjs,authentication,redux,Reactjs,Authentication,Redux,当用户成功登录时,必须将用户重新定向到另一个页面。如果未登录,则无法访问此页面 这是登录表单代码 class LoginForm extends React.Component { handleFormSubmit = e => { e.preventDefault(); const user = e.target.elements.user.value; const password = e.target.elements.pas
class LoginForm extends React.Component {
handleFormSubmit = e => {
e.preventDefault();
const user = e.target.elements.user.value;
const password = e.target.elements.password.value;
this.props.onAuth(user, password);
};
componentDidUpdate() {
console.log(this.props.successLogin)
};
下面是MapStateTops和mapDispatchToProps函数
const mapStateToProps = (state) => {
return {
error: state.error,
successLogin: state.token
}
};
mapDispatchToProps = dispatch => {
return {
onAuth: (username, password) => dispatch(actions.authLogin(username, password))
}
};
这就是它的行动
export const authSuccess = token => {
return {
type: actionTypes.AUTH_SUCCESS,
token: token
}
};
这是登录页面中的代码
const PrivateRoute = ({component: Component, ...rest}) => (
<Route {...rest} render={(props) => (
rest.auth
? <Component {...props}/>
:
<Redirect to='/'/>
)}/>
);
console.log打印两次。一个null和一个带有实际标记的。
我的问题是,使console.log(this.props.successLogin)
print ones为空,然后使令牌为空的逻辑有什么错误,注销时反之亦然?
这带来的挑战是我的页面最终被重新定向到
``,尽管我已通过身份验证,并且我可以访问所有
PrivateRoute路径`在呈现之前,您必须检查用户是否已成功通过身份验证
{this.props.isAuthenticated && <PrivateRoute path="/Main_Page" exact component={Main_Page} auth={this.props.isAuthenticated}/>}
{this.props.isAuthenticated&&}
在呈现之前,您必须检查用户是否已成功通过身份验证
{this.props.isAuthenticated && <PrivateRoute path="/Main_Page" exact component={Main_Page} auth={this.props.isAuthenticated}/>}
{this.props.isAuthenticated&&}
以下是我如何修复它的
<Route exact path="/"render={() => (this.props.isAuthenticated ? (
<Redirect to="/Main_Page"/>) : (
<Log_in_Page/>
(this.props.isAuthenticated(
) : (
以下是我如何修复它的
<Route exact path="/"render={() => (this.props.isAuthenticated ? (
<Redirect to="/Main_Page"/>) : (
<Log_in_Page/>
(this.props.isAuthenticated(
) : (
您是否在使用类似于redux-thunk
?或redux-promise-middleware
?在这种情况下,第一个操作可能会返回默认值null
,然后当它再次更新时,我认为redux没有问题。@AdolfoOnrubia是的,我正在使用redux-thunk。这会导致一个小问题,因为我的页面最终会重新定向虽然我已通过身份验证,但我还是选择了“``”。您是否使用了类似于redux-thunk
?或redux-promise中间件
?在这种情况下,可能第一个操作返回默认值null
,然后当它再次更新时,我认为redux没有问题。@AdolfoOnrubia是的,我使用的是redux-thunk。这会导致轻微的错误因为我的页面最终被重新定向到“`”,尽管我已通过身份验证。您在哪里添加此检查?在加载主页之前,您必须检查用户是否经过身份验证。您在哪里添加此检查?在加载主页之前,您必须检查用户是否经过身份验证。