react privateRoute
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);
react privateRoute的更多相关文章
- arcgis api 4.x for js 结合 react 入门开发系列react全家桶实现加载天地图(附源码下载)
基于两篇react+arcgis的文章介绍,相信大家也能体会两者的开发区别了.在“初探篇”中作者也讲述了自己的选择,故废话不多说,本篇带大家体验在@arcgis/webpack-plugin环境下,使 ...
- react的路由权限控制
在使用路由的时候,有的时候我们的界面只能够在登录之后才可以看的到,这个时候就需要使用路由权限控制了 找了资料发现一个就是我使用的方法,一个是高阶组件. 原谅菜鸟看不太懂不会使用高阶组件………… 首先在 ...
- react的登录逻辑
https://blog.csdn.net/qq_36822018/article/details/83028661(先看看这个 https://blog.csdn.net/weixin_342681 ...
- react组件的生命周期
写在前面: 阅读了多遍文章之后,自己总结了一个.一遍加强记忆,和日后回顾. 一.实例化(初始化) var Button = React.createClass({ getInitialState: f ...
- 十分钟介绍mobx与react
原文地址:https://mobxjs.github.io/mobx/getting-started.html 写在前面:本人英语水平有限,主要是写给自己看的,若有哪位同学看到了有问题的地方,请为我指 ...
- RxJS + Redux + React = Amazing!(译一)
今天,我将Youtube上的<RxJS + Redux + React = Amazing!>翻译(+机译)了下来,以供国内的同学学习,英文听力好的同学可以直接看原版视频: https:/ ...
- React 入门教程
React 起源于Facebook内部项目,是一个用来构建用户界面的 javascript 库,相当于MVC架构中的V层框架,与市面上其他框架不同的是,React 把每一个组件当成了一个状态机,组件内 ...
- 通往全栈工程师的捷径 —— react
腾讯Bugly特约作者: 左明 首先,我们来看看 React 在世界范围的热度趋势,下图是关键词“房价”和 “React” 在 Google Trends 上的搜索量对比,蓝色的是 React,红色的 ...
- 2017-1-5 天气雨 React 学习笔记
官方example 中basic-click-counter <script type="text/babel"> var Counter = React.create ...
随机推荐
- alter session set events
.alter session set events 一.Oracle跟踪文件 Oracle跟踪文件分为三种类型,一种是后台报警日志文件,记录数据库在启动.关闭和运行期间后台进程的活动情况,如表空 ...
- Delphi 正则表达式语法(7): 匹配转义字符
Delphi 正则表达式语法(7): 匹配转义字符 // ? 号的意义是匹配 0-1 次, 如果需要匹配 ? 怎么办 var reg: TPerlRegEx; begin reg := TPe ...
- webservice -- cxf客户端调用axis2服务端
背景: 有个项目, 需要由第三方提供用户信息, 实现用户同步操作, 对方给提供webservice接口(axis2实现)并也使用axis2作主客户端调用我方提供的webservice接口 起初, 由于 ...
- eclipse添加tomcat运行时
方法一:添加jar包 方法二配置依赖 比如缺少javax.servlet.http.HttpServlet,ctrol+shift+t查找这个包 <dependencies> <de ...
- Codeforces Round #385 (Div. 1) C. Hongcow Buys a Deck of Cards
地址:http://codeforces.com/problemset/problem/744/C 题目: C. Hongcow Buys a Deck of Cards time limit per ...
- 解决Can't connect to local MySQL server through socket '/tmp/mysql.sock'
0 Problem 电脑重启后用python的MySQLdb连接数据库报错 Can't connect to local MySQL server through socket '/tmp/mysql ...
- HDU 1241 油田
这道题明明很简单但不知道为什么运行结果一直错,但提交却是对的!代码真是神奇,不过我猜测可能是提上给出的数据错了,可能提上给的数据m和n后多给了一个空格或回车,但题的数据没有 #include<s ...
- tomcat深入学习—权限篇
如果想给一个应用程序配置权限,可能需要的代码量很多,但你知道吗,仅通过配置tomcat,就能有同样的效果(为一个web应用添加了权限) 效果:打开http://localhost:8080/searc ...
- 搭建maven项目步骤
整体项目结构如下: 第一步 第二步 第三步:删除src目录,只留pom文件 第四步: 第五步: 6 7 8 9 10 11 12
- HttpServletResponse 的 sendError( )方法以及常用的HttpServletResponse常量级错误代码
HttpServletResponse 的 sendError( )方法以及常用的HttpServletResponse常量级错误代码 转载:http://hi.baidu.com/yanfei_ ...