extend laravel 5 built-in authentication to login only “if user == active”

前端 未结 5 2095
春和景丽
春和景丽 2021-02-05 20:56

I use the included authentication of laravel 5.1.6 and want to know how I can extend it, to work like this:

if (Auth::attempt([\'email\' => $email, \'password         


        
5条回答
  •  一向
    一向 (楼主)
    2021-02-05 21:30

    I would add following first thing in postLogin() function.

           $this->validate($request, [
                'email' => 'required|email', 'password' => 'required',
            ]);
    
            if ($this->auth->validate(['email' => $request->email, 'password' => $request->password, 'active' => 0])) {
                return redirect($this->loginPath())
                    ->withInput($request->only('email', 'remember'))
                    ->withErrors('Your account is Inactive or not verified');
            }
    

    active is a flag in user table. 0 = Inactive, 1 = active. so whole function would look like following..

    public function postLogin(Request $request)
        {
            $this->validate($request, [
                'email' => 'required|email', 'password' => 'required',
            ]);
            if ($this->auth->validate(['email' => $request->email, 'password' => $request->password, 'active' => 0])) {
                return redirect($this->loginPath())
                    ->withInput($request->only('email', 'remember'))
                    ->withErrors('Your account is Inactive or not verified');
            }
            $credentials  = array('email' => $request->email, 'password' => $request->password);
            if ($this->auth->attempt($credentials, $request->has('remember'))){
                    return redirect()->intended($this->redirectPath());
            }
            return redirect($this->loginPath())
                ->withInput($request->only('email', 'remember'))
                ->withErrors([
                    'email' => 'Incorrect email address or password',
                ]);
        }
    

提交回复
热议问题