-
Notifications
You must be signed in to change notification settings - Fork 3
/
middleware.ts
35 lines (29 loc) · 984 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";
export const config = {
matcher: ["/((?!api|_next/static|_next/image|favicon.ico).*)"],
};
export async function middleware(request: NextRequest) {
const response = NextResponse.next();
if (!request.cookies.get("access_token")) {
const data = await fetch("https://osu.ppy.sh/oauth/token", {
body: JSON.stringify({
client_id: process.env.OSU_CLIENT_ID,
client_secret: process.env.OSU_CLIENT_SECRET,
grant_type: "client_credentials",
scope: "public",
}),
headers: {
"Content-Type": "application/json",
},
method: "POST",
});
if (!data.ok) throw new Error("Can't get user token from osu.ppy.sh");
const json = await data.json();
response.cookies.set("osu_access_token", json.access_token, {
expires: new Date(Date.now() + json.expires_in * 1000),
httpOnly: true,
secure: true,
});
}
return response;
}