Switch user sessions using Account Switcher service
By editor, 23 May, 2018
Question
How to switch user sessions using account switcher (account_switcher) service ?
Example:
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.
$accountSwitcher->switchBack();
Comments