-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmiddleware.ts
37 lines (34 loc) · 1.04 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
import { createMiddlewareClient } from '@supabase/auth-helpers-nextjs';
import { NextRequest, NextResponse } from 'next/server';
export async function middleware(req: NextRequest) {
const res = NextResponse.next();
const supabase = createMiddlewareClient({ req, res });
const {
data: { session },
} = await supabase.auth.getSession();
if (req.nextUrl.pathname.startsWith('/dashboard')) {
if (!session) {
return NextResponse.redirect(new URL('/login', req.url));
}
}
const emailLinkError = 'Email link is invalid or has expired';
if (
req.nextUrl.searchParams.get('error_description') === emailLinkError &&
req.nextUrl.pathname !== '/signup'
) {
return NextResponse.redirect(
new URL(
`/signup?error_description=${req.nextUrl.searchParams.get(
'error_description'
)}`,
req.url
)
);
}
if (['/login', '/signup'].includes(req.nextUrl.pathname)) {
if (session) {
return NextResponse.redirect(new URL('/dashboard', req.url));
}
}
return res;
}