oussama.tn
oussama.tn

Reputation: 299

Laravel, can't log a user by id and then redirect him

I'm using Laravel 5.2. I'd like to log a user by his id and then redirect him to the dashboard but it's not working.

I did this:

$result = Auth::loginUsingId($id);
var_dump($result->toArray());

and the result is fine. It returns the object user with all his data.

But after redirecting the user to the dashboard with return redirect()->route('dashboard'); it send me to login page!

I discover then that Auth::user() returns null !

What shall i do?

Thanks

Upvotes: 3

Views: 957

Answers (2)

Muihlinn
Muihlinn

Reputation: 571

Use $redirectTo as stated in the documentation, if you get into login again Auth wasn't successful, perhaps something related with session or cookies, or just a bad time configuration. Try Auth::loginUsingId($id, true); then.

Upvotes: 1

Bogdan
Bogdan

Reputation: 44526

Authentication needs sessions and for sessions to work you need to use the web middleware. So the routes that need working sessions should be defined like this:

Route::group(['middleware' => ['web']], function () {
    // Routes that need sessions go here
});

Upvotes: 4

Related Questions