Auth0 with Laravel 5.4 including saving the user

Disclaimer: Sorry for my english, not my mother tongue.. but i try my best.

Vacation after I managed to recover from Wacken 2017, the rest of the time is once again put into some fun with PHP and Laravel.

For a small project where a user administration is necessary, I was recently recommended by a colleague the service auth0.

What is auth0? It takes you all the effort to include various authentication providers such as Facebook, Google, Github, and so on. In the free version somewhat limited, but for a beginning more than sufficient (7000 User, 2 Provider).

Well now it was time to connect my Laravel application to auth0. The documentation is quite good (also related to the integration in Laravel) but when i tried to save the users into my own databases, the examples did not work. So if you have to accomplish the same task, or if i have to do it again.. here comes a small tutorial.

As a starting point, let’s assume:

  • Laravel 5.4 application works, and the migrations for the users (are already there) were executed
  • Auth0 account is set up

For the most times we will follow their documentation, with some small changes. If you are new to Laravel, i would suggest the Book “Laravel Up and Running, A Framework for Building Modern PHP Apps

Integrate Auth0 in Laravel 5.4

In your project directory, we install us first the right package for Auth0

Now change your: /config/app.php

Now we differ from the documentation, since we want to save our users in our databases. For this we build a UserRepository: /app/Repository/UserRepository.php with the following content:

We do not set a password, the login is managed via auth0. What you still have to do is to insert the column auth0id in the user table, which of course, Laravel does not know before. The ID is used for the assignment.

The repository will now be registered in our AppServiceProvider: /app/Providers/AppServiceProvider.php

Now it goes to the configuration. For this, we create the necessary configuration files, and also set the necessary routes for the service.

php artisan vendor:publish

File: /routes/web.php

The functions in the IndexController are as follows:

So it is already clear, how you later access the user data: \ Auth :: user (); Now we need to include auth0 as userprovider: /app/config/auth.php

Settings for the application

To get the necessary parameters for our configuration, we visit the website of Auth0. You have to create a basic web application there, and set the following URLs.

The whole should also work with “localhost” if you do not have a domain yet.

For the configuration of the applications we still need:

  • Domain
  • ClientID
  • ClientSecret

In your /app/config/laravel-auth0.php you now either set directly the values or use your “.env” file.

Testing auth0

If you now call, you should seea login and then in your user object the corresponding data as well as an entry in the Users database table.

What is missing in the documentation: How do I actually protect a route? Simply add the Auth-Middleware to your route.

Thats it! — if you prefer a german version of this visit: