forked from enBloc-org/kindly
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmiddleware.ts
43 lines (39 loc) · 1009 Bytes
/
middleware.ts
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
import { NextResponse, NextRequest } from 'next/server';
import { createClient } from '@/supabase/utils/middleware';
export async function middleware(request: NextRequest) {
try {
const { supabase } = createClient(request);
const {
data: { user },
} = await supabase.auth.getUser();
if (user) {
const newHeaders = new Headers(request.headers);
newHeaders.set('k-active-user', user!.id);
return NextResponse.next({
request: {
headers: newHeaders,
},
});
}
return NextResponse.redirect(
new URL('/login?message=Please login to use this feature', request.url)
);
} catch (error) {
console.error(error);
return NextResponse.redirect(
new URL(
'/login?message=Something has gone wrong. Please try again later.',
request.url
)
);
}
}
export const config = {
matcher: [
'/conversations',
'/item/:id*',
'/profile',
'/add-item',
'/delete-account',
],
};