-
Notifications
You must be signed in to change notification settings - Fork 8
/
middleware.js
35 lines (30 loc) · 851 Bytes
/
middleware.js
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
import { NextResponse } from "next/server"
import { createMiddlewareClient } from "@supabase/auth-helpers-nextjs"
export async function middleware(req) {
const res = NextResponse.next()
const pathname = req.nextUrl.pathname
const supabase = createMiddlewareClient({ req, res })
const {
data: { session },
} = await supabase.auth.getSession()
// console.log(session.user)
if (
!session &&
(pathname.startsWith("/dashboard") || pathname.startsWith("/settings"))
) {
const url = new URL(req.url)
url.pathname = "/signin"
return NextResponse.redirect(url)
}
if (
session &&
(pathname === "/signin" ||
pathname === "/signup" ||
pathname === "/forgot-password")
) {
const url = new URL(req.url)
url.pathname = "/dashboard"
return NextResponse.redirect(url)
}
return res
}