|
| 1 | +"use client"; |
| 2 | + |
| 3 | +import { createContext, useState, ReactNode, useEffect } from "react"; |
| 4 | +import { useRouter } from "next/navigation"; |
| 5 | +import { fetchApi } from "@/lib/client/fetch"; |
| 6 | + |
| 7 | +interface Org { |
| 8 | + id: number; |
| 9 | + name: string; |
| 10 | + nameId: string; |
| 11 | + role: string; |
| 12 | +} |
| 13 | + |
| 14 | +interface User { |
| 15 | + _id: number; |
| 16 | + email: string; |
| 17 | + name: string; |
| 18 | + orgs: Org[]; |
| 19 | +} |
| 20 | + |
| 21 | +interface AuthContextType { |
| 22 | + user: User | null; |
| 23 | + login: (email: string, password: string) => Promise<User>; |
| 24 | + signup: (email: string, password: string, fullName: string) => Promise<User>; |
| 25 | + logout: () => Promise<void>; |
| 26 | + isAuthenticated: boolean; |
| 27 | + setIsAuthenticated: (status: boolean) => void; |
| 28 | +} |
| 29 | + |
| 30 | +const defaultContext: AuthContextType = { |
| 31 | + user: null, |
| 32 | + login: async () => Promise.reject("Not implemented"), |
| 33 | + signup: async () => Promise.reject("Not implemented"), |
| 34 | + logout: async () => Promise.reject("Not implemented"), |
| 35 | + isAuthenticated: false, |
| 36 | + setIsAuthenticated: () => {}, |
| 37 | +}; |
| 38 | + |
| 39 | +export const AuthContext = createContext<AuthContextType>(defaultContext); |
| 40 | + |
| 41 | +export const AuthProvider: React.FC<{ children: ReactNode }> = ({ |
| 42 | + children, |
| 43 | +}) => { |
| 44 | + const [user, setUser] = useState<User | null>(null); |
| 45 | + const [isAuthenticated, setIsAuthenticated] = useState(false); |
| 46 | + const router = useRouter(); |
| 47 | + |
| 48 | + useEffect(() => { |
| 49 | + const fetchUser = async () => { |
| 50 | + try { |
| 51 | + const data = await fetchApi<User>("/auth/me"); |
| 52 | + if (data.email) { |
| 53 | + setIsAuthenticated(true); |
| 54 | + setUser(data); |
| 55 | + } |
| 56 | + } catch (error) { |
| 57 | + console.error("User not authorized"); |
| 58 | + } |
| 59 | + }; |
| 60 | + |
| 61 | + fetchUser(); |
| 62 | + }, []); |
| 63 | + |
| 64 | + const login = async (email: string, password: string): Promise<User> => { |
| 65 | + try { |
| 66 | + const data = await fetchApi<User>("/auth/login", { |
| 67 | + method: "POST", |
| 68 | + body: JSON.stringify({ email, password }), |
| 69 | + }); |
| 70 | + |
| 71 | + if (!data.email) { |
| 72 | + throw new Error("Login failed: Invalid response"); |
| 73 | + } |
| 74 | + |
| 75 | + setUser(data); |
| 76 | + setIsAuthenticated(true); |
| 77 | + router.push("/admin"); |
| 78 | + return data; |
| 79 | + } catch (error) { |
| 80 | + console.error("Login failed:", error); |
| 81 | + throw new Error("Invalid credentials"); |
| 82 | + } |
| 83 | + }; |
| 84 | + |
| 85 | + const register = async ( |
| 86 | + email: string, |
| 87 | + password: string, |
| 88 | + fullName: string, |
| 89 | + ): Promise<User> => { |
| 90 | + try { |
| 91 | + const data = await fetchApi<User>("/auth/register", { |
| 92 | + method: "POST", |
| 93 | + body: JSON.stringify({ email, password, fullName }), |
| 94 | + }); |
| 95 | + |
| 96 | + setUser(data); |
| 97 | + setIsAuthenticated(true); |
| 98 | + router.push("/admin"); |
| 99 | + return data; |
| 100 | + } catch (error) { |
| 101 | + console.error("Signup failed:", error); |
| 102 | + throw new Error("Registration failed"); |
| 103 | + } |
| 104 | + }; |
| 105 | + |
| 106 | + const logout = async (): Promise<void> => { |
| 107 | + try { |
| 108 | + await fetchApi("/auth/logout", { method: "DELETE" }); |
| 109 | + setUser(null); |
| 110 | + setIsAuthenticated(false); |
| 111 | + router.push("/auth/login"); |
| 112 | + } catch (error) { |
| 113 | + console.error("Logout failed:", error); |
| 114 | + throw error; |
| 115 | + } |
| 116 | + }; |
| 117 | + |
| 118 | + return ( |
| 119 | + <AuthContext.Provider |
| 120 | + value={{ |
| 121 | + user, |
| 122 | + login, |
| 123 | + signup: register, |
| 124 | + logout, |
| 125 | + isAuthenticated, |
| 126 | + setIsAuthenticated, |
| 127 | + }} |
| 128 | + > |
| 129 | + {children} |
| 130 | + </AuthContext.Provider> |
| 131 | + ); |
| 132 | +}; |
0 commit comments