Create auth context
This commit is contained in:
1 parent
229cc27be2
commit
9937e0e2b9
2 files changed
+38
No files matched your search
@@ -0,0 +1,3 @@
|
|||||||
|
import { createContext } from "react";
|
||||||
|
|
||||||
|
export const AuthContext = createContext(null);
|
||||||
@@ -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>
|
||||||
|
);
|
||||||
|
}
|
||||||
Reference in new issue
Block a user