I know, this seems like a basic question:

In Drupal 9 how do I redirect a user after logging in to a path/route that requires the UID in the path?

I've researched and tried different implementations but none of them support tokens (Rules, Redirect After Login, etc.)

For example, after a user logs in they should land on "/homebox-page/dashboard/UID" <- numerical value from current user ID.

I was hoping for a simple module to handler this but it looks like I may need to do it through custom code?

  • 1
    This sounds like an event subscriber that checks if the user is logged in, if not redirect them and include the destination parameter so they return there once they log in.
    – Kevin
    Jan 17, 2022 at 0:39
  • As you didn't list it explicitly: Login Destination seems to support tokens.
    – berliner
    Jan 17, 2022 at 2:13
  • Thank you. I think I'll go with custom code rather than Login Destinate which includes too much extra functionality that a few lines of code can handle. Jan 17, 2022 at 13:25

1 Answer 1


This should be possible with a few lines of custom code. The user login form redirects already to entity.user.canonical, which requires the UID:

  public function submitForm(array &$form, FormStateInterface $form_state) {

    if (empty($uid = $form_state->get('uid'))) {
    $account = $this->userStorage->load($uid);

    // A destination was set, probably on an exception controller.
    if (!$this->getRequest()->request->has('destination')) {
        ['user' => $account->id()]
    else {
      $this->getRequest()->query->set('destination', $this->getRequest()->request->get('destination'));


You could add your own submit handler doing the same, overwriting the redirect. There you can use \Drupal::currentUser() because user_login_finalize() in the core submit handler sets this service with the user just logged in.

  • Thank you! Yes, I actually ended up going the submit handler route from hook_form_alter() and it's working perfect. Jan 17, 2022 at 17:40

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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