Advertisement
Guest User

Untitled

a guest
Aug 24th, 2017
71
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.96 KB | None | 0 0
  1. import React, { Component } from 'react';
  2.  
  3. export default class Login extends Component{
  4. constructor(props){
  5. super(props);
  6.  
  7. this.state={
  8. username:"",
  9. password:""
  10. }
  11.  
  12. this.tryLogin = this.tryLogin.bind(this);
  13. }
  14.  
  15. render(){
  16. return(
  17. <div>
  18. <div className="logo"></div>
  19. <div className="login-block">
  20. <form action="">
  21. <h1>Login</h1>
  22. <input type="text" value={this.state.username} placeholder="Username" id="username" onChange={(e)=>{this.setState({username:e.target.value})}}/>
  23. <input type="password" value={this.state.password} placeholder="Password" id="password" onChange={(e)=>{this.setState({password:e.target.value})}} />
  24. <button onClick={this.tryLogin}>Submit</button>
  25. </form>
  26. </div>
  27. </div>
  28. );
  29. }
  30.  
  31. tryLogin(e){
  32. e.preventDefault();
  33. if(this.state.username !== "" && this.state.password !== ""){
  34. fetch('http://dev.api.solution404.io/login/login', {
  35. method: 'POST',
  36. headers: {
  37. 'Accept': 'application/json',
  38. 'Content-Type': 'application/json',
  39. },
  40. body: JSON.stringify({
  41. username: this.state.username,
  42. password: this.state.password
  43. })
  44. }).then((res)=>{
  45. if(res.status === 200){
  46. res.json().then((resjson)=>{
  47. if(resjson === 'true'){
  48. localStorage.setItem("user", this.state.username);
  49. console.log(window.location.assign("/"))
  50. }
  51. });
  52. }
  53. })
  54. }
  55. }
  56. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement