-
Notifications
You must be signed in to change notification settings - Fork 0
/
middleware.ts
57 lines (42 loc) · 1.66 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
import { NextRequest, NextResponse } from 'next/server'
const getProtocol = (): string =>
process.env.NODE_ENV === 'production' ? 'https://' : 'http://'
type Middleware = (req: NextRequest) => NextResponse
// Hack to read proper headers from the request, without this host will always be "localhost", even on production. Its unsafe because host may be forged
// https://github.com/vercel/next.js/issues/37536
function withHostFromHeaders(middleware: Middleware) {
return (...args: any[]) => {
let { nextUrl, headers, url } = args[0]
nextUrl.host = headers.get('Host') ?? nextUrl.host
url = nextUrl.href
// @ts-expect-error
return middleware(...args)
}
}
export default withHostFromHeaders((req: NextRequest): NextResponse => {
const hostname = req.headers.get('host')
if (!hostname) {
return NextResponse.next()
}
const domainKey = hostname.split('.')[0]
// There's no subdomain so continue
if (!domainKey || domainKey === hostname) {
return NextResponse.next()
}
// Safeguard to not redirect on these URLs (eg. app.example.com)
if (['www', 'app', 'dev', 'stg', 'vercel', 'example'].includes(domainKey)) {
return NextResponse.next()
}
// Remove domainKey from hostname
const origin = hostname.replace(`${domainKey}.`, '')
const signInUrl = `${getProtocol()}${origin}/signin?domainKey=${domainKey}`
// this will be correct Url
// on production https://app.example.com/signin?domainKey=foobar
// on locahost http://localhost:3000/signin?domainKey=foobar
console.log({ signInUrl })
return NextResponse.redirect(signInUrl)
})
// Run only on index (eg. foobar.example.com)
export const config = {
matcher: ['/'],
}