|
| 1 | +import React, { useEffect, useState } from "react"; |
| 2 | + |
| 3 | +import { Link, useNavigate } from "react-router-dom"; |
| 4 | +import Swal from "sweetalert2"; |
| 5 | +import withReactContent from "sweetalert2-react-content"; |
| 6 | + |
| 7 | +import axios from "axios"; |
| 8 | + |
| 9 | +export default function Register() { |
| 10 | + const baseURL = "http://127.0.0.1:8000/api"; |
| 11 | + |
| 12 | + const navigate = useNavigate(); |
| 13 | + const MySwal = withReactContent(Swal); |
| 14 | + |
| 15 | + useEffect(() => { |
| 16 | + document.title = "Login"; |
| 17 | + if (localStorage.getItem("token")) { |
| 18 | + //redirect page dashboard |
| 19 | + navigate("/dashboard"); |
| 20 | + } |
| 21 | + }, []); |
| 22 | + |
| 23 | + const [formData, setFormData] = useState({ |
| 24 | + email: "", |
| 25 | + password: "", |
| 26 | + }); |
| 27 | + |
| 28 | + const initialFormData = { |
| 29 | + email: "", |
| 30 | + password: "", |
| 31 | + }; |
| 32 | + |
| 33 | + const [formErrors, setFormErrors] = useState({ |
| 34 | + email: "", |
| 35 | + password: "", |
| 36 | + }); |
| 37 | + |
| 38 | + const validateForm = () => { |
| 39 | + let errors = {}; |
| 40 | + let formIsValid = true; |
| 41 | + |
| 42 | + // Validate input description |
| 43 | + if (!formData.email) { |
| 44 | + formIsValid = false; |
| 45 | + errors.email = "Email is required"; |
| 46 | + } |
| 47 | + |
| 48 | + // Validate input description |
| 49 | + if (!formData.password) { |
| 50 | + formIsValid = false; |
| 51 | + errors.password = "Password is required"; |
| 52 | + } |
| 53 | + |
| 54 | + setFormErrors(errors); |
| 55 | + return formIsValid; |
| 56 | + }; |
| 57 | + |
| 58 | + const handleInputChange = (event) => { |
| 59 | + const { name, value } = event.target; |
| 60 | + setFormData({ ...formData, [name]: value }); |
| 61 | + }; |
| 62 | + |
| 63 | + const loginHandler = (e) => { |
| 64 | + e.preventDefault(); |
| 65 | + |
| 66 | + if (validateForm()) { |
| 67 | + axios |
| 68 | + .post(`${baseURL}/login`, formData, { |
| 69 | + headers: { |
| 70 | + "Content-Type": "application/json", |
| 71 | + }, |
| 72 | + }) |
| 73 | + .then((response) => { |
| 74 | + if (response.status === 200) { |
| 75 | + localStorage.setItem("token", response.data.token); |
| 76 | + MySwal.fire({ |
| 77 | + title: "Success!", |
| 78 | + text: "Login successfully", |
| 79 | + icon: "success", |
| 80 | + timer: 1500, |
| 81 | + }).then(() => { |
| 82 | + navigate("/dashboard"); |
| 83 | + }); |
| 84 | + } else { |
| 85 | + throw new Error("Network response was not ok"); |
| 86 | + } |
| 87 | + }) |
| 88 | + .catch((error) => { |
| 89 | + console.error("Error:", error); |
| 90 | + }); |
| 91 | + } |
| 92 | + }; |
| 93 | + |
| 94 | + return ( |
| 95 | + <div className="row"> |
| 96 | + <div className="col-12 col-sm-8 offset-sm-2 col-md-6 offset-md-3 col-lg-6 offset-lg-3 col-xl-4 offset-xl-4 tw-mt-10"> |
| 97 | + <div className="card card-primary"> |
| 98 | + <div className="card-header"> |
| 99 | + <h4>Login</h4> |
| 100 | + </div> |
| 101 | + |
| 102 | + <div className="card-body"> |
| 103 | + <form onSubmit={loginHandler}> |
| 104 | + <div className="form-group"> |
| 105 | + <label htmlFor="email">Email</label> |
| 106 | + <input |
| 107 | + type="email" |
| 108 | + name="email" |
| 109 | + id="email" |
| 110 | + className={`form-control ${ |
| 111 | + formErrors.email ? "is-invalid" : "" |
| 112 | + }`} |
| 113 | + value={formData.email || ""} |
| 114 | + onChange={handleInputChange} |
| 115 | + /> |
| 116 | + {formErrors.email && ( |
| 117 | + <div className="invalid-feedback"> |
| 118 | + {formErrors.email} |
| 119 | + </div> |
| 120 | + )} |
| 121 | + </div> |
| 122 | + |
| 123 | + <div className="form-group"> |
| 124 | + <label htmlFor="password">Password</label> |
| 125 | + <input |
| 126 | + type="password" |
| 127 | + name="password" |
| 128 | + id="password" |
| 129 | + className={`form-control ${ |
| 130 | + formErrors.password ? "is-invalid" : "" |
| 131 | + }`} |
| 132 | + value={formData.password || ""} |
| 133 | + onChange={handleInputChange} |
| 134 | + /> |
| 135 | + {formErrors.password && ( |
| 136 | + <div className="invalid-feedback"> |
| 137 | + {formErrors.password} |
| 138 | + </div> |
| 139 | + )} |
| 140 | + </div> |
| 141 | + <div className="form-group"> |
| 142 | + <button |
| 143 | + type="submit" |
| 144 | + className="btn btn-lg btn-block tw-bg-blue-500" |
| 145 | + > |
| 146 | + Login |
| 147 | + </button> |
| 148 | + </div> |
| 149 | + </form> |
| 150 | + </div> |
| 151 | + </div> |
| 152 | + <div className="mt-5 text-muted text-center"> |
| 153 | + Don't have an account?{" "} |
| 154 | + <Link to="/register">Create One</Link> |
| 155 | + </div> |
| 156 | + <div className="simple-footer"> |
| 157 | + Copyright © Stisla 2023 |
| 158 | + </div> |
| 159 | + </div> |
| 160 | + </div> |
| 161 | + ); |
| 162 | +} |
0 commit comments