source: frontend/src/Components/Hooks/User/useLogin.js@ ac19a0c

Last change on this file since ac19a0c was ac19a0c, checked in by darsov2 <62809499+darsov2@…>, 6 months ago

authContext impl, admin panel impl, search bar fixes, reservations listings impl

  • Property mode set to 100644
File size: 1.7 KB
Line 
1import React from "react";
2
3import axios from "../../../axios.js";
4import { Navigate, useAsyncValue, useNavigate } from "react-router-dom";
5import LoginForm from "../../Login/LoginForm.js";
6import {useAuth} from "../../Context/AuthContext";
7
8const useLogin = () => {
9
10 const navigator = useNavigate()
11 const Auth = useAuth();
12 //const history = useNavigate();
13 const login = async (loginData) => {
14 console.log({loginData})
15 console.log(loginData)
16 await axios
17 .post(`/api/login`, {
18 username: loginData.email,
19 password: loginData.password
20 }, {
21 headers: {
22 "Content-Type": "application/x-www-form-urlencoded"
23 }
24 })
25 .then((res) => {
26 console.log("RES LOGIN")
27 console.log(res)
28 if(res.status === 200)
29 {
30 const user = {
31 sessionId: res.data.auth.details.sessionId,
32 userId: res.data.auth.principal.userID,
33 username: res.data.auth.principal.username,
34 role: res.data.auth.principal.role,
35 name: res.data.auth.principal.name,
36 surname: res.data.auth.principal.surname,
37 }
38 Auth.userLogin(user);
39 console.log(user)
40 }
41 const sessionId = res.data.auth.details.sessionId;
42 const userId = res.data.auth.principal.userID;
43
44 localStorage.setItem("sessionId", sessionId);
45 localStorage.setItem("userId", userId);
46 if(sessionId === null)
47 {
48 }
49 navigator("/home")
50 })
51 .catch((err) => {
52 console.log(err);
53 })
54 .finally(() => {
55 });
56 }
57
58 return {
59 login
60 };
61
62}
63
64export default useLogin;
Note: See TracBrowser for help on using the repository browser.