forked from ErickLimaS/anime-website
-
Notifications
You must be signed in to change notification settings - Fork 1
/
middleware.ts
95 lines (58 loc) · 3.23 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
import axios from 'axios'
import { NextResponse } from 'next/server'
import type { NextRequest } from 'next/server'
// handles user selected language for media title, adult content...
export async function middleware(request: NextRequest) {
// Related to Anilist Login Access
if (request.nextUrl.hash) {
const anilistAccessHashOnURL = request.nextUrl.hash
const accessTokenHash = anilistAccessHashOnURL.slice(anilistAccessHashOnURL.search(/\baccess_token=\b/), anilistAccessHashOnURL.search(/\b&token_type\b/)).slice(13)
const tokenType = anilistAccessHashOnURL.slice(anilistAccessHashOnURL.search(/\btoken_type=\b/), anilistAccessHashOnURL.search(/\b&expires_in\b/)).slice(11)
const expiresIn = anilistAccessHashOnURL.slice(anilistAccessHashOnURL.search(/\bexpires_in=\b/)).slice(11)
axios.post(`${process.env.NEXT_PUBLIC_WEBSITE_ORIGIN_URL}/api/anilist`, {
accessToken: accessTokenHash,
tokenType: tokenType,
expiresIn: expiresIn
})
}
const tokenOnCookie = request.cookies.get("access_token")
const accessToken = tokenOnCookie ? JSON.parse(tokenOnCookie.value!).accessToken : undefined
if (request.nextUrl.pathname.startsWith("/watch")) {
const playWrongMedia = request.cookies.get("wrong_media_enabled")?.value
const queryParamString = new URLSearchParams(request.nextUrl.searchParams)
if (playWrongMedia == "true") {
return NextResponse.next({ headers: { authorization: `Bearer ${accessToken}` } })
}
else if (playWrongMedia == "false" && queryParamString.toString().includes("&alert") == false) {
if (accessToken) {
return NextResponse.redirect(`${request.nextUrl.origin}${request.nextUrl.pathname}?${queryParamString.toString()}&alert=true`,
{ headers: { authorization: `Bearer ${accessToken}` } }
)
}
return NextResponse.redirect(`${request.nextUrl.origin}${request.nextUrl.pathname}?${queryParamString.toString()}&alert=true`)
}
if (accessToken) {
return NextResponse.next({ headers: { authorization: `Bearer ${accessToken}` } })
}
return NextResponse.next()
}
if (request.nextUrl.pathname.startsWith("/media")) {
const mediaTitleLang = request.cookies.get("media_title_language")?.value
const isParamsOnPathname = request.nextUrl.search == `?lang=${mediaTitleLang}`
if (mediaTitleLang) {
if (accessToken) {
if (!isParamsOnPathname) {
return NextResponse.redirect(`${request.nextUrl.origin}${request.nextUrl.pathname}?lang=${mediaTitleLang}`, { headers: { authorization: `Bearer ${accessToken}` } })
}
return NextResponse.next({ headers: { authorization: `Bearer ${accessToken}` } })
}
if (!isParamsOnPathname && mediaTitleLang) {
return NextResponse.redirect(`${request.nextUrl.origin}${request.nextUrl.pathname}?lang=${mediaTitleLang}`)
}
}
}
if (accessToken) {
return NextResponse.next({ headers: { authorization: `Bearer ${accessToken}` } })
}
return NextResponse.next()
}