-
Notifications
You must be signed in to change notification settings - Fork 4
/
middleware.js
39 lines (33 loc) · 1.33 KB
/
middleware.js
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
import { authorizeDomain } from "Feature/Authorize/AuthorizeDomain";
import { getIronSession } from "iron-session/edge";
import { ironOptions } from "lib/IronOption";
import { NextResponse } from "next/server";
export const middleware = async (req) => {
const response = NextResponse.next();
const session = await getIronSession(req, response, ironOptions);
if (!session.userToken && req.nextUrl.pathname !== "/userauth") {
return NextResponse.redirect(new URL("/userauth", req.url));
} else if (req.nextUrl.pathname === "/userauth" && session.userToken) {
//* logged in users can't access signup page again
return NextResponse.redirect(new URL("/", req.url));
} else if (session.userToken && req.nextUrl.pathname !== "/userauth") {
//* usage of a buffer instead of string as long string value
//* was taking much space
try {
const data = await authorizeDomain(session.userToken + "");
session.user = data.user;
await session.save();
} catch (error) {
await session.destroy();
return NextResponse.redirect(new URL("/userauth", req.url));
}
}
return response;
};
export const config = {
matcher: [
"/u/:userID/list/anime",
"/u/:userID/list/character",
"/userauth",
],
};