Advertisement
Guest User

Untitled

a guest
Dec 2nd, 2018
140
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import React, { Component } from "react";
  2. import { PageHeader, Button, FormGroup, FormControl, ControlLabel } from "react-bootstrap";
  3. import { Link, Redirect } from "react-router-dom";
  4. import "./Login.css";
  5.  
  6.  
  7. class Register extends Component {
  8.   constructor(props) {
  9.     super(props);
  10.     this.state = {
  11.       username: "",
  12.       password: "",
  13.       isRegistered: false,
  14.     };
  15.   };
  16.  
  17.   usernameChanged = (event) => {
  18.     this.setState({ username: event.target.value });
  19.   }
  20.  
  21.   passwordChanged = (event) => {
  22.     this.setState({ password: event.target.value });
  23.   }
  24.  
  25.   handleRegister = (event) => {
  26.     if(event === "true"){
  27.       this.setState({ isRegistered: true });
  28.     }
  29.   }
  30.  
  31.  
  32.   // not sure where does authenticationObject.authenticate come from ....
  33.   register = () => {
  34.       authenticationObject.authenticate(this.state.username, this.state.password, this.handleRegister)
  35.   }
  36.    /* We are Registering.
  37.   login = () => {
  38.     authenticationObject.authenticate(this.state.username, this.state.password, this.handleLogin);
  39.   }
  40.   */
  41.         /*
  42.       return <Redirect to={"/profile/" + sessionStorage.getItem('id')} />;
  43.       */
  44.  
  45.  
  46.   render() {
  47.  
  48.     if(this.state.isRegistered){
  49.         return <Redirect to={"/profile/" + sessionStorage.getItem('id')} />;
  50.     }
  51.     return (
  52.       <div>
  53.         <PageHeader><center>Register</center></PageHeader>
  54.         <div className="Register">
  55.           <input type="text" placeholder="Username" value={this.state.username} onChange={this.usernameChanged} />
  56.           <input type="text" placeholder="Password" value={this.state.password} onChange={this.passwordChanged} />
  57.           <button onClick={this.register}> Sign up  </button>
  58.         </div>
  59.       </div>
  60.     );
  61.   }
  62. }
  63.  
  64. const authenticationObject = {
  65.   authenticate(username, password, cb){
  66.     fetch('/api/login', {
  67.       method: "POST",
  68.       headers: {
  69.         "Content-Type": "application/json; charset=utf-8",
  70.       },
  71.       body: JSON.stringify({
  72.         username: username,
  73.         password: password,
  74.       }),
  75.     }).then(response => {
  76.       if(response.status === 200){
  77.         console.log("Correct Credentials");
  78.         return response.json();
  79.       }
  80.     }).then(body => {
  81.       console.log(body.username);
  82.       console.log(body.id);
  83.       sessionStorage.setItem('id', body.id);
  84.       sessionStorage.setItem('username', body.username);
  85.       sessionStorage.setItem('loggedIn', "true");
  86.       cb(sessionStorage.getItem('loggedIn'));
  87.     }).catch(() => {
  88.       console.log("Wrong Credentials");
  89.       cb(sessionStorage.getItem('loggedIn'));
  90.     })
  91.   },
  92. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement