Laravel 5 Override Login Function

前端 未结 3 1821
挽巷
挽巷 2021-01-14 17:05

I\'m working on my Laravel Project and trying to override the default postLogin() from AuthenticatesAndRegistersUsers . So I have updated my AuthController and added this t

3条回答
  •  日久生厌
    2021-01-14 17:13

    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, 'status' => 0])) {
                return redirect($this->loginPath())
                    ->withInput($request->only('email', 'remember'))
                    ->withErrors('Your account is Inactive or not verified');
            }
    

    status 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, 'status' => 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',
                ]);
        }
    

提交回复
热议问题