-
Notifications
You must be signed in to change notification settings - Fork 3
/
middleware.ts
112 lines (107 loc) · 3.37 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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
import {} from "cookies-next";
import { NextApiRequest, NextApiResponse } from "next";
import { JWTPayload, jwtVerify } from "jose";
import { NextRequest, NextResponse } from "next/server";
export async function middleware(req: NextRequest, res: NextApiResponse) {
console.log("running middleware!!");
try {
const verifiedResult = await authenticateRequest(req);
const { verified, payload } = verifiedResult;
if (!verified || !payload) {
throw new Error("Unable to verify request");
}
const requestHeaders = new Headers(req.headers);
// Add new request headers
console.log("setting header");
requestHeaders.set("user-id", `${payload.userId}`);
// You can also set request headers in NextResponse.rewrite
const response = NextResponse.next({
request: {
// New request headers
headers: requestHeaders,
},
});
return response;
} catch (e: any) {
// check if request is to passkey endpoints
if (
req.nextUrl.pathname === "/api/auth/passkey/verifyRegistration" ||
req.nextUrl.pathname === "/api/auth/passkey/all"
) {
return NextResponse.next();
}
req.nextUrl.searchParams.set("from", req.nextUrl.pathname);
req.nextUrl.pathname = "/wallet/create";
return NextResponse.redirect(req.nextUrl);
}
}
export type AuthRequestResponse = {
payload: JWTPayload | null;
verified: boolean;
};
export async function authenticateRequest(
req: NextRequest
): Promise<AuthRequestResponse> {
try {
const cookies = req.cookies;
const accessToken = cookies.get("accessToken")?.value;
const refreshToken = cookies.get("refreshToken")?.value;
if (!accessToken || !refreshToken || !process.env.JWT_ACCESS_SECRET) {
return { payload: null, verified: false };
}
const secret = process.env.JWT_ACCESS_SECRET;
const adaptedSecret: Uint8Array = new TextEncoder().encode(secret);
const { payload, protectedHeader } = await jwtVerify(
accessToken,
adaptedSecret
);
// only hit if jwtVerify succeeds
return { payload: payload, verified: true };
} catch (e) {
return { payload: null, verified: false };
}
}
export async function authenticateApiRequest(
req: NextApiRequest
): Promise<AuthRequestResponse> {
try {
const cookies = req.cookies;
const accessToken = cookies["accessToken"];
const refreshToken = cookies["refreshToken"];
if (!accessToken || !refreshToken || !process.env.JWT_ACCESS_SECRET) {
return { payload: null, verified: false };
}
const secret = process.env.JWT_ACCESS_SECRET;
const adaptedSecret: Uint8Array = new TextEncoder().encode(secret);
const { payload, protectedHeader } = await jwtVerify(
accessToken,
adaptedSecret
);
// only hit if jwtVerify succeeds
return { payload: payload, verified: true };
} catch (e) {
return { payload: null, verified: false };
}
}
export const config = {
matcher: [
"/api/user/activeUser",
"/api/user/deleteUser",
"/api/user/updateProfile",
"/api/shares/:path*",
"/api/account/:path*",
"/wallet/send",
"/wallet/receive",
"/wallet/createName",
"/wallet/delete",
"/profile/:path*",
"/earn",
"/wallet",
"/sync/:path*",
"/api/sync/:path*",
"/api/auth/passkey/all",
"/api/auth/passkey/verifyRegistration",
"/api/actions/completed",
"/api/actions/markComplete",
],
};