import React from 'react';
import PropTypes from 'prop-types';
import {Route,Redirect,withRouter} from 'react-router-dom';
import LoginUser from 'service/LoginUser'; //私有路由,只有登录的用户才能访问
class PrivateRoute extends React.Component{
constructor(props) {
super(props);
this..state = {
isAuth : _loginUser.hasLogin();
}
}
componentWillMount(){
if(!isAuth){
const {history} = this.props;
setTimeout(() => {
history.replace("/login");
}, 1000)
}
}
render(){
const { component: Component,path="/",exact=false,strict=false} = this.props;
return this.state.isAuth ? (
<Route path={path} exact={exact} strict={strict} render={(props)=>( <Component {...props} /> )} />
) : <Authenricated />;
}
}
PrivateRoute.propTypes ={
path:PropTypes.string.isRequired,
exact:PropTypes.bool,
strict:PropTypes.bool,
component:PropTypes.func.isRequired
}
export default withRouter(PrivateRoute);

使用redux:

import { Component } from 'react'
import { connect } from 'react-redux'
import { get } from 'lodash'
import PropTypes from 'prop-types'
import toastr from 'toastr'
import { Route } from 'react-router-dom'
import { Redirect } from 'react-router'
import { LoadingIndicator } from 'components' export class PrivateRoute extends Component {
constructor (props) {
super(props) this.state = {
isLoading: true, // 是否於權限檢核中
isAuthed: false // 是否通過權限檢核
}
} static propTypes = {
component: PropTypes.any.isRequired,
funcCode: PropTypes.string.isRequired
} checkAuth = async () => {
let isAuthed = false
const { isLogin, funcCode } = this.props if (isLogin) { this.setState(state => ({ ...state, isLoading: true })) isAuthed = await api.checkAuthWithServer(funcCode)
} if (!isAuthed) { toastr.warning('系統未登录,请先登录')
} // 更新狀態 1.檢核結束 2.檢核結果
this.setState(state => ({ ...state, isAuthed: isAuthed, isLoading: false }))
} componentWillMount = async () => {
await this.checkAuth()
} componentWillReceiveProps = async (nextProps) => {
if (nextProps.location.pathname !== this.props.location.pathname) {
await this.checkAuth()
}
} render () {
const { component: Component, ...rest } = this.props
const { isLoading, isAuthed } = this.state return (
isLoading === true
? <LoadingIndicator />
: <Route {...rest} render={props => (
isAuthed
? <Component {...props} />
: <Redirect to={{ pathname: '/login', state: { from: props.location } }} />
)} />
)
} } const mapStateToProps = state => ({
// 登入系統後會於 redux 中註記登入狀態
isLogin: get(state, 'auth.isLogin')
}) const mapDispatchToProps = dispatch => ({
}) export default connect(mapStateToProps, mapDispatchToProps)(PrivateRoute)

update privateRoute:

import React from 'react';
import {toastr} from "react-redux-toastr";
import {Route, withRouter} from 'react-router-dom';
import LoginUser from 'service/login-service/LoginUser'; import Unauthorized from "page/error/Unauthorized"; const _loginUser = new LoginUser();
//私有路由,只有登录的用户才能访问
class PrivateRoute extends React.Component{
constructor(props) {
super(props);
this.state = {
isAuth : _loginUser.hasLogin()
}
}
componentWillMount(){
if(!this.state.isAuth){
toastr.error('login timeOut, return to the login page after 3s');
const {history} = this.props;
setTimeout(() => {
history.replace("/login");
}, 3000)
}
}
render(){
const { component: Component, path="/", exact=false, strict=false} = this.props;
return this.state.isAuth ? (
<Route path={path} exact={exact} strict={strict}
render={(props)=>( <Component {...props} /> )} />) : <Unauthorized />;
}
}
export default withRouter(PrivateRoute);

最新文章

  1. Java getResourceAsStream() 方法会缓存文件的问题
  2. 【USACO 1.3】Combination Lock
  3. lua操作json,mysql,redis等
  4. SharpGL学习笔记(十六) 多重纹理映射
  5. phalcon: (非官方)简单的多模块
  6. .NET使用OpenSSL生成的pem密钥文件
  7. 移植QT到ZedBoard(制作运行库镜像) 交叉编译 分类: ubuntu shell ZedBoard OpenCV 2014-11-08 18:49 219人阅读 评论(0) 收藏
  8. .c 文件取为.o文件
  9. oracle 查询前一小时、一天、一个月、一年的数据
  10. 【jsp】读取WebRoot下的图像文件
  11. bootstrap 基本页面元素,代码,列表
  12. python IP地址转16进制
  13. 地理位置 API
  14. [Git]2018-10 解决git cmd中文乱码问题
  15. Oier们的镜子(mirror)
  16. laravel中的storePublicly对上传的文件设置上传途径
  17. linux服务器mysql数据库新建数据库并配置数据库用户
  18. 如何使用PhoneGap打包Web App
  19. [java] 虚拟机(JVM)底层结构详解[转]
  20. 配置caffe中出现的问题汇总

热门文章

  1. 004-安装CentOS7后需要的操作
  2. android6.0获取通讯录权限
  3. Java并发—原子类,java.util.concurrent.atomic包(转载)
  4. Python之验证码
  5. day14生成器
  6. LightOJ - 1370 Bi-shoe and Phi-shoe (欧拉函数打表)
  7. SVN插件下载地址及更新地址
  8. JavaScript创建类的三种方式
  9. alter table add constraint 用法
  10. 【JavaScript】撞墙的小球