How can I force the user to register/login first before being redirected back to the current URL? I'm displaying a form using the Form API under a given URL. User gets this URL and accesses it. But before the user fills out the form, they need to be registered or logged in.

How can I send the user to the login page, or present the login form, then subsequently direct them back to the form under the given URL?

This is not a Webform -- I'm using the Form API.
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.