Search code examples
phplaravelauthenticationlaravel-passport

Laravel Passport login user by custom method, without password


Hello I am using Passport as auth provider.

Now I need to login an user programmatically, skipping the password requirement, creating the authtoken and all.

This is what I am trying:

User model

//Return user instance by username
public function LoadByEmail($email) 
{
    return $this->where('email', $email)->first();
}

AuthController->login

    $email = 'existing@email.com';
    $exists = User::EmailExists($email); //user already exists

    if($exists) {
        $user = new User;
        $currentUser = $user->LoadByEmail($email);

        $currentUser = auth()->login($currentUser);
    }

CurrentUser is null. How do I login an eisting user by its email?


Solution

  • use this :

    
    $email = 'existing@email.com';
    $user = User::where('email',$email)->first() ;
    if($user) {
            
      Auth::loginUsingId($user->id, true);
    
    }
    

    the namespace of Auth is use Illuminate\Support\Facades\Auth;