#Simple Auth::login() not reflecting in Inertia auth response

2 messages · Page 1 of 1 (latest)

nimble creek
#

I'm trying to build an OAuth server using Passport + Inertia (F/E not relevant) and multiple user models but authentication won't stick after login and consistently returns null

The (current version of the) auth logic

// This works, even if I'm too lazy to change email -> username on the form/request
if (Auth::attempt([
    'username' => $request->email,
    'password' => $request->password
]) {
            $user = User::where(['username' => $request->email])->first();

            // debug, never triggers as expected
            if(!$user) return back()->withErrors(['email' => 'Something went wrong']);

            // Pretty sure this shouldn't be necessary alongside attempt but I tried it anyway
            Auth::login($user);

            // commented-out for debugging but should be re-enabled later
            //$request->session()->regenerate();

            // These both print out the expected user object in the error log
            error_log(json_encode(Auth::user());
            error_log($request->user());
 
            // I can see this happening but it fails
            return redirect()->intended('dashboard');
        }
 
        // I can see this happening by entering an incorrect email/password
        return back()->withErrors([
            'email' => 'The provided credentials do not match our records.',
        ])->onlyInput('email');

which is shown continuously to work when I log the auth user above but ALWAYS returns auth->user null in the Inertia response

HandInertiaResponse's share() method

public function share(Request $request): array
{
  return [
    ...parent::share($request),
    'auth' => [
      'roast' => 'beef',  // sanity check I added. It works
      'user' => $request->user()
    ],
  ];
}

What disconnect could I be missing?

Note: yes, I am using a custom User model but it has implemented Authenticatable . Sorry I can't show the full table

#

Note: the dashboard middleware is mostly the Breeze default (though think I removed one item in the list for troubleshooting):

Route::get('/dashboard', function() {
  return Inertia::render('Dashboard');
})->middleware(['auth'])->name('dashboard');

Also, by "custom user class" I mean the User model has been configured to point at a custom table, config/auth.php should still work as it points at the same model