-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmiddleware.ts
More file actions
47 lines (38 loc) · 1.3 KB
/
middleware.ts
File metadata and controls
47 lines (38 loc) · 1.3 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
43
44
45
46
47
import { NextResponse, type NextRequest } from 'next/server';
import { createClient } from '@/utils/supabase/middleware';
export async function middleware(request: NextRequest) {
const { supabase, response } = createClient(request);
const {
data: { user },
} = await supabase.auth.getUser();
const { pathname } = request.nextUrl;
const isProtectedRoute =
pathname.startsWith('/login/new-player') ||
pathname.startsWith('/voting') ||
pathname.startsWith('/host') ||
pathname.startsWith('/profile') ||
pathname.startsWith('/kitchen') ||
pathname.startsWith('/minigame');
if (!user && isProtectedRoute) {
const redirectUrl = request.nextUrl.clone();
redirectUrl.pathname = '/login';
return NextResponse.redirect(redirectUrl);
}
// If already logged in, sending them back to login is not useful
if (user && pathname === '/login') {
const redirectUrl = request.nextUrl.clone();
redirectUrl.pathname = '/profile';
return NextResponse.redirect(redirectUrl);
}
return response;
}
export const config = {
matcher: [
'/login/:path*',
'/voting/:path*',
'/host/:path*',
'/profile/:path*',
'/kitchen/:path*',
'/minigame/:path*',
],
};