Wednesday, July 17, 2019

How to switch Authentication from one user to another using Laravel

How to switch Authentication from one user to another using Laravel

EmailThis Premium lets you save unlimited bookmarks, PDF, DOCX files, PPTs and images. It also gives you a PDF copy of every page that you save. Upgrade to Premium →

Firstly, I login as user X, and I have a function like:

public function loginAs($userId) {     Auth::loginUsingId($userId); // now use Y user     return response()->json(['logged' => Auth::check(), 'user' => Auth::user()]);  }

and when I try to print out Auth::check() it returns TRUE which is fine so far I am logged now as user Y, the thing is that I do first login as an X user and then switch to another Y user but when I do call some other functoins it seems that the current user logged is still X, and I want to be Y the current user logged... This might have to do something with session or I dont know exactly how to do this thing, would be grateful if someone has any sample or have any idea how to achieve such things in Laravel 5.

davejal

4,45999 gold badges2626 silver badges5959 bronze badges

asked Dec 15 '15 at 17:04

ArtanArtan

3111 silver badge33 bronze badges

Can you elaborate? I was able to switch users using the following:

use Auth;  class TempController extends Controller {      public function index() {         $user = User::find(1);         Auth::login($user);         var_dump(Auth::user()->id); // returns 1         Auth::logout();           var_dump(Auth::user()); // returns null          $user = User::find(2);         Auth::login($user);         var_dump(Auth::user()->id); // returns 2         Auth::logout();      } }

routes.php

Route::get('/temp', 'TempController@index');

If you run this, you can see the logged in user's id changes.

answered Jan 18 '16 at 21:29

DecipleDeciple

73811 gold badge88 silver badges1616 bronze badges

answered Mar 1 '17 at 5:57

Sina MiandashtiSina Miandashti

1,56711 gold badge2222 silver badges3737 bronze badges

Not the answer you're looking for? Browse other questions tagged php laravel or ask your own question.

Source: https://stackoverflow.com/questions/34295188/how-to-switch-authentication-from-one-user-to-another-using-laravel

Please check the attached file.

EmailThis was not able to extract useful content from the website. Hence, we have saved the webpage to a PDF file. You can find that attached along with this email.

Upgrade to Premium Plan

✔ Save unlimited bookmarks.

✔ Save PDFs, DOCX files, images and Excel sheets as email attachments.

✔ Get priority support and access to latest features.

Upgrade to Premium

No comments: