source: src/lib/auth-middleware.ts@ 32e9876

main
Last change on this file since 32e9876 was 87c9f1e, checked in by Naum Shapkarovski <naumshapkarovski@…>, 6 weeks ago

update the seed script. update the prisma schema, use mapping

  • Property mode set to 100644
File size: 1.1 KB
RevLine 
[5d6f37a]1import { NextRequest, NextResponse } from 'next/server';
2import { auth } from 'src/lib/firebase-admin';
3
4export interface AuthenticatedRequest extends NextRequest {
5 userId: string;
[057453c]6 tenantId: string;
[5d6f37a]7}
8
9export async function authenticateRequest(
10 request: NextRequest
[057453c]11): Promise<{ userId: string; tenantId: string } | NextResponse> {
[5d6f37a]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
[87c9f1e]23 const decodedToken = await auth.verifyIdToken(token);
24 const userId = decodedToken.uid;
[5d6f37a]25
[87c9f1e]26 const tenantId = decodedToken.customClaims?.tenantId || 'cm7lxc3p00000pb7kmdrxsfod';
[5d6f37a]27
[87c9f1e]28 if (!userId || !tenantId) {
29 return NextResponse.json({ error: 'Unauthorized' }, { status: 401 });
30 }
31
32 return { userId, tenantId: 'cm7lxc3p00000pb7kmdrxsfod' };
[5d6f37a]33 } catch (error) {
[87c9f1e]34 console.error('Error verifying token:', error);
[5d6f37a]35 return NextResponse.json({ error: 'Unauthorized' }, { status: 401 });
36 }
37}
Note: See TracBrowser for help on using the repository browser.