Home > Software design >  LARAVEL, SESSION doesn't get saved (AttemptToAuthenticate)
LARAVEL, SESSION doesn't get saved (AttemptToAuthenticate)

Time:05-09

I'm trying to make authentification based on user group, but the session won't get saved! Idk why

Here's the bit of my code. I checked everything else and it works. Actually, It worked even before that but I was implementing stripe and I updated the composer (which probably reset the settings here), and ever since it won't work.

I reimplemented my old code (which worked before) but something's wrong. Fortify\src\actions\AttemptToAuthenticate

 public function handle($request, $next)
{
    if (Fortify::$authenticateUsingCallback) {
        return $this->handleUsingCustomCallback($request, $next);
    }

    if ($this->guard->attempt(
        $request->only(Fortify::username(), 'password'),
        $request->boolean('remember'))
    ) {
        if(Auth::user()->utype === 'ADM')
        {
            session(['utype'=>'ADM']);
        }
        else if(Auth::user()->utype === 'USR')
        {
             session(['utype'=>'USR']);
            return redirect(RouteServiceProvider::HOME);
        }
        else if(Auth::user()->utype === 'ENT')
        {
             session(['utype'=>'ENT']);
        }
        return $next($request);
    }

    $this->throwFailedAuthenticationException($request);
}

NOTE: I've also referenced the Auth (use Illuminate\Support\Facades\Auth;) and RouteServiceProvider (use App\Providers\RouteServiceProvider;) as appropriate. I have checked if it's saving it in the session and it doesn't seem so! Any ideas what could be causing it?

CodePudding user response:

This should be easy to debug. Since I cannot reconstruct your scenario, I recommend the following for you:

  1. Test your session r/w by writing a simple unit test:
/** @test */
public function session_test()
{
    session(['utype' => 'ENT']);
    
    $session = Session::get('utype');
    
    $this->assertEquals('ENT', $session);
}
  1. Assuming you have a User Model and Factory, continue with a simple Authentication test:
/** @test */
public function user_can_authenticate()
{
    $user = User::factory()->create();

    auth()->login($user);

    $this->assertAuthenticatedAs($user);
}
  1. set a breakpoint somewhere at $this->guard->attempt and start the debugger. My guess is, you even don't reach the session(['utype'=>'xxx']); lines
  • Related