index.tsx 1.7 KB
Newer Older
ada committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58
import * as React from 'react'
import { inject, observer } from 'mobx-react'
import { Route, RouteProps } from 'react-router-dom'

import { ComponentExt } from '@utils/reactExt'
import { getCookie } from '@utils/index'
import { COOKIE_KEYS } from '@constants/index'

export interface IStoreProps {
    routerStore?: RouterStore
    userInfo?: IAuthStore.UserInfo
    initUserInfo?: () => IAuthStore.UserInfo
}

@inject(
    (store: IStore): IStoreProps => {
        const { routerStore, authStore } = store
        const { userInfo, initUserInfo } = authStore
        return { routerStore, userInfo, initUserInfo }
    }
)
@observer
class PrivateRoute extends ComponentExt<IStoreProps & RouteProps> {
    gotoLogin = () => {
        const { routerStore } = this.props
        routerStore.history.replace('/login')
    }

    checkLocalUserInfo = () => {
        const token = getCookie(COOKIE_KEYS.TOKEN)
        if (!token) {
            return this.gotoLogin()
        }
        const { userInfo, initUserInfo } = this.props
        if (!userInfo) {
            try {
                const userInfoByInit = initUserInfo()
                if (userInfoByInit.token !== token) {
                    throw new Error('cookie 上储存的token与localStorage 上储存的token不一致!')
                }
            } catch (err) {
                console.warn(err)
                this.gotoLogin()
            }
        }
    }

    componentDidMount() {
        this.checkLocalUserInfo()
    }

    render() {
        const { component: Component, ...rest } = this.props
        return <Route {...rest} render={props => <Component {...props} {...rest} />} />
    }
}

export default PrivateRoute