Country blocking by ip
Unanswered
Korat posted this in #help-forum
KoratOP
Hello, i have nextjs app (13.1.4)
I want to block country by ip on serverside, i tried to use _app.tsx with getInitialProps, useEffect(this solution work on client side). I'm trying to handle middleware.ts, but this is checking every request at now.
We need to detect the first time the user load the app then block them if user's country in the blocklist? How do u implement geoblocking?)))
I want to block country by ip on serverside, i tried to use _app.tsx with getInitialProps, useEffect(this solution work on client side). I'm trying to handle middleware.ts, but this is checking every request at now.
We need to detect the first time the user load the app then block them if user's country in the blocklist? How do u implement geoblocking?)))
19 Replies
export async function middleware(request) {
const { geo } = request
console.log(geo.country)
}do note though that this is provided by the hosting platform and not provided by nextjs, so depending on where you host it you might not have this geolocation information
but if you host on vercel it should be good
KoratOP
will it work on non vercel hostings? i checked it, but on localhost i got undefined and i'm not sure could i get on prod
in dev mode then it is undefined yes
but you can try in prod
as i said, it's not guaranteed to be available on all hosting platforms
though any hosting platforms claiming to support nextjs well should be able to provide this information
KoratOP
maybe u now, how can i check when user try to load my app first time
as i said, middleware
KoratOP
because my implementing has requests to cms to get 'access'=true or false, and it turns out even if i allow the user then everytime for any change i'll make req to external cms
not changes, actions
actions like route handling, call api etc
middleware captures all incoming requests
so if you make another request to your app, middleware is triggered again and that is by design
KoratOP
yep, sure
i found way with cookies maybe
i found way with cookies maybe
in the first load i'll write ip in cookie then handlle it