Redux中利用函数式组件替代onEnter进行登录状态验证的实践

最近使用React和Redux构建一个后台项目,在做登录系统的时候,看了网上很多资料,一般都是使用sessionStorage(包括Cookie,下略)或者localStorage保存从服务器获取的token,然后使用react-router中onEnter这个方法依据sessionStorage或者localStorage中是否存在相应的token来判定登录状态。

Cookie,LocalStorage 与 SessionStorage的详解可以参考:详说 Cookie,LocalStorage 与 SessionStorage一文。

react-router的onEnter方法使用可以参考react-router官方的用例:auth-flow。
仓库地址:https://github.com/reactjs/re...

auth-flow这个用例使用localStorage来保存token,react-router的onEnter调用requireAuth方法来判断auth.loggedIn()是否能正确返回localStorage.token,来维持登录状态,这也是目前常用的做法。

app.js

import React from 'react'
import { render } from 'react-dom'
import { browserHistory,Router,Route,Link,withRouter } from 'react-router'
import auth from './auth'

const App = React.createClass({
  getInitialState() {
    return {
      loggedIn: auth.loggedIn()
    }
  },updateAuth(loggedIn) {
    this.setState({
      loggedIn: loggedIn
    })
  },componentWillMount() {
    auth.onChange = this.updateAuth
    auth.login()
  },render() {
    return (
      <div>
        <ul>
          <li>
            {this.state.loggedIn ? (
              <Link to="/logout">Log out</Link>
            ) : (
              <Link to="/login">Sign in</Link>
            )}
          </li>
          <li><Link to="/about">About</Link></li>
          <li><Link to="/dashboard">Dashboard</Link> (authenticated)</li>
        </ul>
        {this.props.children || <p>You are {!this.state.loggedIn && 'not'} logged in.</p>}
      </div>
    )
  }
})

const Dashboard = React.createClass({
  render() {
    const token = auth.getToken()

    return (
      <div>
        <h1>Dashboard</h1>
        <p>You made it!</p>
        <p>{token}</p>
      </div>
    )
  }
})

const Login = withRouter(
  React.createClass({

    getInitialState() {
      return {
        error: false
      }
    },handleSubmit(event) {
      event.preventDefault()

      const email = this.refs.email.value
      const pass = this.refs.pass.value

      auth.login(email,pass,(loggedIn) => {
        if (!loggedIn)
          return this.setState({ error: true })

        const { location } = this.props

        if (location.state && location.state.nextPathname) {
          this.props.router.replace(location.state.nextPathname)
        } else {
          this.props.router.replace('/')
        }
      })
    },render() {
      return (
        <form onSubmit={this.handleSubmit}>
          <label><input ref="email" placeholder="email" defaultValue="joe@example.com" /></label>
          <label><input ref="pass" placeholder="password" /></label> (hint: password1)<br />
          <button type="submit">login</button>
          {this.state.error && (
            <p>Bad login information</p>
          )}
        </form>
      )
    }
  })
)

const About = React.createClass({
  render() {
    return <h1>About</h1>
  }
})

const Logout = React.createClass({
  componentDidMount() {
    auth.logout()
  },render() {
    return <p>You are now logged out</p>
  }
})

function requireAuth(nextState,replace) {
  if (!auth.loggedIn()) {
    replace({
      pathname: '/login',state: { nextPathname: nextState.location.pathname }
    })
  }
}

render((
  <Router history={browserHistory}>
    <Route path="/" component={App}>
      <Route path="login" component={Login} />
      <Route path="logout" component={Logout} />
      <Route path="about" component={About} />
      <Route path="dashboard" component={Dashboard} onEnter={requireAuth} />
    </Route>
  </Router>
),document.getElementById('example'))

auth.js

module.exports = {
  login(email,cb) {
    cb = arguments[arguments.length - 1]
    if (localStorage.token) {
      if (cb) cb(true)
      this.onChange(true)
      return
    }
    pretendRequest(email,(res) => {
      if (res.authenticated) {
        localStorage.token = res.token
        if (cb) cb(true)
        this.onChange(true)
      } else {
        if (cb) cb(false)
        this.onChange(false)
      }
    })
  },getToken() {
    return localStorage.token
  },logout(cb) {
    delete localStorage.token
    if (cb) cb()
    this.onChange(false)
  },loggedIn() {
    return !!localStorage.token
  },onChange() {}
}

function pretendRequest(email,cb) {
  setTimeout(() => {
    if (email === 'joe@example.com' && pass === 'password1') {
      cb({
        authenticated: true,token: Math.random().toString(36).substring(7)
      })
    } else {
      cb({ authenticated: false })
    }
  },0)
}

localStorage等本地存储容器保存一些用户信息,多少可能会有潜在的风险,那么可不可以不使用这些本地存储来维持用户状态呢?

于是我尝试用redux结合react-router来保持用户的登录状态,最开始的思路是用onEnter调用一个方法来获取store里的登录状态信息,但是发现react-router的路由声明中并不能从store中拿到props,只有路由的history等信息。可能水平有限,只能到处翻文档,无意间在Github中发现一个用例:react-redux-jwt-auth-example

这个用例使用了一个高级函数(high-order function,用例中为requireAuthentication)来包装需要登录权限的Compenent(用例中为ProtectedView),这个Compenent位于所有需要登录权限的顶层:

routers.js

import {HomeView,LoginView,ProtectedView,AView,BView } from '../views';
import {requireAuthentication} from '../components/AuthenticatedComponent';

export default(
    <Route path='/' component={App}>
        <IndexRoute component={HomeView}/>
        <Route path="login" component={LoginView}/>
        <Route path="protected" component={requireAuthentication(ProtectedView)}
            <Route path="a" component={AView}/>
            <Route path="b" component={BVieew}/>
        </Route>
    </Route>
);

利用requireAuthentication()这个高阶函数将ProtectedView这个Compenent作为参数传人,requireAuthentication()中生成一个Compenent,然后调用react-redux中的connect结合mapStateToProps就能将store中的登录状态,token等信息塞入Props中,当前这个requireAuthentication中的Compenent根据Props中的状态信息来决定是否继续渲染ProtectedView Compenent,或者在用户进行页面跳转,检测到登录状态为false时,就会重定向到登录页面。

AuthenticatedComponent.js

import React from 'react';
import {connect} from 'react-redux';
import {pushState} from 'redux-router';

export function requireAuthentication(Component) {

    class AuthenticatedComponent extends React.Component {

        componentWillMount() {
            this.checkAuth();
        }

        componentWillReceiveProps(nextProps) {
            this.checkAuth();
        }

        checkAuth() {
            if (!this.props.isAuthenticated) {
                let redirectAfterLogin = this.props.location.pathname;
                this.props.dispatch(pushState(null,`/login?next=${redirectAfterLogin}`));
            }
        }

        render() {
            return (
                <div>
                    {this.props.isAuthenticated === true
                        ? <Component {...this.props}/>
                        : null
                    }
                </div>
            )

        }
    }

    const mapStateToProps = (state) => ({
        token: state.auth.token,userName: state.auth.userName,isAuthenticated: state.auth.isAuthenticated
    });

    return connect(mapStateToProps)(AuthenticatedComponent);

}

上面是作者给的用法,其中需要注意的是:

import {pushState} from 'redux-router';

由于几个react-router的区别这个问题,打包编译后可能报错,我项目中使用的是react-router-redux。

参考react-router-redux文档中What if I want to issue navigation events via Redux actions?

使用方法是在AuthenticatedComponent中:

import {push} react-router-redux

也就是push代替了原例子中的pushState,作用都差不多。

然后就是加一个中间件:

import { routerMiddleware,push } from 'react-router-redux'

// Apply the middleware to the store
const middleware = routerMiddleware(browserHistory)
const store = createStore(
  reducers,applyMiddleware(middleware)
)

这样就可以在AuthenticatedComponent中愉快地重定向了。

以上利用react-redux,redux-router || react-router-redux基于redux来保持登录状态和进行登录权限验证,可以避免使用Cookie&localStroge等来保存登录信息,其中的缺点就是,用户刷新页面或关闭浏览器后,登录状态就被销毁了,如果有记住用户名等需求,可能依然会用到本地存储容器。

翻阅这个用例最后还发现作者已经写了一个登录权限验证的library:
redux-auth-wrapper

不想搬运代码的兄弟可以参考文档直接拿来用~

第一次写文章,如果有概念错误的地方,请多多指正!!感谢!!

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐


react 中的高阶组件主要是对于 hooks 之前的类组件来说的,如果组件之中有复用的代码,需要重新创建一个父类,父类中存储公共代码,返回子类,同时把公用属性...
我们上一节了解了组件的更新机制,但是只是停留在表层上,例如我们的 setState 函数式同步执行的,我们的事件处理直接绑定在了 dom 元素上,这些都跟 re...
我们上一节了解了 react 的虚拟 dom 的格式,如何把虚拟 dom 转为真实 dom 进行挂载。其实函数是组件和类组件也是在这个基础上包裹了一层,一个是调...
react 本身提供了克隆组件的方法,但是平时开发中可能很少使用,可能是不了解。我公司的项目就没有使用,但是在很多三方库中都有使用。本小节我们来学习下如果使用该...
mobx 是一个简单可扩展的状态管理库,中文官网链接。小编在接触 react 就一直使用 mobx 库,上手简单不复杂。
我们在平常的开发中不可避免的会有很多列表渲染逻辑,在 pc 端可以使用分页进行渲染数限制,在移动端可以使用下拉加载更多。但是对于大量的列表渲染,特别像有实时数据...
本小节开始前,我们先答复下一个同学的问题。上一小节发布后,有小伙伴后台来信问到:‘小编你只讲了类组件中怎么使用 ref,那在函数式组件中怎么使用呢?’。确实我们...
上一小节我们了解了固定高度的滚动列表实现,因为是固定高度所以容器总高度和每个元素的 size、offset 很容易得到,这种场景也适合我们常见的大部分场景,例如...
上一小节我们处理了 setState 的批量更新机制,但是我们有两个遗漏点,一个是源码中的 setState 可以传入函数,同时 setState 可以传入第二...
我们知道 react 进行页面渲染或者刷新的时候,会从根节点到子节点全部执行一遍,即使子组件中没有状态的改变,也会执行。这就造成了性能不必要的浪费。之前我们了解...
在平时工作中的某些场景下,你可能想在整个组件树中传递数据,但却不想手动地通过 props 属性在每一层传递属性,contextAPI 应用而生。
楼主最近入职新单位了,恰好新单位使用的技术栈是 react,因为之前一直进行的是 vue2/vue3 和小程序开发,对于这些技术栈实现机制也有一些了解,最少面试...
我们上一节了了解了函数式组件和类组件的处理方式,本质就是处理基于 babel 处理后的 type 类型,最后还是要处理虚拟 dom。本小节我们学习下组件的更新机...
前面几节我们学习了解了 react 的渲染机制和生命周期,本节我们正式进入基本面试必考的核心地带 -- diff 算法,了解如何优化和复用 dom 操作的,还有...
我们在之前已经学习过 react 生命周期,但是在 16 版本中 will 类的生命周期进行了废除,虽然依然可以用,但是需要加上 UNSAFE 开头,表示是不安...
上一小节我们学习了 react 中类组件的优化方式,对于 hooks 为主流的函数式编程,react 也提供了优化方式 memo 方法,本小节我们来了解下它的用...
开源不易,感谢你的支持,❤ star me if you like concent ^_^
hel-micro,模块联邦sdk化,免构建、热更新、工具链无关的微模块方案 ,欢迎关注与了解
本文主题围绕concent的setup和react的五把钩子来展开,既然提到了setup就离不开composition api这个关键词,准确的说setup是由...
ReactsetState的执行是异步还是同步官方文档是这么说的setState()doesnotalwaysimmediatelyupdatethecomponent.Itmaybatchordefertheupdateuntillater.Thismakesreadingthis.staterightaftercallingsetState()apotentialpitfall.Instead,usecom