如何使用 Laravel 5 登录用户

How to login user with Laravel 5

任何人都可以帮助我使用 Laravel 登录用户,这是我的尝试:

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);

}

这是错误:

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

使用 Auth Facade:

Auth::loginUsingId(1);

Relevant laravel docs article

您不能只实例化 Guard,因为它具有在创建它时需要注入的依赖项。这是构造函数:

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

您有几个选择:

1。使用门面:

Auth::loginUsingId(1);

2。使用 IoC 容器:

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

3。使用依赖注入(推荐):

在class的构造函数中你要使用这个:

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

在你的方法中:

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

如果你得到

Trait 'Illuminate\Auth\UserTrait'

对我来说,这听起来很像 Laravel 4(Laravel 5 不再具有此特征)是否有可能您正在迁移您的应用程序?看看新的 default User model on github