cre-sync/lib/hooks/useAuth.tsx
BusyBee3333 4e6467ffb0 Add CRESync CRM application with Setup page
- Build complete Next.js CRM for commercial real estate
- Add authentication with JWT sessions and role-based access
- Add GoHighLevel API integration for contacts, conversations, opportunities
- Add AI-powered Control Center with tool calling
- Add Setup page with onboarding checklist (/setup)
- Add sidebar navigation with Setup menu item
- Fix type errors in onboarding API, GHL services, and control center tools
- Add Prisma schema with SQLite for local development
- Add UI components with clay morphism design system

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
2026-01-14 17:30:55 -05:00

73 lines
1.8 KiB
TypeScript

'use client';
import { useState, useEffect, createContext, useContext } from 'react';
import { api } from '@/lib/api/client';
export interface User {
id: string;
email: string;
firstName: string;
lastName: string;
role: string;
brokerage?: string;
ghlLocationId?: string;
}
export interface AuthContextType {
user: User | null;
loading: boolean;
isLoading: boolean; // Alias for loading
login: (email: string, password: string) => Promise<void>;
signup: (data: { email: string; password: string; firstName: string; lastName: string }) => Promise<void>;
logout: () => Promise<void>;
refresh: () => Promise<void>;
}
const AuthContext = createContext<AuthContextType | null>(null);
export function AuthProvider({ children }: { children: React.ReactNode }) {
const [user, setUser] = useState<User | null>(null);
const [loading, setLoading] = useState(true);
const refresh = async () => {
try {
const { user } = await api.auth.me();
setUser(user);
} catch {
setUser(null);
} finally {
setLoading(false);
}
};
useEffect(() => {
refresh();
}, []);
const login = async (email: string, password: string) => {
const { user } = await api.auth.login(email, password);
setUser(user);
};
const signup = async (data: { email: string; password: string; firstName: string; lastName: string }) => {
const { user } = await api.auth.signup(data);
setUser(user);
};
const logout = async () => {
await api.auth.logout();
setUser(null);
};
return (
<AuthContext.Provider value={{ user, loading, isLoading: loading, login, signup, logout, refresh }}>
{children}
</AuthContext.Provider>
);
}
export function useAuth() {
const context = useContext(AuthContext);
if (!context) throw new Error('useAuth must be used within AuthProvider');
return context;
}