#Programmatically logging in throws a 500 with no logs

5 messages · Page 1 of 1 (latest)

scenic ore
#

I'm stumped, I have a programmatic login, Auth::login($user), that has worked in the past. Now when I hit that line, I get a 500 error back with no body content and strangely:

  • there are no errors in the laravel log
  • there are no errors in the PHP log
  • there are no errors in the NGINX log
  • there are no errors in the network tab, other than 500 error

What else could it be or how can I figure out what is causing this error?

For context, I'm using Laravel Herd locally, but this is also happening in Vapor. I'm using Intertia with Precognition. On PHP 8.3. Logins to FilamentPHP, the admin for the app, work fine, it's just this programmatic login.

Here is the truncated code where this is happening:

public function store(FindAccountToLoginRequest $request)
{
    // I have some logic here to find the user to login 
    // it's working fine, I can dd($user) and see a valid user
    $user = someLogicToDetermineUserToLogin();

    // at this point, we have verified the user, so log them in
    if ($user != null) {
        Auth::login($user); // <- here is where the 500 error is thrown!

        return Redirect::route('accounts.index');
    }
}
spring ocean
#

Please share an untruncated example, and what the 500 error actually is
Test it locally, ensuring that APP_DEBUG is enabled, and LOG_LEVEL is set to "debug"

zenith grove
#

NGL, sounds like a dd() or abort(500) is somewhere in the code. Could even be in vendor files. That wouldn't log anything.

scenic ore
#

Turned out to be a global scope on the User model that was breaking it, though still not sure why. I changed the global scope registration to only run when logged in and that seemed to fix it: static::addGlobalScope(new UserScope); to if (Auth::check()) { static::addGlobalScope(new UserScope); }

And here's the scope

<?php

namespace App\Models\Scopes;

use Illuminate\Database\Eloquent\Builder;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Scope;

class UserScope implements Scope
{
/**
* Apply the scope to a given Eloquent query builder.
*/
public function apply(Builder $builder, Model $model): void
{
if (!auth()->user()) {
// don't apply scope for logged out users, this logic is handled elsewhere
// if this is removed, logins break as the login logic can't find the user
return;
}

    // super admins have no restrictions
    if (auth()->user()->isSuperAdmin) {
        return;
    }

    // for company admins, only return users for the company
    if (auth()->user()->isCompanyAdmin) {
        $builder->where('company_id', auth()->user()->company_id);
        return;
    }

    // otherwise, filter users by the authenticated user's id
    $builder->where('id', auth()->id());
}

}

rigid dragon
#

From my experience, I advise you can clear and regenerate sessions.