add login / logout logic for user
This commit is contained in:
33
frontend/src/services/auth/AuthProvider.tsx
Normal file
33
frontend/src/services/auth/AuthProvider.tsx
Normal file
@@ -0,0 +1,33 @@
|
||||
import { createContext, useContext } from "react";
|
||||
import { useCurrentUser } from "../api";
|
||||
import type { UserLogged } from "../resources/users";
|
||||
|
||||
export type Auth = {
|
||||
loggedUser: UserLogged | null;
|
||||
isLoading: boolean;
|
||||
}
|
||||
|
||||
const AuthContext = createContext<Auth | undefined>(undefined)
|
||||
|
||||
export function AuthProvider({ children }: {children: React.ReactNode}) {
|
||||
const {data: loggedUser, isLoading} = useCurrentUser();
|
||||
|
||||
const value: Auth = {
|
||||
loggedUser: loggedUser ?? null,
|
||||
isLoading,
|
||||
};
|
||||
|
||||
return (
|
||||
<AuthContext.Provider value={value}>
|
||||
{children}
|
||||
</AuthContext.Provider>
|
||||
)
|
||||
}
|
||||
|
||||
export function useAuth(): Auth {
|
||||
const context = useContext(AuthContext);
|
||||
if (!context) {
|
||||
throw new Error("useAuth must be used inside AuthProvider");
|
||||
}
|
||||
return context;
|
||||
}
|
||||
Reference in New Issue
Block a user