How can I force the user to register/login first before being redirected back to the current URL?
Redirect to the login page and provide a destination query parameter to the current route to redirect back after login:

  public function buildForm(array $form, FormStateInterface $form_state) {
    if ($this-&gt;currentUser()-&gt;isAnonymous()) {
      return $this-&gt;redirect(
          'user.login',
          [],
          ['query' =&gt; ['destination' =&gt; Url::fromRoute('&lt;current&gt;')-&gt;toString()]]
      );
    }

    // build form

    return $form
  }


Don't forget to add a use Drupal\Core\Url; statement to the class.