-
Notifications
You must be signed in to change notification settings - Fork 0
/
middleware.ts
43 lines (40 loc) · 1.07 KB
/
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
32
33
34
35
36
37
38
39
40
41
42
43
import { NextRequest, NextResponse } from "next/server";
import * as jose from "jose";
// Limit the middleware to paths starting with `/api/`
export const config = {
matcher: "/api/profile",
};
export async function middleware(request: NextRequest) {
try {
const requestHeaders = new Headers(request.headers);
const [_, jwt] = requestHeaders.get("authorization").split(" ");
const alg = "HS256";
const secret = new TextEncoder().encode(process.env.SECRET_KEY);
const { payload, protectedHeader } = await jose.jwtVerify(jwt, secret, {
issuer: "urn:supplist:api",
audience: "urn:supplist:app",
});
if (!payload) {
// Call our authentication function to check the request
// Respond with JSON indicating an error message
return NextResponse.json(
{
success: false,
message: "Auth failed",
},
{
status: 401,
}
);
}
} catch (err) {
return NextResponse.json(
{
message: err.message,
},
{
status: 403,
}
);
}
}