Laravel 5 Override Login Function

2019-02-19 03:29发布

问题:

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?

回答1:

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


回答2:

With

if ($this->auth->attempt($credentials, $request->has('remember')))

you are loggin the user in so if you want to log him out use

Auth::logout();

use that piece of code in the else if statement