source: src/lib/auth-middleware.ts@ 057453c

main
Last change on this file since 057453c was 057453c, checked in by Naum Shapkarovski <naumshapkarovski@…>, 5 weeks ago

feat: implement employees

  • Property mode set to 100644
File size: 1.1 KB
Line 
1import { NextRequest, NextResponse } from 'next/server';
2import { auth } from 'src/lib/firebase-admin';
3
4export interface AuthenticatedRequest extends NextRequest {
5 userId: string;
6 tenantId: string;
7}
8
9export async function authenticateRequest(
10 request: NextRequest
11): Promise<{ userId: string; tenantId: string } | NextResponse> {
12 // Get the authorization header
13 const authHeader = request.headers.get('Authorization');
14 if (!authHeader?.startsWith('Bearer ')) {
15 return NextResponse.json({ error: 'Unauthorized' }, { status: 401 });
16 }
17
18 // Extract the token
19 const token = authHeader.split('Bearer ')[1];
20
21 try {
22 // Verify the token
23 const decodedToken = await auth.verifyIdToken(token);
24 const userId = decodedToken.uid;
25 const tenantId = decodedToken.customClaims?.tenantId || 'cm7bwtjy80000pb0m5qenk8am';
26
27 if (!userId || !tenantId) {
28 return NextResponse.json({ error: 'Unauthorized' }, { status: 401 });
29 }
30
31 return { userId, tenantId };
32 } catch (error) {
33 return NextResponse.json({ error: 'Unauthorized' }, { status: 401 });
34 }
35}
Note: See TracBrowser for help on using the repository browser.