Guest User

Untitled

a guest
Oct 2nd, 2018
108
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.44 KB | None | 0 0
  1. import React from 'react';
  2. import {Button, FormGroup, FormControl, ControlLabel} from "react-bootstrap";
  3. import './login.css';
  4.  
  5. export default class loginForm extends React.Component {
  6. constructor(props) {
  7. super(props);
  8. this.state = {
  9. username: '',
  10. password: '',
  11. answer:[],
  12. };
  13. this.handleSubmit = this.handleSubmit.bind(this);
  14. }
  15.  
  16. async handleSubmit(event) {
  17. const jso = JSON.stringify({
  18. username: this.state.username,
  19. password: this.state.password
  20. })
  21. const response = await fetch("https://app.herokuapp.com/authentication", {
  22. method: 'POST',
  23. mode: 'cors',
  24. headers: {
  25. 'Accept': 'application/json',
  26. 'Content-Type': 'application/json'
  27. },
  28. body: jso,
  29. })
  30.  
  31. const json = await response.json()
  32.  
  33. this.setState({answer:json});
  34.  
  35. //check if user is authentificated
  36. alert("Server Answer : "+ this.state.answer.answer);
  37. if(this.state.answer.answer.localeCompare('true') == 0){
  38. this.props.app.setState({auth: true});
  39. sessionStorage.setItem('auth', true);
  40. }
  41. else if (this.state.username != ""){
  42. alert("INCORRECT USERNAME PASSWORD ");
  43. }
  44. }
  45.  
  46.  
  47. render() {
  48.  
  49. return (<div className="Login">
  50. <form onSubmit={this.handleSubmit}>
  51. //part omitted because not relevant for this question
  52. <Button bsSize="small" color="primary" type="submit">
  53. Login
  54. </Button >
  55. </form>
  56. </div>)
  57. }
  58. }
Add Comment
Please, Sign In to add comment