From 9937e0e2b9e0b55410b45e3c2ca1ddc48e831ab2 Mon Sep 17 00:00:00 2001 From: T'JAMPENS QUENTIN p2406187 Date: Sat, 15 Nov 2025 23:27:36 +0100 Subject: [PATCH] Create auth context --- src/contexts/auth/AuthContext.js | 3 +++ src/contexts/auth/authProvider.jsx | 35 ++++++++++++++++++++++++++++++ 2 files changed, 38 insertions(+) create mode 100644 src/contexts/auth/AuthContext.js create mode 100644 src/contexts/auth/authProvider.jsx diff --git a/src/contexts/auth/AuthContext.js b/src/contexts/auth/AuthContext.js new file mode 100644 index 0000000..885fd0e --- /dev/null +++ b/src/contexts/auth/AuthContext.js @@ -0,0 +1,3 @@ +import { createContext } from "react"; + +export const AuthContext = createContext(null); diff --git a/src/contexts/auth/authProvider.jsx b/src/contexts/auth/authProvider.jsx new file mode 100644 index 0000000..a46d3c3 --- /dev/null +++ b/src/contexts/auth/authProvider.jsx @@ -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 ( + + {children} + + ); +}