I have different users, some of them are ordinary users, and some are admins. I need administrators to be able to log in as any of the other users, simply by clicking a button in the user list.
What I still have:
index.blade.php (user list):
<a href='{{URL::route('users.loginas', array('id' => $user->id))}}'>LoginAs</a>
routes.php:
Route::group(array('before'=>'auth'), function() { Route::get('/', array('as'=>'index', 'uses'=>' HomeController@showWelcome ')); Route::resource('users', 'UsersController'); Route::any('users/loginas/{id}', array('as'=>'users.loginas', 'uses' => ' UsersController@loginAs ')); });
UsersController.php:
class UsersController extends BaseController { ... public function loginAs($id) { Auth::logout(); Auth::loginUsingId($id); return Redirect::route('provalogin'); } }
When I click the link for user with identifier 2 from the list of users when logging in with user with identifier 1, I am redirected correctly to mysite.com/users/loginas/2, but then it throws an ErrorException:
Argument 1 passed to Illuminate \ Auth \ Guard :: login () must implement the Illuminate \ Auth \ UserInterface interface, null, called in /var/www/mysite.com/web/vendor/laravel/framework/src/Illuminate/ Auth / Guard.php on line 368 and is determined
Then, if I change the URL to mysite.com/users, I can see that I am really registered as a new user, so Auth::loginUsingId(2) worked.
What am I doing wrong? or how do i do this?