forked from BasedHardware/omi
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathauth.ts
More file actions
42 lines (36 loc) · 1.58 KB
/
Copy pathauth.ts
File metadata and controls
42 lines (36 loc) · 1.58 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
import { NextRequest, NextResponse } from 'next/server';
import { DEV_BYPASS_ENABLED, DEV_BYPASS_TOKEN, DEV_BYPASS_UID } from '@/lib/dev-auth';
import { verifyFirebaseToken, getDb } from '@/lib/firebase/admin';
/**
* Verify that the request comes from an authenticated admin user.
* Checks: (1) valid Firebase ID token in Authorization header,
* (2) user's UID exists in the adminData collection.
* Returns the decoded token on success, or a NextResponse error on failure.
*/
export async function verifyAdmin(request: NextRequest): Promise<
{ uid: string } | NextResponse
> {
const authorization = request.headers.get('Authorization');
if (DEV_BYPASS_ENABLED && authorization === `Bearer ${DEV_BYPASS_TOKEN}`) {
return { uid: DEV_BYPASS_UID };
}
if (!authorization || !authorization.startsWith('Bearer ')) {
return NextResponse.json({ error: 'Unauthorized: Missing or invalid token' }, { status: 401 });
}
const token = authorization.split('Bearer ')[1];
try {
const decodedToken = await verifyFirebaseToken(token);
if (!decodedToken) {
return NextResponse.json({ error: 'Unauthorized: Invalid token' }, { status: 401 });
}
const db = getDb();
const adminDoc = await db.collection('adminData').doc(decodedToken.uid).get();
if (!adminDoc.exists) {
return NextResponse.json({ error: 'Forbidden: Not an admin' }, { status: 403 });
}
return { uid: decodedToken.uid };
} catch (error) {
console.error('Error verifying admin:', error);
return NextResponse.json({ error: 'Internal server error during auth' }, { status: 500 });
}
}