-
Notifications
You must be signed in to change notification settings - Fork 1
/
middleware.ts
91 lines (78 loc) · 3.05 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
import { NextResponse } from "next/server";
import type { NextRequest } from "next/server";
import { auth } from "./auth";
export async function middleware(request: NextRequest) {
// start a console timer
const pathname = request.nextUrl.pathname;
// check if user session has username if not redirect to /null if not already there
const session = await auth();
if (
session?.user &&
(!session.user.username || !session.user.username_normalized) &&
pathname !== "/null"
) {
return NextResponse.redirect(new URL("/null", request.url));
} else if (!session?.user && pathname === "/null") {
return NextResponse.redirect(new URL("/", request.url));
}
// if the request is localhost, return
const hostHeaders = request.headers.get("host") ?? "";
if (hostHeaders.startsWith("localhost")) {
return;
}
// list of domains
const shortDomain = "eid.to";
const mainDomain = "e-id.to";
const emojiDomain = "👤.to";
const emojiDomainPunycode = "xn--mq8h.to";
// list of domains with www
const shortDomainWWW = `www.${shortDomain}`;
const mainDomainWWW = `www.${mainDomain}`;
const emojiDomainWWW = `www.${emojiDomain}`;
const emojiDomainPunycodeWWW = `www.${emojiDomainPunycode}`;
// list https url's
const shortHttps = `https://${shortDomain}`;
const mainHttps = `https://${mainDomain}`;
const emojiHttps = `https://${emojiDomain}`;
const emojiHttpsPunycode = `https://${emojiDomainPunycode}`;
const userAgent = request.headers.get("user-agent") ?? "";
// check if the user agent is Safari and not Chrome
// since safari does support emoji domains and chrome does not
const useEmojiDomain =
/Safari/i.test(userAgent) &&
/Version\//i.test(userAgent) &&
!/Chrome/i.test(userAgent) &&
!/CriOS/i.test(userAgent);
const targetUrl = useEmojiDomain ? emojiHttps : mainHttps;
const targetDomain = useEmojiDomain ? emojiDomain : mainDomain;
const targetDomainWWW = useEmojiDomain ? emojiDomainWWW : mainDomainWWW;
const targetDomainPunycode = useEmojiDomain
? emojiDomainPunycode
: mainDomain;
const targetDomainPunycodeWWW = useEmojiDomain
? emojiDomainPunycodeWWW
: mainDomainWWW;
// console.log("targetDomain pre", targetDomain);
// console.log("hostHeaders pre", hostHeaders);
// console.log("targetUrl pre", targetUrl);
// rewrite only if targetUrl is not the same as the current
if (
targetDomain === hostHeaders ||
targetDomainWWW === hostHeaders ||
targetDomainPunycode === hostHeaders ||
targetDomainPunycodeWWW === hostHeaders
) {
// console.log("targetUrl is the same as the current");
// finnish console timer
return;
}
// console.log("targetDomain", targetDomain);
// console.log("hostHeaders", hostHeaders);
// console.log("targetUrl", targetUrl);
return NextResponse.redirect(new URL(pathname, targetUrl));
}
// Optionally, don't invoke Middleware on some paths
export const config = {
// matcher: ["/((?!.+\\.[\\w]+$|_next).*)", "/", "/(api|auth|trpc)(.*)"],
matcher: ["/((?!auth|api|_next/static|_next/image|favicon.ico).*)"],
};