How to log only active users?

Asked

Viewed 927 times

8

In authentication beyond the data needed to log in the user, I want only those who are in status ativo = 1 log in, but I don’t know how to do this check.

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'));

}

2 answers

11


Pass the third parameter that corresponds to your status column (active or inactive, as activated):

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

Laravel will add the activated at the where at the time of locating the user, if it is inactive it will not bring.

  • Thank you Flávio H. Ferreira.

1

Another way would be 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.');
        }
  • Hi, Marcus, welcome to [en.so]. It’s nice to give a brief explanation of why your code solves the problem. The guide [Answer] has more details.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.