Laravel 5 Override Login Function

14,282

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',
            ]);
    }
Share:
14,282
jackhammer013
Author by

jackhammer013

Noob

Updated on June 28, 2022

Comments

  • jackhammer013
    jackhammer013 almost 2 years

    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 to override the built-in login,

    public function postLogin(Request $request)
    {
    
        $this->validate($request, [
            'email' => 'required|email', 'password' => 'required',
        ]);
    
        $credentials = $request->only('email', 'password');
    
    
    
        if ($this->auth->attempt($credentials, $request->has('remember')))
        {
            /* Check if the user is Activated */
            $userID = \Auth::user()->id;
            $user = new \App\User;
            $result = $user->isUserActivated($userID);
    
            if($result[0]->status == 1)
            {
                return redirect()->intended($this->redirectPath());
            }
            else if($result[0]->status == 0)
            {
                Session::flash('alert-danger', 'Your account is not yet Activated.');
                return Redirect::to('auth/login');
            }
    
        }
    
        return redirect($this->loginPath())
                    ->withInput($request->only('email', 'remember'))
                    ->withErrors([
                        'email' => $this->getFailedLoginMessage(),
                    ]);
    }
    

    As you can see I have $result[0]->status which tells if the user is activated, if not then I will redirect them back to auth/login. I tried to var_dump($result[0]->status); and it is working fine and also means I override that it coz it's displaying it but my problem is instead of redirecting it to auth/login it is still going thru home and can login even if the status is 0. Seems I'm it's my override doesn't work, but when I var_dump $result[0]->status, it shows. Did i missed something?