Create auth context

This commit is contained in:
T'JAMPENS QUENTIN p2406187
2025-11-15 23:27:36 +01:00
parent 229cc27be2
commit 9937e0e2b9
2 changed files with 38 additions and 0 deletions
+3
View File
@@ -0,0 +1,3 @@
import { createContext } from "react";
export const AuthContext = createContext(null);
+35
View File
@@ -0,0 +1,35 @@
import { useState, useEffect } from "react";
import loginFunction from "../../utils/login.js";
import getUser from "../../utils/getUser.js";
import { AuthContext } from "./AuthContext";
export function AuthProvider({ children }) {
const [user, setUser] = useState(null);
const [loading, setLoading] = useState(true);
useEffect(() => {
(async () => {
const u = await getUser();
setUser(u);
setLoading(false);
})();
}, []);
const login = async (email, password) => {
await loginFunction(email, password);
const u = await getUser();
setUser(u);
};
const logout = async () => {
// TODO: Appeller fonction logout
setUser(null);
};
return (
<AuthContext.Provider value={{ user, loading, login, logout }}>
{children}
</AuthContext.Provider>
);
}