How to log in only active users?

5

In authentication, in addition to the data required to log in, I want only those with status ativo = 1 to log in, but I do not know how to do this.

table user:

id
username
email
password
ativo

UserController

public function store()
{
      $userdata = array(
          'email'    => Input::get('email'),
          'password' => Input::get('password')
      );
      $rules = array(
          'email'    => 'required',
          'password' => 'required'
      );
      $message = array(
         'email.required'    => 'O :attribute é obrigatório.',
         'password.required' => 'A senha é obrigatória.'
        );
      $validator = Validator::make($userdata, $rules, $message);

     if ($validator->passes())
      {
       if (Auth::attempt($userdata))
          {
           return Redirect::to('')->with('success', 'You have logged in successfully');
          }
          else
          {
           return Redirect::route('admin.create')->withErrors(array('password' => 'Senha inválida'))->withInput(Input::except('password'));
          }
      }

  // Something went wrong.
  return Redirect::route('admin.create')->withErrors($validator)->withInput(Input::except('password'));

}

    
asked by anonymous 08.01.2014 / 03:44

2 answers

10

Pass the third parameter that corresponds to your status column (active or inactive, I'll sample it as activated ):

 $userdata = array(  
      'email'     => Input::get('email'),  
      'password'  => Input::get('password'),  
      'activated' => 'A'  
  );  

Laravel will add activated to where when it finds the user, if it is inactive it will not.

    
08.01.2014 / 04:10
1

Another way would look like this:

        if (Auth::attempt(array('username' => Input::get('username'), 'password' => Input::get('password')), Input::has('lembar') ? true : false)) {
            if(Auth::user()->status == TRUE) {
                return Redirect::intended('dashboard');
            } else {
                Auth::logout();
                return Redirect::route('entrar')->withErrors('Desculpe, mas o Usuário está desativado.');
            }

        } else {
            return Redirect::route('entrar')->withErrors('Usuário ou Senha Inválido.');
        }
    
26.10.2014 / 22:50