forked from BasedHardware/omi
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathauth-provider.tsx
More file actions
103 lines (92 loc) · 3.23 KB
/
Copy pathauth-provider.tsx
File metadata and controls
103 lines (92 loc) · 3.23 KB
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
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
'use client';
import React, { createContext, useContext, useEffect, useState, ReactNode } from 'react';
import { User, onAuthStateChanged, signOut as firebaseSignOut } from 'firebase/auth';
import { doc, getDoc } from 'firebase/firestore';
import { getFirebaseAuth, getFirebaseDb } from '@/lib/firebase/client';
import { DEV_BYPASS_ENABLED, DEV_BYPASS_TOKEN, DEV_BYPASS_UID } from '@/lib/dev-auth';
import { useRouter } from 'next/navigation'; // Use next/navigation for App Router
interface AuthContextProps {
user: User | null;
isAdmin: boolean;
loading: boolean;
signOut: () => Promise<void>;
}
const AuthContext = createContext<AuthContextProps>({
user: null,
isAdmin: false,
loading: true,
signOut: async () => {},
});
function createBypassUser(): User {
return {
uid: DEV_BYPASS_UID,
getIdToken: async () => DEV_BYPASS_TOKEN,
} as User;
}
export function AuthProvider({ children }: { children: ReactNode }) {
const bypassAuth = DEV_BYPASS_ENABLED;
const [user, setUser] = useState<User | null>(bypassAuth ? createBypassUser() : null);
const [isAdmin, setIsAdmin] = useState<boolean>(bypassAuth);
const [loading, setLoading] = useState<boolean>(!bypassAuth);
const router = useRouter();
const signOut = async () => {
try {
await firebaseSignOut(getFirebaseAuth());
setUser(null);
setIsAdmin(false);
router.push('/login'); // Redirect to login after sign out
} catch (error) {
console.error('Error signing out:', error);
}
};
useEffect(() => {
if (bypassAuth) {
setUser(createBypassUser());
setIsAdmin(true);
setLoading(false);
return;
}
const auth = getFirebaseAuth();
const db = getFirebaseDb();
const unsubscribe = onAuthStateChanged(auth, async (currentUser) => {
setLoading(true);
if (currentUser) {
console.log('currentUser', currentUser);
// Check if user is an Omi admin by looking for their UID in the specific product path
const adminDocRef = doc(db, 'adminData', currentUser.uid);
try {
const adminDoc = await getDoc(adminDocRef);
if (adminDoc.exists()) {
setUser(currentUser);
setIsAdmin(true);
console.log(`Admin user ${currentUser.email} signed in.`);
} else {
console.warn(`User ${currentUser.email} is not an admin. Signing out.`);
await firebaseSignOut(auth);
setUser(null);
setIsAdmin(false);
router.push('/login?error=unauthorized'); // Redirect non-admin to login
}
} catch (error) {
console.error('Error checking admin status:', error);
await firebaseSignOut(auth);
setUser(null);
setIsAdmin(false);
router.push('/login?error=check_failed'); // Redirect on error
}
} else {
setUser(null);
setIsAdmin(false);
}
setLoading(false);
});
// Cleanup subscription on unmount
return () => unsubscribe();
}, [bypassAuth, router]); // Add router to dependency array
return (
<AuthContext.Provider value={{ user, isAdmin, loading, signOut }}>
{children}
</AuthContext.Provider>
);
}
export const useAuth = () => useContext(AuthContext);