Login only if user is active using Laravel

前端 未结 19 1864
孤城傲影
孤城傲影 2020-11-30 23:26

I\'m currently working on my Laravel app and to prevent spam I decided that only active users are able to login. I\'m currently using Laravel\'s login system just like in La

19条回答
  •  难免孤独
    2020-11-30 23:53

    You don't have to override the whole function. You can just change the Validator in AuthController to achieve that adding "exists:table,column" validation.

    Let's assume that you have a users table with email,password and active fields.

    'email' => 'exists:users,email,active,1'

    Here is the validotor function should look like in AuthController.php

    protected function validator(array $data)
    {
        return Validator::make($data, [
            'email' => 'required|email|max:255|exists:users,email,active,1',
            'password' => 'required|confirmed'
        ]);
    }
    

    or if you are using soft deletes this should work too.

    'email' => 'exists:users,email,deleted_at,NULL'

    You can also check out the validation rule at this link http://laravel.com/docs/5.1/validation#rule-exists

提交回复
热议问题