I have a React web app frontend with a Laravel API, as well as a React Native mobile app using the same API. I want to find out if the request is coming from the mobile app, or the web.
I can't use the user-agent to figure this out (or the related packages I've seen) because I want to distinguish the web browser on the phone vs the react native mobile app.
Does anyone have a nice solution for this?
Current solution: mobile app sends along a token w/ request. Laravel Middleware detects token, concludes it's the mobile app, otherwise assumes web browser.
I'm asking this question because I'm wondering if there's some cool Laravel feature that can do this for me elegantly instead of me coding this myself.
@mkrell There’s no such thing. Browsers access servers exactly the same way, and any heuristic you use (such as user agent) can be easily spoofed.
The only way you’re going to be able to do it is by identifying the client that issued the token if you’re using Passport, or the device name from the token if using Sanctum.
Yeah the spoofing was the other reason I didn't want to use user-agent.
I like your idea with making a second Passport client. So the app could ask for the mobile app client, and then Laravel could check which one the bearer token came from. That seems nice and clean.