Skip to content

Instantly share code, notes, and snippets.

@henideepak
Created January 8, 2022 09:44
Show Gist options
  • Save henideepak/2b6f9b3d066143fdfe389bc6bf7a6d70 to your computer and use it in GitHub Desktop.
Save henideepak/2b6f9b3d066143fdfe389bc6bf7a6d70 to your computer and use it in GitHub Desktop.
Drupal 9 one time login url
$account = User::load(\Drupal::currentUser()->id());
$timestamp = REQUEST_TIME;
$langcode = $options['langcode'] ?? $account->getPreferredLangcode();
$reset_url = Url::fromRoute('user.reset',
[
'uid' => $account->id(),
'timestamp' => $timestamp,
'hash' => user_pass_rehash($account, $timestamp),
],
[
'absolute' => TRUE,
'language' => \Drupal::languageManager()->getLanguage($langcode),
]
)->toString();
// calback function to get hash code.
function test_hash(UserInterface $account, $timestamp) {
$data = $timestamp;
$data .= $account->getLastLoginTime();
$data .= $account->id();
$data .= $account->getEmail();
return Crypt::hmacBase64($data, Settings::getHashSalt() . $account->getPassword());
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment