Sorry if this is a real Newb Question and even this may be not related to the Laravel. I have googled for hours and still I could't find how should I implement this. I can do some trial and error learning thing, but I need to understand what is the real login behind this.
I am relatively new to Laravel and still working on my first Laravel project using Laravel 5.1 . I have done the user authentication using laravel built-in AuthController authentication. Now users can login to system and they can browse the system without an any issue.
But I am going to implement some user updates using API and ajax posts. My question is how can I authenticate those ajax posts. I do not want to prompt user for the username/password if they are already logged in and also they should not be able to perform update using API if they are not logged in to the system. Further more I need to redirect them to login page if their session is expired (I can do this with return the status, but should be able to access the same web session for validation).
@ohffs thanks for the great video. its really easy work to with api/ajax with Laravel
But may be I was not very clear with explaining my issue. That demo does not work with authentication.
I want to authenticate those ajax posts. I do not want to prompt user for the username/password if they are already logged in to the web site. And only the logged in user should be able to update his profile the api post. Is it possible to share the same web session or use authcontroller validation for the api authentication as well like we do with web requests?
@WPS2 the ajax calls are 'authenticated' as the CSRF token will be checked against the current user - it's up to you if that's enough I guess. If you want something more like your own API keys/tokens you'll probably have to make your own middleware though. Or is your API outside your main app?
@WPS2 hopefully the CSRF token should be "good enough" then? You could add your own API token if you wanted I guess - take something unique about your user, a salt etc.
@ohffs Thanks for your help. I did implement the X-CSRF-TOKEN token and the update is working fine.
But it's keep getting the same token every time even after I sign out and sign in again. Do we need to keep the same token till the user sign out or till his session is expired?
Is it possible to regenerate the token every time when the use logs in to the system?
@WPS2 I'm not 100% sure when laravel regenerates the token - it'll time out after a while though. There's some info here about regenerating it on each request though which you could probably adapt to suit what you want.