first commit
This commit is contained in:
commit
5d6023a8af
52 files changed
+5830
No files matched your search
@@ -0,0 +1,103 @@
|
||||
"use client";
|
||||
|
||||
import {
|
||||
createContext,
|
||||
useContext,
|
||||
useState,
|
||||
useEffect,
|
||||
useCallback,
|
||||
ReactNode
|
||||
} from "react";
|
||||
|
||||
import { useRouter } from "next/navigation";
|
||||
|
||||
import {
|
||||
checkGoogleSession,
|
||||
handleGoogleSignout
|
||||
} from "../actions/GoogleAction";
|
||||
|
||||
interface User {
|
||||
id: string | null | undefined;
|
||||
email: string | null | undefined;
|
||||
name: string | null | undefined;
|
||||
}
|
||||
|
||||
interface AuthState {
|
||||
user: User;
|
||||
isAuthenticated: boolean;
|
||||
isLoading: boolean;
|
||||
}
|
||||
|
||||
interface AuthContextType extends AuthState {
|
||||
checkGoogle: () => void;
|
||||
logout: () => Promise<void>;
|
||||
}
|
||||
|
||||
const AuthContext = createContext<AuthContextType | undefined>(undefined);
|
||||
|
||||
export function AuthProvider({ children }: { children: ReactNode }) {
|
||||
const router = useRouter();
|
||||
|
||||
const user = {
|
||||
id: "",
|
||||
email: "",
|
||||
name: ""
|
||||
};
|
||||
|
||||
const [authState, setAuthState] = useState<AuthState>({
|
||||
user: user,
|
||||
isAuthenticated: false,
|
||||
isLoading: true
|
||||
});
|
||||
|
||||
const checkGoogle = async () => {
|
||||
const session = await checkGoogleSession();
|
||||
if (session) {
|
||||
const currentUser: User = {
|
||||
id: session.user?.id,
|
||||
name: session.user?.name,
|
||||
email: session.user?.email
|
||||
};
|
||||
setAuthState({
|
||||
user: currentUser,
|
||||
isAuthenticated: true,
|
||||
isLoading: false
|
||||
});
|
||||
}
|
||||
};
|
||||
|
||||
// Check auth status on mount
|
||||
useEffect(() => {
|
||||
checkGoogle();
|
||||
}, []);
|
||||
|
||||
const logout = useCallback(async () => {
|
||||
setAuthState({
|
||||
user: user,
|
||||
isAuthenticated: false,
|
||||
isLoading: false
|
||||
});
|
||||
|
||||
await handleGoogleSignout();
|
||||
}, []);
|
||||
|
||||
const value: AuthContextType = {
|
||||
...authState,
|
||||
checkGoogle,
|
||||
logout
|
||||
};
|
||||
|
||||
return <AuthContext.Provider value={value}>{children}</AuthContext.Provider>;
|
||||
}
|
||||
|
||||
/**
|
||||
* useAuth hook - uses shared AuthContext
|
||||
* All components using this hook share the same auth state
|
||||
*/
|
||||
export function useAuth() {
|
||||
const context = useContext(AuthContext);
|
||||
if (context === undefined) {
|
||||
throw new Error("useAuth must be used within an AuthProvider");
|
||||
}
|
||||
return context;
|
||||
}
|
||||
Reference in new issue
Block a user