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
You can check if the
$customer
variable isnull
before accessing its status property.