-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmiddleware.ts
35 lines (33 loc) · 981 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
29
30
31
32
33
34
35
import { NextRequest, NextResponse } from "next/server";
import { verify } from "./utils/server-utils";
export async function middleware(req: NextRequest) {
// const { pathname } = req.nextUrl;
// console.log(req.headers.get("x-auth-token"));
try {
const token = req.headers.get("x-auth-token");
const payload: any = await verify(token, process.env.JWT_PRIVATE_KEY);
const { id } = payload.userId;
// console.log(id);
const response = NextResponse.next();
response.headers.set("x-user-id", id);
return response;
} catch (error) {
return NextResponse.json(
{ message: "Please authenticate using valid token" },
{ status: 401 }
);
}
}
// "Matching Paths"
export const config = {
matcher: [
"/api/profile/get",
"/api/profile/add",
"/api/profile/update",
"/api/profile/remove",
"/api/connections/getall",
"/api/connections/add",
"/api/connections/update",
"/api/connections/remove",
],
};