Secure laravel api route from being called outside of application
I have seen similar questions on here but couldn't find an answer.
I have an api route that I want to utilise with a Vue component that sits on a public page.
It looks up addresses from a post code.
How can I make sure this route can only be used from the same web app and not called directly please.
The page I want to put it on is public so there is no logged in user so I don't think I can use something like Sanctum in this scenario.
Just put the route in the web routes file and wrap it in Auth middleware. The browser will send the user's cookie with the XHR request and therefore the route can be restricted to authenticated users only.
@HardDrive You can give the URL a time restricted signature. On your own page, pass the route to the page via blade (not via pre-compiled javascript files). To access the endpoint the client needs to provide the signed URL. See https://laravel.com/docs/9.x/urls#signed-urls
Ok I'm nearly there with this, the temporary signed url is great I could maybe pass this as a prop to my component but if I add a url parameter to the URL after it is signed it invalidates the signature 🤔
@Snapey could you elaborate on this please. How would I generate a session if not logged in. This would be useful to store partial progress too as I have a multi step form
@Snapey thanks, I need to brush up on this. SO CSRF token is unique per user browsing session? so if I wanted to log progress I could use this to save progress etc. Im guessing CSRF is based on cookie or whatever session driver is being used?