authentication - 登录后我想重定向到 jitsiReact 页面
问题描述
登录并成功将 jwt 令牌存储在 localStorage 中后,如果我已登录,我想将登录重定向到仪表板页面,所以我无法访问登录或注册页面,这就是我想要的,请帮助解决这个问题,我不想要使用 redux
我正在使用受保护的路由器方法进行身份验证并将其导入 app.js 文件
----------
(App.js)
function App() {
const getAuth = localStorage.getItem('user-agent');
const isAuth = JSON.parse(getAuth);
return (
<div className="App">
<Suspense fallback={loading()}>
<Switch>
<Route exact path="/login" name="Login Page" render={props => <Login {...props} />} />
<Route exact path="/signup" name="Signup Page" render={props => <Signup {...props} />} />
<AuthRouter path="/" name="Jitsi" isLoggedin={isAuth ? isAuth.isAuthentication : false} component={DefaultLayout} />
</Switch>
</Suspense>
</div>
);
---------
---------
(AuthRouter.js)
function AuthRouter({ component: Component, isLoggedin, ...rest }) {
return (
<Route {...rest} render={(props) => (
isLoggedin === true ?
<Component {...rest} {...props} />
: <Redirect to="/login" />
)
} />
)
}
------------
------------
(Login.js)
export default class Login extends Component {
constructor(props) {
super(props);
this.state = {
email: '',
password: '',
error: ''
}
}
// get the inputs values on change
handleChange = e => {
this.setState({
[e.target.name]: e.target.value
})
}
// submit form
handleLogin = async e => {
e.preventDefault();
const res = await fetch('http://localhost:5000/api/login/user', {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({ ...this.state })
});
const json = await res.json();
if (res.status === 200) {
const userCredentials = JSON.stringify(json)
localStorage.setItem('user-agent', userCredentials)
this.props.history.push('/dashboard')
} else {
this.setState({
...this.state,
error: 'Something went wrong!!!'
})
}
}
render() {
return (
<div className="d-flex align-items-center login-form">
<div className="col-md-5 mx-auto border shadow-sm rounded p-3 p-md-5 bg-light">
<p className="text-danger text-center">{this.state.error}</p>
<form>
<div className="form-group">
<label>Email</label>
<input type="email" className="form-control" placeholder="Enter your email" name="email" onChange={this.handleChange} />
</div>
<div className="form-group">
<label>Password</label>
<input type="password" className="form-control" placeholder="Enter your password" name="password" onChange={this.handleChange} />
</div>
<div className="text-center">
<button onClick={this.handleLogin} type="submit" className="btn btn-primary btn-block">Login</button>
<small className="pt-3 d-block">-or-</small>
<Link to="/signup" className="btn btn-link">Create new account</Link>
</div>
</form>
</div>
</div>`enter code here`
)
}
}
------------
解决方案
推荐阅读
- php - 使用准备好的 select 语句循环遍历行
- model-view-controller - 非数据库“动作”在 MVC 中的位置在哪里?例如。发送电子邮件、使用网络服务等
- java - 如何将 @Context 注释字段注入 RestEasy @Provider?
- sql - SQL Server:如何从选择结果中删除相同/相反的值(+ 和 - 符号)
- r - 为(非引用)j 表达式提供名称作为字符向量
- python - 如何在python中创建图形列表?
- java - 如何将 Message 转换为 CharSequence 以在通知中显示来自套接字的消息?
- angular - 如何在 Angular 的隐藏表单字段中发送 web api 的值
- django - Django:authenticate() 只返回一种情况
- javascript - 以角度 7 动态加载组件时出错。未找到组件工厂