Added update function to reload user information without re-logging in

This commit is contained in:
2026-08-29 18:07:04 +02:00
parent 643e865c88
commit b13b63c3cb

View File

@@ -20,6 +20,7 @@ type AuthContextType = {
loading: boolean; loading: boolean;
login: () => void; login: () => void;
logout: () => Promise<void>; logout: () => Promise<void>;
update: () => Promise<boolean>;
}; };
const AuthContext = createContext<AuthContextType>({ const AuthContext = createContext<AuthContextType>({
@@ -27,6 +28,9 @@ const AuthContext = createContext<AuthContextType>({
loading: false, loading: false,
login: () => {}, login: () => {},
logout: async () => {}, logout: async () => {},
update: async () => {
return false;
},
}); });
export function AuthProvider({ children }: PropsWithChildren) { export function AuthProvider({ children }: PropsWithChildren) {
@@ -70,24 +74,7 @@ export function AuthProvider({ children }: PropsWithChildren) {
}); });
} }
let data = await fetch(`${import.meta.env.VITE_BASE_API_URL}/me`, { if (await update()) {
credentials: "include",
})
.then((r) => r.json())
.catch((e) => {
notify(
{
type: "danger",
loading: false,
title: "An error occured.",
message: `An error occured: ${e}`,
},
10000,
);
});
if (data !== undefined) {
setUser(data);
notify( notify(
{ {
type: "success", type: "success",
@@ -98,7 +85,6 @@ export function AuthProvider({ children }: PropsWithChildren) {
2000, 2000,
); );
} }
setLoading(false); setLoading(false);
}; };
@@ -140,8 +126,32 @@ export function AuthProvider({ children }: PropsWithChildren) {
}); });
}; };
const update = async () => {
let data = await fetch(`${import.meta.env.VITE_BASE_API_URL}/me`, {
credentials: "include",
})
.then((r) => r.json())
.catch((e) => {
notify(
{
type: "danger",
loading: false,
title: "An error occured.",
message: `An error occured: ${e}`,
},
10000,
);
});
if (data !== undefined) {
setUser(data);
return true;
}
return false;
};
return ( return (
<AuthContext.Provider value={{ user, loading, login, logout }}> <AuthContext.Provider value={{ user, loading, login, logout, update }}>
{children} {children}
</AuthContext.Provider> </AuthContext.Provider>
); );