Ignore:
Timestamp:
01/10/21 21:16:40 (4 years ago)
Author:
i-ina <76742075+i-ina@…>
Branches:
master
Children:
fc8421e
Parents:
17abe5e
Message:

user login

File:
1 edited

Legend:

Unmodified
Added
Removed
  • client_app/src/components/main/App.js

    r17abe5e r6f9b7b0  
    11import React, {Component} from 'react';
    2 import {BrowserRouter as Router, Route} from 'react-router-dom';
     2import {BrowserRouter as Router, Route, Redirect} from 'react-router-dom';
    33import Login from '../login/login';
    44import Profile from "../data/profile";
     
    1212    super(props);
    1313    this.state = {
    14       currentUser: {}
     14        logged: false,
     15        error: null,
     16        currentUser: {}
    1517    }
    1618  }
    1719
    1820  render(){
    19       return(
    20           <Router>
    21               <HeaderComp acc={this.state.currentUser.email} accType={this.state.currentUser.type} name={this.state.currentUser.name}/>
    22               <main>
    23                   <Route path={"/login"} render={() => <Login onCompleteForm={this.attemptLogin}/>}/>
    24                   <Route path={"/profile"} render={() => <Profile userProfile={this.state.currentUser}/>}/>
    25                   <Route path={"/"}/>
    26                   <Route path={"/jobs"}/>
    27                   <Route path={"/internships"}/>
    28               </main>
    29           </Router>
    30       );
     21          return(
     22              <Router>
     23                  <HeaderComp acc={this.state.currentUser.email} accType={this.state.currentUser.type} name={this.state.currentUser.name}/>
     24                  <main>
     25                      <Route path={"/login"} render={() => <Login error={this.state.error} onCompleteForm={this.attemptLogin} loggedIn={this.state.logged}/>}/>
     26                      <Route path={"/profile"} render={() => <Profile userProfile={this.state.currentUser}/>}/>
     27                      <Route path={"/"} render={() => <Redirect to={"/login"}/>}/>
     28                  </main>
     29              </Router>
     30          );
    3131  }
    3232
    3333  attemptLogin = (email, password, type) => {
    34 
    3534      UserLogin.login(email, password, type).then((res) =>{
    3635          console.log(res.data);
    37           this.setState({
    38               currentUser: res.data
    39           });
     36          if(res.data.email==null){
     37              this.setState({
     38                  logged: false,
     39                  error: res.data.error,
     40              });
     41          }else{
     42              this.setState({
     43                  logged: true,
     44                  currentUser: res.data,
     45                  error: null,
     46              });
     47          }
    4048      });
     49
     50      if(this.state.currentUser.email!=null){
     51          return <Redirect to={"/profile"}/>;
     52      }
    4153  }
    4254}
Note: See TracChangeset for help on using the changeset viewer.