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
131 lines (120 loc) · 3.66 KB
/
Copy pathauth-provider.tsx
File metadata and controls
131 lines (120 loc) · 3.66 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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
"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 { usePathname, 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 pathname = usePathname();
const isTvKiosk = pathname?.startsWith("/tv/view/") ?? false;
const isTvKioskRef = React.useRef(isTvKiosk);
isTvKioskRef.current = isTvKiosk;
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.`
);
setUser(null);
setIsAdmin(false);
// Kiosk must not sign out or redirect — a TV may share a browser
// with an admin session, and kiosk→dashboard must not bounce login.
if (!isTvKioskRef.current) {
await firebaseSignOut(auth);
router.push("/login?error=unauthorized");
}
}
} catch (error) {
console.error("Error checking admin status:", error);
setUser(null);
setIsAdmin(false);
if (!isTvKioskRef.current) {
await firebaseSignOut(auth);
router.push("/login?error=check_failed");
}
}
} else {
setUser(null);
setIsAdmin(false);
}
setLoading(false);
});
// Cleanup subscription on unmount
return () => unsubscribe();
}, [bypassAuth, router]);
return (
<AuthContext.Provider value={{ user, isAdmin, loading, signOut }}>
{children}
</AuthContext.Provider>
);
}
export const useAuth = () => useContext(AuthContext);