Log in with loginza with Laravel 4

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?

+4
source share
4 answers

I use Session to set the user id equal to auth user id. Then, if I want to switch users, I just change the session id, but save the auth user id as myself. In my application, I always pass the session id of the session when requesting data. If I update, create, or gently delete a record, I pass my auth user id. Thus, I have a record of who really changed the data. I never need to log out to switch users, just set the session user ID.

0
source

It appears that the user model is not configured correctly, inheriting from UserInterface.

Your user model should look something like this:

 class User extends Eloquent implements UserInterface 

And your auth.php configuration should look something like this:

 return array( /* |-------------------------------------------------------------------------- | Default Authentication Driver |-------------------------------------------------------------------------- | | This option controls the authentication driver that will be utilized. | This drivers manages the retrieval and authentication of the users | attempting to get access to protected areas of your application. | | Supported: "database", "eloquent" | */ 'driver' => 'eloquent', /* |-------------------------------------------------------------------------- | Authentication Model |-------------------------------------------------------------------------- | | When using the "Eloquent" authentication driver, we need to know which | Eloquent model should be used to retrieve your users. Of course, it | is often just the "User" model but you may use whatever you like. | */ 'model' => 'User', /* |-------------------------------------------------------------------------- | Authentication Table |-------------------------------------------------------------------------- | | When using the "Database" authentication driver, we need to know which | table should be used to retrieve your users. We have chosen a basic | default value but you may easily change it to any table you like. | */ 'table' => '', /* |-------------------------------------------------------------------------- | Password Reminder Settings |-------------------------------------------------------------------------- | | Here you may set the settings for password reminders, including a view | that should be used as your password reminder e-mail. You will also | be able to set the name of the table that holds the reset tokens. | */ 'reminder' => array( 'email' => 'emails.forgot-pass', 'table' => 'forgot_pass' ), ) 
0
source

For me, this worked with \Session::flush(); . It seemed that some variables were still remembered in the session.

0
source

I found out that my user is empty, and I decided by downloading the correct user, I don’t know if it will work for you, but you can try.

0
source

Source: https://habr.com/ru/post/1483157/


All Articles