-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAuthContext.tsx
More file actions
31 lines (26 loc) · 848 Bytes
/
AuthContext.tsx
File metadata and controls
31 lines (26 loc) · 848 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
import { createContext, useContext } from "react";
import { type User } from "@supabase/supabase-js";
import type { Profile } from "../types/db";
import { useAuthUser } from "../hooks/useAuthUser";
type AuthContextType = {
user: User | null;
profile: Profile | null;
isLoading: boolean;
isAuthenticated: boolean;
logout: () => void;
refetchProfile: () => Promise<void>;
};
export const AuthUserContext = createContext<AuthContextType | undefined>(
undefined
);
export function useAuth() {
const ctx = useContext(AuthUserContext);
if (!ctx) throw new Error("useAuth must be used inside AuthUserContext");
return ctx;
}
export function AuthProvider({ children }: { children: React.ReactNode }) {
const auth = useAuthUser();
return (
<AuthUserContext.Provider value={auth}>{children}</AuthUserContext.Provider>
);
}