Advertisement
rhuntington

loginform

May 16th, 2019
164
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import React, { Component } from 'react';
  2. import './App.css';
  3. import { withStyles } from '@material-ui/core/styles';
  4. import Typography from '@material-ui/core/Typography';
  5. import Button from '@material-ui/core/Button';
  6. import TextField from '@material-ui/core/TextField';
  7. import { Link, Redirect, Switch } from 'react-router-dom';
  8. import Axios from 'axios';
  9.  
  10. const styles = {
  11.     root: {
  12.       backgroundColor: '#1262e2',
  13.       color: 'white',
  14.     },
  15.     grow: {
  16.       flexGrow: 1,
  17.     },
  18.     form: {
  19.       marginTop: '5em',
  20.       color: 'Black',
  21.       backgroundColor: 'white',
  22.       width: 550,
  23.       height: 350,
  24.       borderRadius: 5,
  25.      
  26.     },
  27.     input: {
  28.       width: 300,
  29.       color: 'white',
  30.       marginTop: '1em',
  31.       marginBottom: '0.5em',
  32.     }
  33.   };
  34.  
  35. class LoginForm extends Component {
  36.     constructor(props) {
  37.         super(props);
  38.    
  39.         this.onChangeUsername = this.onChangeUsername.bind(this);
  40.         this.onChangePassword = this.onChangePassword.bind(this);
  41.         this.onSubmit = this.onSubmit.bind(this);
  42.    
  43.         this.state = {
  44.           username: '',
  45.           password: '',
  46.           authenticated: false,
  47.         };
  48.     }
  49.     componentDidMount() {
  50.         fetch('/users/authenticated')
  51.         .then(res => res.text())
  52.         .then(
  53.           (res) => {
  54.             if(res === 'true') {
  55.               this.setState({authenticated: true});
  56.             } else {
  57.               this.setState({authenticated: false});
  58.             }
  59.           }
  60.         )
  61.         .catch(err => console.log(err));
  62.       }
  63.       onChangeUsername = (e) => {
  64.         this.setState({username: e.target.value});
  65.       };
  66.    
  67.       onChangePassword = (e) => {
  68.         this.setState({password: e.target.value});
  69.       };
  70.    
  71.       onSubmit(e) {
  72.         const params = {
  73.           username: this.state.username,
  74.           password: this.state.password
  75.         };
  76.         Axios.post('/users/login', params).then((res) => {
  77.           if(res.status === 200) {
  78.             this.setState({ authenticated: true });
  79.           }
  80.         }).catch((err) => {
  81.           console.log(err);
  82.         });
  83.         e.preventDefault();
  84.     };
  85.  
  86.     render() {
  87.         const { authenticated } = this.state;
  88.         if(authenticated === true) {
  89.             return (authenticated === true ? <div><p className="redirect">Redirecting you...</p><Redirect  to= '/income' /></div>: <p>Oops</p>);
  90.         } else {
  91.             return(
  92.                 <div>
  93.                     <form className={this.props.classes.form} onSubmit={this.onSubmit}>
  94.                         <Typography variant="h5" color="inherit">Login</Typography>
  95.                         <TextField
  96.                             name="username"
  97.                             label="Enter Your Username"
  98.                             required
  99.                             autoFocus
  100.                             className={this.props.classes.input}
  101.                             variant="outlined"
  102.                             value={this.state.username}
  103.                             onChange={this.onChangeUsername}
  104.                         />
  105.                         <TextField
  106.                             label="Enter Your Password"
  107.                             className={this.props.classes.input}
  108.                             variant="outlined"
  109.                             type="password"
  110.                             value={this.state.password}
  111.                             onChange={this.onChangePassword}
  112.                         />
  113.                         <div className="form-group">
  114.                             <Button variant="contained" className={this.props.classes.root} type="submit">Login</Button>
  115.                         </div>
  116.                         <Switch>
  117.                             <Link to='/register'>Not Signed Up? Click Here to Register...</Link>
  118.                         </Switch>
  119.                     </form>
  120.                 </div>
  121.             )
  122.         }
  123.     }
  124. }
  125.  
  126. export default withStyles(styles)(LoginForm);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement