fixing url issues
This commit is contained in:
+6
-3
@@ -7,18 +7,21 @@ type Session = { user: { id: string; email: string } }
|
|||||||
|
|
||||||
export async function middleware(request: NextRequest) {
|
export async function middleware(request: NextRequest) {
|
||||||
const { pathname } = request.nextUrl
|
const { pathname } = request.nextUrl
|
||||||
const isPublic = PUBLIC_PATHS.some((p) => pathname.startsWith(p))
|
|
||||||
|
if (PUBLIC_PATHS.some((p) => pathname.startsWith(p))) {
|
||||||
|
return NextResponse.next()
|
||||||
|
}
|
||||||
|
|
||||||
const { data: session } = await betterFetch<Session>('/api/auth/get-session', {
|
const { data: session } = await betterFetch<Session>('/api/auth/get-session', {
|
||||||
baseURL: `http://localhost:${process.env.PORT ?? 3000}`,
|
baseURL: `http://localhost:${process.env.PORT ?? 3000}`,
|
||||||
headers: { cookie: request.headers.get('cookie') ?? '' },
|
headers: { cookie: request.headers.get('cookie') ?? '' },
|
||||||
})
|
})
|
||||||
|
|
||||||
if (!session && !isPublic) {
|
if (!session) {
|
||||||
return NextResponse.redirect(new URL('/login', request.url))
|
return NextResponse.redirect(new URL('/login', request.url))
|
||||||
}
|
}
|
||||||
|
|
||||||
if (session && pathname === '/login') {
|
if (pathname === '/login') {
|
||||||
return NextResponse.redirect(new URL('/dashboard', request.url))
|
return NextResponse.redirect(new URL('/dashboard', request.url))
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user