Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to login user with Laravel 5

Anyone can help me login user with Laravel, this is my try:

public function execute($hasCode){
    if(!$hasCode) return $this -> getAuthorizationFrist();  
    $user = $this->socialite->driver('facebook')->user();
    echo $user->getNickname();
    echo $user->getName();
    echo $user->getEmail();
    echo $user->getAvatar();


    $login = new Guard(); 
    $login ->loginUsingId(1);

}

This is error:

Argument 1 passed to Illuminate\Auth\Guard::__construct() must be an instance of Illuminate\Contracts\Auth\UserProvider, none given, called in /home/comment/public_html/bild/app/AuthenticateUser.php on line 31 and defined

like image 710
Vladimir Avatar asked Mar 18 '23 00:03

Vladimir


1 Answers

You can't just instantiate Guard because it has dependencies that need to be injected when creating it. This is the constructor:

public function __construct(UserProvider $provider,
                            SessionInterface $session,
                            Request $request = null)

You have a few options:

1. Use the facade:

Auth::loginUsingId(1);

2. Use the IoC container:

$auth = app('auth');
$auth->loginUsingId(1);

3. Use dependency injection (recommended):

In the constructor of the class you want to use this:

public function __construct(\Illuminate\Auth\Guard $guard){
    $this->auth = $guard;
}

And in your method:

$this->auth->loginUsingId(1);

If you're getting

Trait 'Illuminate\Auth\UserTrait'

That sounds a lot like Laravel 4 to me (Laravel 5 doesn't have this trait anymore) is it possible that you are migrating your application? Take a look at the new default User model on github

like image 116
lukasgeiter Avatar answered Mar 19 '23 12:03

lukasgeiter