How to login user with Laravel 5

Vladimir

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

lukasgeiter

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

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

Laravel 5: How to store extra attributes to user login session

How to check user status while login in Laravel 5?

laravel 5 redirect user after login based on user's role

Laravel - How to redirect to login if user is not authenticated

How to restrict user login in Laravel if email is not verified

How to view login user data in Laravel 5.2

How to update the last login column when the user successful login with Laravel?

How to change default login URL in Laravel 5?

How to disable auto login on register in Laravel 5?

extend laravel 5 built-in authentication to login only "if user == active"

Laravel 5: The difference between Auth::guard($this->getGuard())->login($user); and auth()->login($user);

Authenticate user on login - Laravel

Laravel login as another user

How to change Bcrypt to Sha1 when user login with using oauth2 or via web in Laravel 5

Auth::login($user) in laravel not able to login the user

Laravel 5 TokenMismatchException on login

How to make user, and roles relationship in laravel 5?

Symfony 5 : how to check if user is banned or not before login

How to prevent user login automatically after registration in Laravel 5.5

How to Verify Email Without Asking the User to Login to Laravel

How to check user activeness (from database) before login in Laravel

Laravel how to prevent login of users based on extra flag on User model?

how to change !isset($_SESSION['user_login']) to laravel?

how to check if the user login and visit specific page first time Laravel?

How to redirect a specific user to a question answer page on login in laravel

How to make working login into Laravel 5.+ with JMeter?

How use the hash in my custom login laravel5

Laravel Auth count user login

change user login method in Laravel