|
|
@ -6,6 +6,7 @@ import { |
|
|
|
onAuthStateChanged |
|
|
|
} from 'firebase/auth'; |
|
|
|
import { auth, googleProvider } from '../lib/firebase'; |
|
|
|
import { useRouter } from 'next/router'; |
|
|
|
// import { supabase } from '../lib/supabase';
|
|
|
|
|
|
|
|
interface AuthContextType { |
|
|
@ -30,7 +31,7 @@ export const AuthProvider: React.FC<{ children: React.ReactNode }> = ({ children |
|
|
|
const [currentUser, setCurrentUser] = useState<User | null>(null); |
|
|
|
const [loading, setLoading] = useState(true); |
|
|
|
const [userProfile, setUserProfile] = useState<any | null>(null); |
|
|
|
|
|
|
|
const router = useRouter(); |
|
|
|
// Function to sync Firebase user with Supabase
|
|
|
|
const syncUserWithDatabase = async (user: User) => { |
|
|
|
if (!user) return null; |
|
|
@ -72,8 +73,12 @@ export const AuthProvider: React.FC<{ children: React.ReactNode }> = ({ children |
|
|
|
const signInWithGoogle = async () => { |
|
|
|
try { |
|
|
|
const result = await signInWithPopup(auth, googleProvider); |
|
|
|
const profile = await syncUserWithDatabase(result.user); |
|
|
|
setUserProfile(profile); |
|
|
|
const profile = result.user; |
|
|
|
if (profile?.email?.includes("nivesh.com")) { |
|
|
|
setUserProfile(profile); |
|
|
|
router.push('/dashboard'); |
|
|
|
} |
|
|
|
|
|
|
|
} catch (error) { |
|
|
|
console.error('Error signing in with Google:', error); |
|
|
|
} |
|
|
@ -93,10 +98,8 @@ export const AuthProvider: React.FC<{ children: React.ReactNode }> = ({ children |
|
|
|
useEffect(() => { |
|
|
|
const unsubscribe = onAuthStateChanged(auth, async (user) => { |
|
|
|
setCurrentUser(user); |
|
|
|
|
|
|
|
if (user) { |
|
|
|
const profile = await syncUserWithDatabase(user); |
|
|
|
setUserProfile(profile); |
|
|
|
if (user?.email?.includes("nivesh")) { |
|
|
|
setUserProfile(user); |
|
|
|
} else { |
|
|
|
setUserProfile(null); |
|
|
|
} |
|
|
|