I'm on Drupal 8 and currently looking to replicate similar functionality like: https://www.drupal.org/project/urllogin -- They do not have a D8 version yet.

I need to auto login a specific user depending on which URL the user visits. How can this be done on D8? I was thinking of using rules, but there doesnt seem to be an action for "login a user" if Path alias equals X.

Another possible scenario I could just do is create a custom module to auto login a user. Which functions would I need to fire in Drupal 8 to do this?

  • This sounds like a big security risk. – Eyal Jun 13 '16 at 5:41

You just need to use drupal_session_regenerate(), you can see the use of this function here. All you have to do is either play with Uid or if you want to do this for anonymous user then you just create a user xyz and use that ID for session regenerate. You can grab the UID from URL for different users like www.example.com/custom-url-to-login/125 or www.example.com/custom-url-to-login?uid=125

now supposing 125 be the ID of user whom you want to login on visiting the URL.

The code will go something like this:

global $user;
$user = user_load(540); // suppose 540 be UID of xyz account

This is just a suggestion, if it does not work for you please let me the know the correct solution.


| improve this answer | |
  • I think that meddling with the global variable $user is bad practice. – Eyal Jun 13 '16 at 14:20

You can use Auto Login URL Module

Use case

You may get an auto login URL for a user($uid).

// Drupal 7.
$auto_login_url = auto_login_url_create($uid, $destination);
print $auto_login_url;

// Drupal 8. As above plus.
$alu_service = \Drupal::service('auto_login_url.create');
auto_login_url = $alu_service->create($uid, $destination);
print $auto_login_url;

Or convert all links of a text to auto login for a user($uid).

// Drupal 7.
$auto_login_url_text = auto_login_url_convert_text($uid, $text);
print $auto_login_url_text;

// Drupal 8. As above plus.
$alu_service = \Drupal::service('auto_login_url.create');
auto_login_url_text = $alu_service->convert_text($uid, $text);
print $auto_login_url_text;

Also there are two tokens:

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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