-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathmiddleware.ts
More file actions
102 lines (93 loc) · 2.78 KB
/
Copy pathmiddleware.ts
File metadata and controls
102 lines (93 loc) · 2.78 KB
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
import { NextRequest, NextResponse } from 'next/server'
import { jwtVerify, type JWTPayload } from 'jose'
export const AUTHENTICATED_USER_ID_HEADER = 'x-authenticated-user-id'
export interface DecodedAccessToken {
user: {
id: string
name?: string
profilePicUrl?: string
eoa?: string
handle?: string
multiSig?: string
email?: string
}
}
// Force-redirect every HTTP request to HTTPS
function forceHTTPS(req: NextRequest) {
if (
process.env.NODE_ENV === 'production' &&
req.headers.get('x-forwarded-proto') === 'http' &&
// This check prevents us from getting trapped in HTTPS localhost if we are
// testing a production build locally via `next build && next start`; we
// can use `req.headers.get('host')` to get the true host (e.g.
// 'faithdashboard.com'), whereas `req.nextUrl.host` is always
// 'localhost:3000'
!req.headers.get('host')?.includes('localhost')
) {
return NextResponse.redirect(
`https://${req.headers.get('host')}${req.nextUrl.pathname}${
req.nextUrl.search
}`,
301
)
}
}
// Redirect every www request to the non-www equivalent
function redirectWwwToNonWww(req: NextRequest) {
const host = req.headers.get('host') || ''
const wwwRegex = /^www\./
if (wwwRegex.test(host) && !req.headers.get('host')?.includes('localhost')) {
const newHost = host.replace(wwwRegex, '')
return NextResponse.redirect(
`https://${newHost}${req.nextUrl.pathname}`,
301
)
}
}
async function authenticate(req: NextRequest) {
const accessToken = req.headers.get('x-access-token')
const headers = new Headers(req.headers)
// Ensure that the authenticatedUserId header cannot be passed in from the client
headers.delete(AUTHENTICATED_USER_ID_HEADER)
if (accessToken && process.env.NEXT_PUBLIC_JWT_SECRET) {
try {
const { payload } = (await jwtVerify(
accessToken,
new TextEncoder().encode(process.env.NEXT_PUBLIC_JWT_SECRET)
)) as unknown as { payload: JWTPayload & DecodedAccessToken }
headers.set(AUTHENTICATED_USER_ID_HEADER, payload.user.id)
} catch (e: any) {
console.error(e)
return new NextResponse(
JSON.stringify({ success: false, message: 'Authentication failed' }),
{ status: 401, headers: { 'content-type': 'application/json' } }
)
}
}
return NextResponse.next({
request: {
headers,
},
})
}
// Sequentially process an array of middleware functions (this function is to
// avoid repetition and produce cleaner code)
function processMiddlewareFunctions(
req: NextRequest,
middlewareFns: Function[]
) {
for (const middlewareFn of middlewareFns) {
const fnResponse = middlewareFn(req)
if (fnResponse) {
return fnResponse
}
}
return NextResponse.next()
}
export function middleware(req: NextRequest) {
return processMiddlewareFunctions(req, [
forceHTTPS,
redirectWwwToNonWww,
authenticate,
])
}