Ad

Making A Log In As User In Laravel

- 1 answer

i want to know if there is any way to simulate the login as user and make the session for that user .i want my help-desk to see the site from users vision so they need to log in as user as long as i cant show the user s password to the admin (its not possible as far as i know if its not that would be a good solution too ). i want to place a button with some text for example :

LoginAsThisUser

and when my help desk clicks on the button he logs in as the user is there any way to do that ??

Ad

Answer

It's possible, and it's fairly simple to do. All you need to do is to create a route, something like this:

Route::get('admin/login-as-user/{id}', 'Admin\[email protected]')->name('login.as.user');

After that, pass the route to your LoginAsThisUser button with desired user id :

<a target="_blank" rel="nofollow noreferrer" href="{{route('login.as.user'), $user->id}}" class="btn btn-default">LoginAsThisUser</a>

And create a function in your controller:

public function loginAsUser($id)
{
     $user = User::findOrFail($id);
     Auth::login($user);
     return redirect('/');
} 

Note: this is not tested, let me know if you encounter any errors.

Ad
source: stackoverflow.com
Ad