Guest User

Untitled

a guest
May 24th, 2018
84
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.70 KB | None | 0 0
  1. handleLoginSubmit = (e) => {
  2. e.preventDefault()
  3. let { email,password } = this.state
  4. const data = {
  5. email : email,
  6. password : password
  7. }
  8. fetch('http://localhost:3001/auth/login',{
  9. method : 'post',
  10. body : JSON.stringify(data),
  11. headers : {
  12. "Content-Type":"application/json"
  13. }
  14. }).then(res => res.json())
  15. .then(data => {
  16. if(data.success){
  17. sessionStorage.setItem('userid',data.user.id)
  18. sessionStorage.setItem('email',data.user.email)
  19. }
  20. this.setState({loginData : data,
  21. userData : data,
  22. email:"",
  23. password:""})
  24. if(data.token) {
  25. Auth.authenticateUser(data.token)
  26. this.props.history.push('/dashboard')
  27. }
  28. this.handleLoginMessage()
  29. this.isUserAuthenticated()
  30. })
  31. }
  32.  
  33. export default withRouter(AuthPage)
  34.  
  35. const PrivateRoute = ({ component : Component, ...rest }) => {
  36. return (
  37. <Route {...rest} render = { props => (
  38. Auth.isUserAuthenticated() ? (
  39. <Component {...props} {...rest} />
  40. ) : (
  41. <Redirect to = {{
  42. pathname: '/',
  43. state: { from: props.location }
  44. }}/>
  45. )
  46. )}/>
  47. )
  48. }
  49.  
  50. const PropsRoute = ({ component : Component, ...rest }) => (
  51. <Route {...rest} render = { props => (
  52. <Component {...props} {...rest} />
  53. )}/>
  54. )
  55.  
  56. const Root = () => (
  57. <BrowserRouter>
  58. <Switch>
  59. <PropsRoute exact path = "/" component = { AuthPage } />
  60. <PrivateRoute path = "/dashboard" component = { DashboardPage } />
  61. <Route path = "/logout" component = { LogoutFunction } />
  62. <Route component = { () => <h1> Page Not Found </h1> } />
  63. </Switch>
  64. </BrowserRouter>
  65. )
Add Comment
Please, Sign In to add comment