source: RebuMKReact/src/Components/Login/login.js

Last change on this file was 364f27d, checked in by MetodiMladenovski <meto.18@…>, 14 months ago

added projects and db scripts

  • Property mode set to 100644
File size: 3.2 KB
Line 
1import React from 'react';
2import { useNavigate } from 'react-router-dom';
3import RebuMKService from "../../repository/rebuRepository";
4import CenteredContainer from '../UtilComponents/CenteredContainer';
5import '../UtilComponents/App.css'
6
7const Login = (props) => {
8
9 const navigate = useNavigate();
10 const [formData, updateFormData] = React.useState({
11 username: "",
12 password: ""
13 })
14
15 const handleChange = (e) => {
16 updateFormData({
17 ...formData,
18 [e.target.name]: e.target.value.trim()
19 })
20 }
21
22 const onFormSubmit = (e) => {
23 e.preventDefault();
24 RebuMKService.login(formData.username, formData.password).then(resp => {
25 if(resp.data.typeOfLoggedUser === "Driver"){
26 localStorage.setItem("driverId", resp.data.idOfLoggedUser)
27 props.onLogin();
28 props.onLoadRequests(resp.data.idOfLoggedUser)
29 navigate("/home");
30 } else if(resp.data.typeOfLoggedUser === "Passenger"){
31 localStorage.setItem("passengerId", resp.data.idOfLoggedUser)
32 props.onLogin();
33 navigate("/home");
34 } else if(resp.data.typeOfLoggedUser === "Admin"){
35 localStorage.setItem("adminId", resp.data.idOfLoggedUser)
36 props.onLogin();
37 navigate("/home");
38 }
39 else {
40 alert("Login unsucessfull, wrong password or email, try again.")
41 }
42 })
43
44 }
45
46 return (
47 <CenteredContainer>
48 <h3 style={{textAlign: "center", color: "darkcyan"}}>RebuMK</h3>
49 <h5 style={{textAlign: "center", color: "darkcyan"}}>Login to your account</h5>
50 <hr></hr>
51 <form onSubmit={onFormSubmit}>
52 <div className="form-group">
53 <label htmlFor="username">Username</label>
54 <input type="text"
55 className="form-control"
56 name="username"
57 id="username"
58 required
59 placeholder="Enter username"
60 onChange={handleChange}
61 />
62 </div>
63 <br></br>
64 <div className="form-group">
65 <label htmlFor="price">Password</label>
66 <input type="password"
67 className="form-control"
68 name="password"
69 id="password"
70 placeholder="Enter password"
71 required
72 onChange={handleChange}
73 />
74 </div>
75 <br></br>
76 <div className="d-grid gap-2 col-6 mx-auto">
77 <button id="submit" type="submit" className="myButton btn btn-primary" style={{backgroundColor: "darkcyan", borderColor: "black"}}>Login</button>
78 </div>
79 </form>
80 </CenteredContainer>
81 )
82}
83
84export default Login;
Note: See TracBrowser for help on using the repository browser.