AngularJS + Laravel 5 Authentication

When creating my SPA with angularJS, I came to the point where I want to implement user authentication on my angularJS site. However, I do not know where to start and what are the best practices.

Basically, I have confidence that you can have one or more roles. I was looking for examples, so I could get a basic idea of ​​how to handle this correctly, but so far I have come across examples that are very simple or not so safe (like this ).

So my question is how to implement an authentication service using REST (or custom API URLs) to authenticate the user, and then display the user information on the page using angularJS, and also provide the best security coverage using (for example) csrf token from Laravel?

Thanks in advance, Nick van der Mage

+8
angularjs authentication php laravel
source share
2 answers

I am making an AngularJS application and a RESTful API made with Laravel 5 for the backend, and my authentication approach:

  • Installed jwt-auth . Basically extends the Laravel Auth model by adding authorization with tokens.
  • Added a simple role package for laravel. I used permiso . It has several roles / users and permissions / roles. Very simple.
  • Added jStorage for the interface. (you can use the AngularJS module instead).

So the steps are:

  • Frontend sends the user credentials (email address and password).
  • Checks the server, jwt-auth makes a token to this user and sends it back.
  • Frontend stores the token in the browser storage (without this csrf).
  • All subsequent API calls are created with the title Authorization: Bearer (or with ?token=... )
+18
source share

I like the same approach that @neoroger uses with JSON Web Tokens with jwt-auth. I used the Satellizer package to store the token on the front side and then sent it along with each request to the API.

I put together a couple of tutorials that show how to implement two packages if you are interested:

https://scotch.io/tutorials/token-based-authentication-for-angularjs-and-laravel-apps

http://ryanchenkie.com/token-based-authentication-for-angularjs-and-laravel-apps/

+8
source share

All Articles