Changeset 0f5aa27 for frontend/src/Components/Hooks/User
- Timestamp:
- 02/04/24 16:57:49 (10 months ago)
- Branches:
- master
- Children:
- efaa053
- Parents:
- 07f4e8b
- Location:
- frontend/src/Components/Hooks/User
- Files:
-
- 2 edited
Legend:
- Unmodified
- Added
- Removed
-
frontend/src/Components/Hooks/User/useCreateUser.js
r07f4e8b r0f5aa27 10 10 .post(`/register`, user) 11 11 .then((res) => { 12 navigator("/login")12 window.location.href = "http://localhost:8080/login" 13 13 }) 14 14 .catch((err) => { -
frontend/src/Components/Hooks/User/useLogin.js
r07f4e8b r0f5aa27 1 import React from "react"; 2 1 import { useState } from "react"; 3 2 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"; 3 import {useAuth} from "../../Context/AuthContext"; // Assuming you have axios configured 7 4 8 5 const useLogin = () => { 6 const [loading, setLoading] = useState(false); 7 const [error, setError] = useState(null); 8 const Auth = useAuth(); 9 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; 10 const handleLoginCallback = async () => { 11 setLoading(true); 43 12 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 } 13 try { 14 const response = await axios.get("http://localhost:8080/principal"); 15 16 // Extract the needed attributes from the response 17 const { id, role, username } = response.data; 18 19 Auth.userLogin({userId: id, username: username, role: role}) 20 21 } catch (err) { 22 setError(err.message); 23 } finally { 24 setLoading(false); 25 } 26 }; 57 27 58 28 return { 59 login 29 loading, 30 error, 31 handleLoginCallback, 60 32 }; 61 62 } 33 }; 63 34 64 35 export default useLogin;
Note:
See TracChangeset
for help on using the changeset viewer.