Search code examples
phplaravellaravel-5laravel-5.3

override laravel login method


my problem is - registered users data(username,email,password) stored on remote(other) server. when user tries to login with email and password, request goes to that remote server, and retrieve data from there(with API). the problem is - when i retrieve data, if user exists, i should login him/her in(i don't check anything in database). here's laravel login method

public function login(Request $request)
{
    $this->validateLogin($request);

    // If the class is using the ThrottlesLogins trait, we can automatically throttle
    // the login attempts for this application. We'll key this by the username and
    // the IP address of the client making these requests into this application.
    if ($this->hasTooManyLoginAttempts($request)) {
        $this->fireLockoutEvent($request);

        return $this->sendLockoutResponse($request);
    }

    $credentials = $this->credentials($request);

    if ($this->guard()->attempt($credentials, $request->has('remember'))) {
        return $this->sendLoginResponse($request);
    }

    // If the login attempt was unsuccessful we will increment the number of attempts
    // to login and redirect the user back to the login form. Of course, when this
    // user surpasses their maximum number of attempts they will get locked out.
    $this->incrementLoginAttempts($request);


    return $this->sendFailedLoginResponse($request);
}

i already wrote methods i need to retrieve data, but how should i login in after i get that data? should i override login method or maybe there is a better way?


Solution

  • Here is how to login users manually on 5.2:

    if (/* your condition */) {
        $user = User::where('email', $credentials['email'])->first();
        Auth::login($user);
        return $this->handleUserWasAuthenticated($request, $throttles);
    }
    

    Here is for 5.3:

    if (/* your condition */) {
        $user = User::where('email', $credentials['email'])->first();
        Auth::login($user);
        return $this->sendLoginResponse($request);
    }