skip to Main Content

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?

2

Answers


  1. Chosen as BEST ANSWER

    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 
    }
    

  2. You can add a closure in the attempt() method in authenticate() method at appHttpRequestsAuthLoginRequest.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());
        }
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search