I have built an API with Laravel + Sanctum consumed by a React Native app. I need to store the user’s country, regardless they are logged-in or not. Will have to check the visitor’s country on each request to send them country-specific products in response, need the user be identifiable like a web session.
How/where to store the info as session is not available or a viable option to store the data on the server with regard to API?
I know it’s not a Laravel specific question but I could not find any standard answer online.
2
Answers
You grab their IP address in Laravel via the
request()->ip()
method.There is also a package you can use in order to map their IP address to the country they are from: https://github.com/Torann/laravel-geoip
Make an API endpoint which uses that package to map the request IP address to the persons country and store the response as a cookie in your React Native app.
When someone loads your app, check if their country cookie is set. If it isn’t, call your API endpoint to get their country and store it in a cookie.
You can use Auth for Authentition
You Can Also Use Session