ivanolivero's avatar

Custom authentication not working after login

Hi,

Sorry for the long text, Its been a couple of days and I still can't solve this issue.

I'm working on a site that includes two different logins, one for admins using the default User model and one for end-users using a custom model extending Authenticatable and validating the password on a separate site.

I created a CustomUserServiceProvider extending UserProvider with the required retrieveById, retrieveByCredentials and validateCredentials methods.

Added a CustomAuthServiceProvider with:

        Auth::provider('custom_web', function ($app, array $config) {
            return new CustomUserServiceProvider();
        });

Added this to auth.php

    'providers' => [
        'users' => [
            'driver' => 'eloquent',
            'model'  => App\Models\User::class,
        ],

        'custom_users' => [
            'driver' => 'custom_web',
            'model'  => \App\Models\CustomUser::class
         ],
    ],

and

    'guards' => [

        'web' => [
            'driver'   => 'session',
            'provider' => 'users',
        ],

// this is the guard the custom auth should use

        'front' => [
            'driver'   => 'session',
            'provider' => 'custom_users'
        ],

        'api' => [
            'driver'   => 'token',
            'provider' => 'users',
            'hash'     => false,
        ],

        'graphql' => [
            'driver' => 'custom_token'
        ]
    ],

I also added a custom LoginController to handle the login process.

    public function login()
    {
        if (Auth::guard('front')->attempt(request()->only('email', 'password'))) {
            return redirect(route('front.home'));
        }

        return back()->withErrors([
            'email' => 'invalid credentials',
        ]);
    }

When debugging this process, the Auth attempt completes successfully, going into the CustomUserServiceProvider validateCredentials method, going into the external service, validating the credentials and it returns the user object.

If I add

dd(Auth::guard('front')->check());

Right above the redirect it returns true.

I have my routes wrapped in a FrontAuthenticate middleware

Route::middleware('auth.front')->group( function () {
    Route::get( '/', [ IndexController::class, 'videos'] )->name('front.home');
} );

That has this check which at this point returns false when checking if the request is authenticated and get redirected back to the login page.

    public function handle($request, Closure $next, ...$guards)
    {
        if (Auth::guard('front')->check()) {
			// a dd(Auth::guard('front')->check()); in this point returns false 		
            return $next('/');
        }

        return redirect(route('front.login'));
    }

So between login-in and going to the middleware the check fails and goes back to the login screen.

What could be happening that makes the Authentication guard to fail at that point?

0 likes
0 replies

Please or to participate in this conversation.