Custom laravel authentication: What's next after authenticating users via Fortify::authenticateUsing()

102 Views Asked by At

The goal of my task is to properly authenticate username and password against a third party API, get the access token and store in the session, and use the access token to get user details such as email, full name, etc, and store them in the session, which would be fetched via Auth::user(). This means that I don't use laravel's default method of getting user data from the database via ID.

I have recently setup a new laravel project to use Fortify. now my code looks something like this:

class FortifyServiceProvider extends ServiceProvider
{
    ...

    /**
     * Bootstrap any application services.
     */
    public function boot(): void
    {
        ...

        // CUSTOM AUTHENTICATION
        Fortify::authenticateUsing(function (Request $request) {
            ...
            return new VRAPIUser($user_details);
        });
    }
}

the VRAPIUSer() looks like this, basically containing the access token that I need to do API requests

class VRAPIUser extends Authenticatable
{
    /**
     * The attributes that are mass assignable.
     *
     * @var array<int, string>
     */
    protected $fillable = [
        'id',
        'first_name',
        'last_name',
        'email',
        ...
        'access_token',
        'refresh_token',
        'token_expires_in',
    ];
}

On Laravel's documentation, it states that I need to create a custom provider, guard, and all stuff that I have a hard time grasping what codes to put inside.

So inside AuthServiceProvider's boot() method, I registered the following, which up to now, I have zero idea how to properly implement. Laravel's documentation doesn't give a detailed step-by-step instruction on how to.

// custom driver
Auth::extend('custom_auth', function (Application $app, string $name, array $config) {
    return new VRAppCustomAuthGuard(NO IDEA WHAT TO PUT);
});

// custom provider
Auth::provider('custom_auth', function (Application $app, array $config) {
    return new VRAppCustomUserProvider($app, $config);
});

so my VRAppCustomUserProvider declares the class as below based on Laravel's documentation and it contains all the stubs needed (thanks to my IDE doing it for me), so I think I've implemented it just fine.

class VRAppCustomUserProvider implements UserProvider

VRAppCustomAuthGuard on the other hand is the driver that I don't know how to properly implement or how to properly instantiate

I've tried declaring it as class VRAppCustomAuthGuard extends SessionGuard but declaring it as such on the AuthServiceProvider's boot() method would ask a UserProvider and a type Illuminate\Contracts\Session\Session but I don't know where to get it from. Tried also declaring it as class VRAppCustomAuthGuard implements Guard, StatefulGuard but I think it's more complicated.

I think just knowing where Fortify::authenticateUsing() passes the user information would make me progress greatly.

Help and/or direction would be appreciated

0

There are 0 best solutions below