skip to Main Content

First time i try to log in in my website the website crash with error " status on null" , sometimes when i refresh it lets . I cant find the error.

public function authenticate()
{
        $this->ensureIsNotRateLimited();
        if (!Auth::attempt($this->only('email', 'password'), $this->boolean('remember'))) {
            RateLimiter::hit($this->throttleKey());
            throw ValidationException::withMessages([
                'email' => trans('auth.failed'),
            ]);
        }

        $user = $this->user();
        $customer = $user->customer;
        if ($customer->status !== CustomerStatus::Active->value) { 

            /*the problem is here*/
            Auth::guard('web')->logout();

            $this->session()->invalidate();
            $this->session()->regenerateToken();
            throw ValidationException::withMessages([
                'email' => 'Your account has been disabled',
            ]);
        }

        RateLimiter::clear($this->throttleKey());
    }

Here is the customer status,

enum CustomerStatus: string
{
    case Active = 'active';
    case Disabled = 'disabled';
}

There ary many errors like this in my project , i tried this :

if ($customer->status ?? '' !== CustomerStatus::Active->value)

But then I cant log in , all my accounts become disabled.

2

Answers


  1. public function authenticate()
    {
        $this->ensureIsNotRateLimited();
        if (!Auth::attempt($this->only('email', 'password'), $this->boolean('remember'))) {
            RateLimiter::hit($this->throttleKey());
            throw ValidationException::withMessages([
                'email' => trans('auth.failed'),
            ]);
        }
    
        $user = $this->user();
        $customer = $user->customer;
    
        if ($customer->status !== CustomerStatus::Active->value && $customer->status != null) {
            /*the problem is here*/
            Auth::guard('web')->logout();
    
            $this->session()->invalidate();
            $this->session()->regenerateToken();
            throw ValidationException::withMessages([
               'email' => 'Your account has been disabled',
            ]);
        }
    
        RateLimiter::clear($this->throttleKey());
    }
    
    Login or Signup to reply.
  2. You can check if the $customer variable is null before accessing its status property.

    if (!$customer || $customer->status !== CustomerStatus::Active->value) { 
        // handle disabled account
    }
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search