-
Notifications
You must be signed in to change notification settings - Fork 38
/
middleware.ts
28 lines (25 loc) · 832 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
import { auth } from '~/server/auth'
import { NextResponse } from 'next/server'
export default auth((req) => {
if (req.nextUrl.pathname.startsWith('/api/v1') && !req.auth) {
return Response.json(
{ success: false, message: 'authentication failed' },
{ status: 401 }
)
}
if (req.nextUrl.pathname.startsWith('/admin') && !req.auth) {
return NextResponse.redirect(new URL('/login', req.url))
}
if (req.auth && req.nextUrl.pathname === '/login') {
return NextResponse.redirect(new URL('/', req.url))
}
});
// Optionally, don't invoke Middleware on some paths
// Read more: https://nextjs.org/docs/app/building-your-application/routing/middleware#matcher
export const config = {
matcher: [
"/((?!_next/static|_next/image|favicon.ico).*)",
"/admin/:path*",
'/api/v1/:path*',
],
}