-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmiddleware.ts
More file actions
29 lines (24 loc) · 828 Bytes
/
middleware.ts
File metadata and controls
29 lines (24 loc) · 828 Bytes
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
import NextAuth from 'next-auth';
import { NextRequest, NextResponse } from 'next/server';
import { authConfig } from '@/app/(auth)/auth.config';
import { extractUserInfoForAnalytics } from '@/lib/analytics/middleware';
export default NextAuth(authConfig).auth((req: NextRequest) => {
// Extract analytics data and add it to headers for server components
const userInfo = extractUserInfoForAnalytics(req);
const headers = new Headers(req.headers);
// Add analytics data as headers
Object.entries(userInfo).forEach(([key, value]) => {
if (value) {
headers.set(`x-analytics-${key}`, value.toString());
}
});
return NextResponse.next({
request: {
// New request headers
headers,
},
});
});
export const config = {
matcher: ['/chat', '/chat/:id', '/api/:path*', '/auth'],
};