-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmiddleware.ts
More file actions
26 lines (22 loc) · 900 Bytes
/
middleware.ts
File metadata and controls
26 lines (22 loc) · 900 Bytes
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
import { NextResponse } from 'next/server';
import type { NextRequest } from 'next/server';
import { getToken } from 'next-auth/jwt';
export async function middleware(req: NextRequest) {
const token = await getToken({ req, secret: process.env.NEXTAUTH_SECRET });
if (!token) {
const isApiRequest = req.nextUrl.pathname.startsWith("/api");
if (isApiRequest) {
return new NextResponse(
JSON.stringify({ status: false, message: "Unauthorized access" }),
{ status: 401, headers: { "Content-Type": "application/json" } }
);
}
const signInUrl=(new URL('/signin', req.url));
signInUrl.searchParams.set('callbackUrl', req.nextUrl.pathname)
return NextResponse.redirect(signInUrl)
}
return NextResponse.next();
}
export const config = {
matcher: ['/api/stores/:path*', '/api/products/:path*' , '/api/admin/:path*', '/api/sendemail/:path*'],
};