-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathmiddleware.ts
37 lines (32 loc) · 1.16 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
import type { NextRequest } from 'next/server'
import { NextResponse } from 'next/server'
export async function middleware(request: NextRequest) {
const nextResponse = NextResponse.next()
if (request.cookies.has('bitenow_geo')) {
return nextResponse
}
// As per the Next.js docs, middleware should not have complex data fetching logic.
// https://nextjs.org/docs/app/building-your-application/routing/middleware#use-cases
// You may want to setup your own geoIP for your server. https://docs.nginx.com/nginx/admin-guide/dynamic-modules/geoip
const res = await fetch(process.env.NEXT_PUBLIC_URL + '/api/geo', {
method: 'POST'
})
const { latitude, longitude } = (await res.json()) as Coords
nextResponse.cookies.set(
'bitenow_geo',
JSON.stringify({
latitude,
longitude
})
)
// If you are using Vercel, you can easily get the user's location.
// https://nextjs.org/docs/app/api-reference/functions/next-request#geo
// nextResponse.cookies.set(
// 'bitenow_geo',
// JSON.stringify({
// latitude: request.geo?.latitude || 0,
// longitude: request.geo?.longitude || 0
// })
// )
return nextResponse
}