Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Laravel 5 - Manually logging in a user without password

I want to login users after authenticating them using OAuth and Google account without entering any password.

I know Auth::login functon can do such action but I don't know how to get the user instance while they haven't been authenticated yet.

Is there a way to do that?

like image 728
Omid Avatar asked Sep 18 '15 21:09

Omid


People also ask

How do I log in manually Laravel?

Manually Logging In A User If you need to log an existing user instance into your application, you may call the login method with the user instance: Auth::login($user); This is equivalent to logging in a user via credentials using the attempt method.

What is auth ()- user () in Laravel?

Auth::user() — You can check if a user is authenticated or not via this method from the Auth Facade. It returns true if a user is logged-in and false if a user is not. Check here for more about how Facades work in Laravel.

What is Guard () in Laravel?

Guards define how users are authenticated for each request. For example, Laravel ships with a session guard which maintains state using session storage and cookies. Providers define how users are retrieved from your persistent storage.


1 Answers

You can 'manually' log a user in in two diefferent ways:

// log user in by ID
Auth::loginUsingId([id of user]);

Or

$user = User::find([id here]);
// or maybe
$user = User::whereUsername([username here])->first();

// and then
Auth::login($user);

See the documentation for authentication.

like image 179
alexrussell Avatar answered Oct 06 '22 19:10

alexrussell