Advertisement
Guest User

Untitled

a guest
Oct 23rd, 2017
126
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import React, { Component } from 'react'
  2. import Nav from './navbar'
  3. import { Link } from 'react-router-dom'
  4. import axios from 'axios'
  5.  
  6.  
  7. class Login extends Component {
  8.    
  9.      constructor(props){
  10.         super(props);
  11.         this.state = {
  12.             email : '',
  13.             password: '',
  14.         }
  15.      }
  16.  
  17.      onSubmit(e){
  18.         e.preventDefault();
  19.         const {email , password} = this.state ;
  20.         axios.post('api/login', {
  21.             email,
  22.             password
  23.           })
  24.           .then(response=> {
  25.             this.setState({err: false});
  26.             this.props.history.push("home") ;
  27.            
  28.           })
  29.           .catch(error=> {
  30.             this.refs.email.value="";
  31.             this.refs.password.value="";
  32.             this.setState({err: true});
  33.           });
  34.      }
  35.  
  36.      onChange(e){
  37.         const {name, value} = e.target;
  38.         this.setState({[name]: value});
  39.      }
  40.  
  41.     render() {
  42.        
  43.         let error = this.state.err ;
  44.         let msg = (!error) ? 'Login Successful' : 'Wrong Credentials' ;
  45.         let name = (!error) ? 'alert alert-success' : 'alert alert-danger' ;
  46.         return (
  47.             <div >
  48.                 <Nav />
  49.                 <div className="container-fluid">
  50.                     <div className="row">
  51.                         <div className="col-md-8 col-md-offset-2">
  52.                             <div className="panel panel-default">
  53.                                 <div className="panel-heading">Login</div>
  54.                                 <div className="panel-body">  
  55.                                     <div className="col-md-offset-2 col-md-8 col-md-offset-2">
  56.                                         {error != undefined && <div className={name} role="alert">{msg}</div>}
  57.                                     </div>  
  58.                                     <form className="form-horizontal" role="form" method="POST" onSubmit= {this.onSubmit.bind(this)}>
  59.                                         <div className="form-group">
  60.                                             <label for="email" className="col-md-4 control-label">E-Mail Address</label>
  61.  
  62.                                             <div className="col-md-6">
  63.                                                 <input id="email" type="email" ref="email" className="form-control" name="email"  onChange={this.onChange.bind(this)} required />
  64.                                             </div>
  65.                                         </div>
  66.  
  67.                                         <div className="form-group">
  68.                                             <label for="password" className="col-md-4 control-label">Password</label>
  69.  
  70.                                             <div className="col-md-6">
  71.                                                 <input id="password" type="password" ref="password" className="form-control" name="password"  onChange={this.onChange.bind(this)}  required />
  72.                                             </div>
  73.                                         </div>
  74.  
  75.                                         <div className="form-group">
  76.                                             <div className="col-md-6 col-md-offset-4">
  77.                                                 <div className="checkbox">
  78.                                                     <label>
  79.                                                         <input type="checkbox" name="remember" /> Remember Me
  80.                                                     </label>
  81.                                                 </div>
  82.                                             </div>
  83.                                         </div>
  84.  
  85.                                         <div className="form-group">
  86.                                             <div className="col-md-8 col-md-offset-4">
  87.                                                 <button type="submit" className="btn btn-primary">
  88.                                                     Login
  89.                                                 </button>
  90.  
  91.                                                 <li className="btn btn-link">
  92.                                                     <Link to = "forgotpassword">Forgot Your Password?</Link>
  93.                                                 </li>
  94.                                             </div>
  95.                                         </div>
  96.                                     </form>
  97.                                 </div>
  98.                             </div>
  99.                         </div>
  100.                     </div>
  101.                 </div>
  102.             </div>
  103.            
  104.     );
  105. }
  106. }
  107.  
  108. export default Login;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement