-
Notifications
You must be signed in to change notification settings - Fork 1
/
middleware.ts
56 lines (47 loc) · 1.83 KB
/
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
44
45
46
47
48
49
50
51
52
53
54
55
56
import { createMiddlewareClient } from '@supabase/auth-helpers-nextjs';
import { NextResponse } from 'next/server';
import type { NextRequest } from 'next/server';
import { Database } from '@/types/database.supabase';
export async function middleware(req: NextRequest) {
console.log(req.url);
// We need to create a response and hand it to the supabase client to be able to modify the response headers.
const res = NextResponse.next();
// Forward req if User tries to reset password, authorization will happen on the client
// console.log(req.cookies.has('supabase-auth-token'));
if (
req.nextUrl.pathname == '/passwordreset' ||
req.nextUrl.pathname == '/tos' ||
req.nextUrl.pathname == '/privacy' ||
req.nextUrl.pathname == '/auth/callback'
)
return res;
// Create authenticated Supabase Client.
const supabase = createMiddlewareClient<Database>({ req, res });
// Check if we have a session
const {
data: { session },
} = await supabase.auth.getSession();
const redirectUrl = req.nextUrl.clone();
// Check auth condition
if (session?.user) {
// Authentication successful, forward request to protected route.
//Trying to access login route while logged in, redirect to index
if (req.nextUrl.pathname == '/login') {
redirectUrl.pathname = '/';
return NextResponse.redirect(redirectUrl);
}
return res;
}
// Auth condition not met, redirect to home page.
if (req.nextUrl.pathname == '/login') return res;
console.log('bruh');
redirectUrl.pathname = '/login';
redirectUrl.searchParams.set('redirectedFrom', req.nextUrl.pathname);
return NextResponse.redirect(redirectUrl);
}
export const config = {
matcher: ['/((?!favicon.ico|_next|opengraph*).*)', '/api/:path*'],
};
// export const config = {
// matcher: ['/((?!favicon.ico|_next).*)', '/api/:path*'],
// };