Switch user sessions using Account Switcher service | Drupal 8

Switch user sessions using Account Switcher service

Submitted by editor on Wed, 05/23/2018 - 22:12

How to switch user sessions using account switcher (account_switcher) service ?


use Drupal\Core\Session\UserSession;
// Call the account switcher service
$accountSwitcher = \Drupal::service('account_switcher');
// Switch to the admin user
$accountSwitcher->switchTo(new \Drupal\Core\Session\UserSession(['uid' => 1]));
// Your Code Here...
// Switch back to old session.


Raymond Wagner (not verified)

Tue, 01/22/2019 - 11:04

I am very happy to read this writing.I got more information about drupal by reading this. It is very helpful writing.This is very useful website and every thing explained in cleared manner so every one can understand this site and this is yours plus point.Thank you for sharing this valuable information.And i am a regular reader of your site.I am also a writer and doing work atone of the best buy essay writing service.




William (not verified)

Sun, 08/11/2019 - 23:03

The correct would be "Your code here..." otherwise it looks like a tutorial to create a website that listens to the users :)

Add new comment

Plain text

  • No HTML tags allowed.
  • Lines and paragraphs break automatically.
  • Web page addresses and email addresses turn into links automatically.