Auth0, Laravel and existing user auth

Please include the following information in your post:

auth0-PHP

  • SDK Version: 7.6.2
  • Platform Version: e.g. Laravel v8.32.1

We have an existing laravel application that already has a working user database using laravel passport. We wanted to implement SSO using auth0 and keep our existing users as is.

We found a few different approaches in the documentation section of auth0. While some of the code samples was using Laravel 6 we decided to go with the readme from github:

The readme says:

// get user
auth('auth0')->user();
// check if logged in
auth('auth0')->check();
// protect routes via middleware use
Route::group(['middleware' => 'auth:auth0'], function () {});

I’ve been trying to use auth(‘auth0’)->user(); and auth(‘auth0’)->check(); but i can’t figure out the correct syntax to use these?

In this readme it configures auth0 as another provider besides the existing ‘users’-provider which i already have. This is exactly what I want. But this doesn’t seem to work. I can login using my auth0 credentials but when i browse pages it tries to lookup the user in the user table instead of using the auth0 driver.

I have been testing by echoing this:
dd(Auth::guard('auth0')->check());

and this:
dd(Auth::check());

both returns false even though there was no errors when logging in.

I can see that it tries to lookup the user in the users table of the database using the jwt token for the id column.

If I change:

'providers' => [
    ...
    'auth0' => [
        'driver' => 'auth0',
    ],
],

to:

'providers' => [
    ...
    'users' => [
        'driver' => 'auth0',
    ],
],

and login again, call the api and everything works perfectly. But this disables my existing user authentication.

Can anyone point me in the right direction as to how I can keep my existing users and also have auth0 provider running on some routes? I got the feeling that i’m really close to have it working. Any advice is very much appreciated!

Thanks in advance

2 Likes