Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- handleLoginSubmit = (e) => {
- e.preventDefault()
- let { email,password } = this.state
- const data = {
- email : email,
- password : password
- }
- fetch('http://localhost:3001/auth/login',{
- method : 'post',
- body : JSON.stringify(data),
- headers : {
- "Content-Type":"application/json"
- }
- }).then(res => res.json())
- .then(data => {
- if(data.success){
- sessionStorage.setItem('userid',data.user.id)
- sessionStorage.setItem('email',data.user.email)
- }
- this.setState({loginData : data,
- userData : data,
- email:"",
- password:""})
- if(data.token) {
- Auth.authenticateUser(data.token)
- this.props.history.push('/dashboard')
- }
- this.handleLoginMessage()
- this.isUserAuthenticated()
- })
- }
- export default withRouter(AuthPage)
- const PrivateRoute = ({ component : Component, ...rest }) => {
- return (
- <Route {...rest} render = { props => (
- Auth.isUserAuthenticated() ? (
- <Component {...props} {...rest} />
- ) : (
- <Redirect to = {{
- pathname: '/',
- state: { from: props.location }
- }}/>
- )
- )}/>
- )
- }
- const PropsRoute = ({ component : Component, ...rest }) => (
- <Route {...rest} render = { props => (
- <Component {...props} {...rest} />
- )}/>
- )
- const Root = () => (
- <BrowserRouter>
- <Switch>
- <PropsRoute exact path = "/" component = { AuthPage } />
- <PrivateRoute path = "/dashboard" component = { DashboardPage } />
- <Route path = "/logout" component = { LogoutFunction } />
- <Route component = { () => <h1> Page Not Found </h1> } />
- </Switch>
- </BrowserRouter>
- )
Add Comment
Please, Sign In to add comment