[e6c2521] | 1 | import React from "react";
|
---|
| 2 |
|
---|
| 3 | import axios from "../../../axios.js";
|
---|
| 4 | import { Navigate, useAsyncValue, useNavigate } from "react-router-dom";
|
---|
| 5 | import LoginForm from "../../Login/LoginForm.js";
|
---|
| 6 | import {useAuth} from "../../Context/AuthContext";
|
---|
| 7 |
|
---|
| 8 | const 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 |
|
---|
| 64 | export default useLogin; |
---|