Redux 登录状态判断的一些实践

最近一直在写一个React、Redux的前端项目,登录状态验证这一块还是比较头疼的。

我的实践下有三种方式来验证用户登录状态,目前我选择用三种方式一起用在项目里面。

  1. Redux高阶函数验证(High-Order Function)

  2. Actions中间件验证

  3. Component WillMount 验证

之所以用三种方式一起是因为Redux高阶函数在性能调优的时候并不是特别容易。

Redux高阶函数验证

//把需要登录状态验证的Component传入到这个高阶函数中
export function requireAuthentication(Component) {
    class AuthenticatedComponent extends Component {
        constructor(props) {
            super(props)
        }
        //只在首次Mount时来验证权限
        componentWillMount() {
            this.checkAuth();
        }
        checkAuth(){
            const {path,isSignIn,dispatch}=this.props;
            if(!isSignIn){
                //没有登录
                //记录当前页面path
                //跳转到SignIn Page处理登录 登录完成够会跳回当前页面
                dispatch(CommonActions.setNextUrl(path))
                browserHistory.push('/sign');
            }
        }
        render() {
            console.log('auth render')
            return (
                <div>
                
                    {this.props.isSignIn == true
                        ? <Component  {...this.props}/>
                        : null
                    }
                </div>
            )

        }
    }

    const mapStateToProps = (state) => ({
        path:state.routing.locationBeforeTransitions.pathname,
        isSignIn:state.common.isSignIn,
        state:state
    })
    return connect(mapStateToProps)(AuthenticatedComponent);
}

你可以把它像这样用在router中:

{ path:'courseCate',component:requireAuthentication(CourseCate)}

目前存在的一些问题:

  • 高阶函数中传入的最顶层的Component不能再使用connect

  • 过多的高阶函数生命周期的逻辑难以维护、性能隐患

Actions中间件验证

export function checkAuth(nextUrl,nextFn) {
    return (dispatch,getState)=>{
        //检测用户登录情况
        if(!getState().common.isSignIn){
            //没有登录 记录当前path 
            //跳转到sign page 登录完成后 跳转回来
            dispatch(setNextUrl(nextUrl));
            pushUrl('/sign');//封装了 browserHistory.push(url)
        }else{
            //通过验证后 执行下一个Fn
            dispatch(nextFn);
        }
    }
}

你可以像这样用在你的Actions中

export function fetchFoo(url,conf) {

    return (dispatch,getState) => {
        if(shouldFetchFoo(getState())){
            dispatch(requestFetchFoo());
            return fetch(url,conf)
            .then(res=>res.json())
            .then(json=>{
                ...
            })
        }
    }
    
}
export function needFetchFoo(nextUrl,url,conf){
    retrun (dispatch)=>{
        dispatch(checkAuth(nextUrl,fetchFoo(url,conf)))
    }
}

目前存在的一些问题:

  1. 虽然可以避免过多的高阶函数函数导致页面性能下降,但是无法很好满足业务逻辑

  2. 验证如果未通过直接阻断当前操作

Component WillMount 验证

这基本上可以认为是Actions中间验证的一种变种

export function checkAuthWithoutNextFn(nextUrl) {
    return (dispatch,getState)=>{
        //check if user sign in
        if(!getState().common.isSignIn){
            //if not set the nexturl 
            //and push url to sign page
            dispatch(setNextUrl(nextUrl));
            pushUrl('/sign');
        }
    }
}

你可以像这样把他用在Component WillMount事件中

    componentWillMount(){
        const {dispatch} = this.props;
        dispatch(CommonActions.checkAuthWithoutNextFn('/coursecate'));
    }

目前存在的一些问题:

  • 权限未得到验证时,不会阻断子控件渲染,触发子控件生命周期中的事件

举个例子:
比如父控件中componetWillDidMount中调用该方法判断用户登录状态
fatherComponet.js

    componentWillMount(){
        const {dispatch} = this.props;
        dispatch(CommonActions.checkAuthWithoutNextFn('/coursecate'));
    }

然而子控件componentWillMount中有一个fetch是需要权限验证(此时父控件中并没有阻断子控件渲染,在父控件正在验证权限的同时,子控件的fetch执行了。高阶函数可以阻断子控件渲染)的。
虽然渲染顺序是fatherComponet->childComponet
但是childComponet里的componentWillMount也是触发了(也就是说,在未验证登录情况下就触发了fetch),可能会造成一些不必要的请求
我目前的处理方式:在子控件ComponentWillMount的fetch中加入一个shouldFetch()进行请求前判断

总结

1.高阶函数适合用在:子控件需要确定权限后渲染
2.actions中间件适合:无状态页面中的登录状态判断
3.component验证,使用范围就比较狭窄了。

大家雨露均沾

以上是我个人目前的一些小见解,欢迎各位指正和补充哈。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值