Ignore:
Timestamp:
02/04/24 16:57:49 (5 months ago)
Author:
darsov2 <62809499+darsov2@…>
Branches:
master
Children:
efaa053
Parents:
07f4e8b
Message:

ouath, mailing impl

File:
1 edited

Legend:

Unmodified
Added
Removed
  • frontend/src/Components/Hooks/User/useLogin.js

    r07f4e8b r0f5aa27  
    1 import React from "react";
    2 
     1import { useState } from "react";
    32import 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";
     3import {useAuth} from "../../Context/AuthContext"; // Assuming you have axios configured
    74
    85const useLogin = () => {
     6        const [loading, setLoading] = useState(false);
     7        const [error, setError] = useState(null);
     8        const Auth = useAuth();
    99
    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);
    4312
    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        };
    5727
    5828        return {
    59                 login
     29                loading,
     30                error,
     31                handleLoginCallback,
    6032        };
    61 
    62 }
     33};
    6334
    6435export default useLogin;
Note: See TracChangeset for help on using the changeset viewer.