Advertisement
Guest User

Untitled

a guest
Jul 9th, 2018
104
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import React, { Component } from 'react'
  2. import axios from 'axios'
  3.  
  4. import Message from './message'
  5.  
  6. class LogIn extends Component {
  7.   constructor (props) {
  8.     super(props)
  9.     this.state = {
  10.       username: '',
  11.       password: '',
  12.       message: '',
  13.       type: ''
  14.     }
  15.   }
  16.   render () {
  17.     const { message, type } = this.state
  18.  
  19.     return (
  20.       <div className="login">
  21.         <div className="title">Log in to your account.</div>
  22.         <div className="sub-title">Provide your previously created username and password.</div>
  23.         <div className="form__section">
  24.           Username: <input type="text" value={this.state.username} onChange={e => this.setState({ username: e.target.value })} />
  25.         </div>
  26.         <div className="form__section">
  27.           Password: <input type="password" value={this.state.password} onChange={e => this.setState({ password: e.target.value })} />
  28.         </div>
  29.         <div className="form__section">
  30.           <button onClick={this.logIn.bind(this)}>Log in</button>
  31.         </div>
  32.         <Message text={message} type={type} />
  33.       </div>
  34.     )
  35.   }
  36.   logIn () {
  37.     const { username, password } = this.state
  38.  
  39.     axios.post('/login', {
  40.       username,
  41.       password
  42.     })
  43.       .then(res => {
  44.         const { status } = res.data
  45.         if (status === 'success') {
  46.           this.setState({
  47.             message: 'Logged in!',
  48.             type: 'success'
  49.           })
  50.           this.props.history.push('/')
  51.         } else {
  52.           this.setState({
  53.             message: 'Invalid username/password combination.',
  54.             type: 'info'
  55.           })
  56.         }
  57.       })
  58.       .catch(err => {
  59.         this.setState({
  60.           message: 'Could not connect to the server.',
  61.           type: 'error'
  62.         })
  63.       })
  64.   }
  65. }
  66.  
  67. export default LogIn
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement