Files
2024_DEV_BUT3/src/contexts/auth-context.jsx

103 lines
2.4 KiB
React
Raw Normal View History

2024-03-27 01:37:35 +01:00
import React, { useEffect, useState } from "react";
import {
2024-03-31 23:45:25 +02:00
createUser as registerApi,
isLoggedIn,
login as loginApi,
logout as logoutApi,
2024-03-27 01:37:35 +01:00
} from "../api";
import { useQuery } from "../hooks";
import { useLocation, useNavigate } from "react-router-dom";
const initState = {
2024-03-31 23:45:25 +02:00
user: undefined,
2024-03-27 01:37:35 +01:00
};
export const AuthenticationContext = React.createContext({
2024-03-31 23:45:25 +02:00
...initState,
login: () => {},
register: () => {},
logout: () => {},
2024-03-27 01:37:35 +01:00
});
export const AuthenticationProvider = ({ children }) => {
2024-03-31 23:45:25 +02:00
const navigate = useNavigate();
const location = useLocation();
const query = useQuery();
const [authState, setAuthState] = useState(initState);
const [isLoading, setIsLoading] = useState(true);
2024-03-27 01:37:35 +01:00
2024-03-31 23:45:25 +02:00
const redirect = () => {
navigate(query.get("redirect_uri") || "/");
};
2024-03-27 01:37:35 +01:00
2024-03-31 23:45:25 +02:00
const login = async (email, password) => {
2024-04-01 12:55:40 +02:00
try {
const user = await loginApi(email, password);
setAuthState({ user });
redirect();
} catch (error) {
console.error("Erreur lors de la connexion :", error);
setAuthState({ user: undefined });
}
2024-03-31 23:45:25 +02:00
};
2024-03-27 01:37:35 +01:00
2024-03-31 23:45:25 +02:00
const register = async (username, password, confirmation) => {
2024-04-01 12:55:40 +02:00
try {
const user = await registerApi(username, password, confirmation);
2024-03-31 23:45:25 +02:00
setAuthState({ user });
redirect();
2024-04-01 12:55:40 +02:00
} catch (error) {
console.error("Erreur lors de l'inscription :", error);
setAuthState({ user: undefined });
}
2024-03-31 23:45:25 +02:00
};
2024-03-27 01:37:35 +01:00
2024-03-31 23:45:25 +02:00
const logout = () => {
logoutApi().then(() => {
setAuthState(initState);
navigate(`/login`);
});
};
2024-03-27 01:37:35 +01:00
2024-03-31 23:45:25 +02:00
useEffect(() => {
isLoggedIn()
.then((user) => {
if (user === "Unauthorized") throw new Error("Unauthorized");
setAuthState({ user });
if (location.pathname === "/login") {
redirect();
}
})
.catch(() => {
setAuthState({ user: undefined });
2024-03-27 01:37:35 +01:00
2024-03-31 23:45:25 +02:00
if (!location.pathname.match(/^(\/|\/login|\/register)$/)) {
navigate(`/login?redirect_uri=${encodeURI(location.pathname)}`);
2024-03-27 01:37:35 +01:00
}
2024-03-31 23:45:25 +02:00
})
.finally(() => {
setIsLoading(false);
});
}, []);
2024-03-27 01:37:35 +01:00
2024-03-31 23:45:25 +02:00
useEffect(() => {
if (location.pathname === "/logout") {
logout();
2024-03-27 01:37:35 +01:00
}
2024-03-31 23:45:25 +02:00
}, [location]);
if (isLoading) {
return <p>Loading...</p>;
}
2024-03-27 01:37:35 +01:00
2024-03-31 23:45:25 +02:00
return (
<AuthenticationContext.Provider
value={{ ...authState, login, logout, register }}
>
{children}
</AuthenticationContext.Provider>
);
2024-03-27 01:37:35 +01:00
};