-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmiddleware.ts
31 lines (23 loc) · 925 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
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: { user } } = await supabase.auth.getUser()
if (user && req.nextUrl.pathname === '/') {
return NextResponse.redirect(new URL('/dashboard', req.url))
}
if (user && req.nextUrl.pathname === '/signIn') {
return NextResponse.redirect(new URL('/dashboard', req.url))
}
if (!user && req.nextUrl.pathname !== '/dashboard/:path*') {
return NextResponse.redirect(new URL('/signIn', req.url))
}
if (!user && req.nextUrl.pathname !== '/account/:path*') {
return NextResponse.redirect(new URL('/signIn', req.url))
}
return res;
}
export const config = {
matcher: ['/account/:path*', '/dashboard/:path*']
}