Home > Mobile >  How to check additional field Laravel 9 breeze
How to check additional field Laravel 9 breeze

Time:11-29

I added new field 'approved' to USERS table. Field is index for now it could be 0 or 1. On user login I want to check this field and if it's 0 do not User let login. How to do in Laravel 9 breeze?

CodePudding user response:

You should go to LoginRequest and write something like this in authenticate() method

$user = User::where('email', $this->email)->first();

if(!$user->approved) {
        throw message here and redirect 
}

CodePudding user response:

You can add a closure in the attempt() method in authenticate() method at app\Http\Requests\Auth\LoginRequest.php

In LoginRequest

    public function authenticate()
    {
        $this->ensureIsNotRateLimited();

        if (! Auth::attempt(array_merge($this->validated(),[
                fn($query) => $query->where('approved', 1) // check if the user is approved if not it will not authenticate the user
            ]), $this->boolean('remember')))
            {
            RateLimiter::hit($this->throttleKey());

            throw ValidationException::withMessages([
                'email' => trans('auth.failed'),
            ]);
        }

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