Guest User

Untitled

a guest
Mar 19th, 2019
36
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.33 KB | None | 0 0
  1. import React from "react";
  2. import { Redirect, withRouter } from "react-router-dom";
  3. import Swal from "sweetalert2";
  4.  
  5. class Login extends React.Component {
  6. state = {
  7. user: "",
  8. password: "",
  9. redirect: false
  10. };
  11.  
  12. handleChange = event => {
  13. const key = event.target.name;
  14. const value = event.target.value;
  15.  
  16. this.setState({
  17. [key]: value
  18. });
  19. };
  20.  
  21. handleSubmit = event => {
  22. event.preventDefault();
  23. fetch("http://localhost:80/correctsaler/src/backend/login.php", {
  24. method: "post",
  25. body: JSON.stringify({
  26. user: this.state.user,
  27. password: this.state.password
  28. })
  29. })
  30. .then(res => res.json())
  31. .then(data => {
  32. if (data !== "") {
  33. this.props.toggleAuth(data);
  34. this.props.history.replace("/");
  35. this.props.toggleUser({user: this.state.user})
  36. } else {
  37. Swal.fire("Wrong credentials! Please try again!");
  38. }
  39. })
  40. .catch(function(error) {
  41. console.log("Request failed", error);
  42. });
  43. };
  44. componentDidMount() {
  45. fetch("http://localhost:80/correctsaler/src/backend/login.php")
  46. .then(res => res.json())
  47. .then(row => {
  48. console.log("Request succeeded with JSON response", row);
  49. })
  50. .catch(function(error) {
  51. console.log("Request failed", error);
  52. });
  53. }
  54. render() {
  55. return (
  56. <section className="hero is-primary">
  57. <div className="hero-body">
  58. <form className="section" id="form" onSubmit={this.handleSubmit}>
  59. <label>
  60. <p>Username:</p>
  61. <input
  62. className="input"
  63. type="text"
  64. value={this.state.user}
  65. onChange={this.handleChange}
  66. name="user"
  67. />
  68. <p>Password:</p>
  69. <input
  70. className="input"
  71. type="password"
  72. value={this.state.password}
  73. onChange={this.handleChange}
  74. name="password"
  75. />
  76. </label>
  77. <br />
  78. <br />
  79. <input className="button is-primary" type="submit" value="Submit" />
  80. </form>
  81. </div>
  82. </section>
  83. );
  84. }
  85. }
  86.  
  87. export default withRouter(Login);
Add Comment
Please, Sign In to add comment