2

I am building an application that is using a third party authentication database. I have created a custom composer package to "intercept" the POST request to /login. Everything is working great - I'm able to get a user object back and save it to my (laravel) database.

I am now at the point where I want to redirect to the home page and do "stuff". I would like to use Laravel's native Auth as much as possible if I can.

For example, on the home page I am doing this:

$foo = auth()->user()->foo->where('active', 1);

No surprise, since I am not using Laravel's native Auth method, auth()->user() is returning null. Once I have my user created/found in my database, is it possible to tie back into Laravel's auth() methods?

Thank you for any suggestions!

EDIT

Reading the documentation, this looks like the direction I need to go but I'm falling short understanding how to connect/register my custom package (I think)...

EDIT 2

I am going to keep updating this as I feel I make any progress in hopes that it will not only help me, but help others get a better picture of what I am trying to accomplish. Ultimately help others who may be trying to do the same.

I have updated my app/Providers/AuthServiceProviderAuthServiceProvider as such:

use My\Package\MyThirdPartyServiceProvider;


...


Auth::provider('foo', function ($app, array $config) {
        // Return an instance of Illuminate\Contracts\Auth\UserProvider...

    return new MyThirdPartyServiceProvider($app->make('foo.connection'));
});

I have also updated my config/auth file:

'providers' => [
    'users' => [
        'driver' => 'foo',
        'model' => App\User::class,
    ]
2
  • You will need a class to implement the Illuminate\Contracts\Auth\UserProvider interface (either in your package or in your project). That class can be used as an auth driver by configuring it in auth.php and registering it in a service provider. Commented Oct 23, 2019 at 14:29
  • Hi PtrTon - could please elaborate a bit? I'm not concerned which project I implement this class. At a high-level, I'm not sure how to implement it. Thank you for your time! Commented Oct 23, 2019 at 16:15

1 Answer 1

4

As you mentioned the documentation suggests implementing a custom user provider. The following steps more or less describe how you'll tackle it in a bit more detail.

  1. Create or edit a service provider You can create a new service provider by running

php artisan make:provider CustomAuthServiceProvider

  1. In the boot method of your service provider you'll have to configure our auth provider (which will be implemented in step 4).
    public function boot()
    {
        Auth::provider('custom-auth', function ($app, array $config) {
            return new CustomAuthProvider();
        });
    }
  1. Update your auth.php configuration to use the serviceprovider we registered in step 2
'providers' => [
    'users' => [
        'driver' => 'custom-auth',
    ],
],
  1. Create the CustomAuthProvider class itself and implement the UserProvider interface
class CustomAuthProvider implements UserProvider
{
    public function retrieveById($identifier) {
        // Retrieve a user by their unique identifier.
    }

    public function retrieveByToken($identifier, $token) {
        // Retrieve a user by their unique identifier and "remember me" token.
    }

    public function updateRememberToken(Authenticatable $user, $token) {
        // Update the "remember me" token for the given user in storage.
    }

    public function retrieveByCredentials(array $credentials) {
        // Retrieve a user by the given credentials.
    }

    public function validateCredentials(Authenticatable $user, array $credentials) {
        // Validate a user against the given credentials.
    }
}
Sign up to request clarification or add additional context in comments.

1 Comment

This helped me get going in the right direction. Additionally, this was a great help

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.