简体   繁体   中英

How can i implement jwt validation in my react app

I am new on this project and I don't understand how I can implement jwt auth in my react app. When I refresh my page I lose my session every time. I've got the token saved in localstorage, but I don't underestand where I am meant to check that for auth instead. Here is some code I think may be relevant

routes.js

export default (
<Route path='/' component={App}>
<IndexRedirect to='/in'/>
<Route path='in' component={Authentication(MainPage)}>
  <Route path='welcome' component={Welcome}/>
  <Route path='faq' component={Faq}/>
  <Route path='update_password/:token' component={UpdatePassword}/>
  <Route path='register_data_files' component={DataFileManagement}/>
  <Route path='manage_users' component={User}/>
  <Route path='user_access_logs' component={Logs}/>
  <Route path='flat_file' component={FlatFile}/>
  <Route path='edit_user' component={EditUser}/>
  <Route path='quick_reports' component={QuickReports}/>
  <Route path='*' component={NotFound}/>
</Route>
<Route path='faq' component={LoginBar}/>
<Route path='forgot_password' component={ForgotPassword}/>
<Route path='login' component={LoginPage}/>
<Route path='update_password/:token' component={UpdatePassword}/>
<Route path='*' component={NotFound}/>

)

mainPage.jsx

const MainPage = props => (
  <div className='app-container'>
    <ApplicationBar
      actions={props.actions}
      token={props.token}
      ui={props.ui}
      user={props.user}
    />
    {React.cloneElement(props.children, props)}
  </div>
)


MainPage.propTypes = {
  actions: PropTypes.object.isRequired,
  token: PropTypes.string.isRequired,
  user: PropTypes.object.isRequired
}

const mapStateToProps = state => ({
  token: state.token,
  user: state.user,
  ui: state.ui
})
const mapDispatchToProps = dispatch => ({ actions: 
bindActionCreators(actions, dispatch) })

export default connect(mapStateToProps, mapDispatchToProps)(MainPage)

authentication.jsx

export default function (Component) {
  class Authentication extends Component {
    componentWillMount() {
      this.pushToLoginIfNotAuthenticated(this.props.auth)
}

componentWillReceiveProps({auth}) {
  this.pushToLoginIfNotAuthenticated(auth)
}

pushToLoginIfNotAuthenticated(auth) {
  !auth && this.store.dispatch(push('/login'))
}

    render() {
      return this.props.auth && <Component {...this.props} />
    }
  }

  const mapStateToProps = state => ({ auth: true })
  const mapDispatchToProps = dispatch => ({ push: 
bindActionCreators(push, 
dispatch) })

  return connect(mapStateToProps, mapDispatchToProps)(Authentication)
}

Before your app starts you should check for localStorage for the token, and if it is not null, dispatch an action so you can set it on your state. I usually do this after setting my redux store. If you need some code sample just let me know and I will add it here! Bye!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM