How do I redirect to the login page? I have a custom search form which appears in the modal and is accessible only to logged in users.
The ajax url of the form is /my/custom/search/modal/{nojs}.
The router definition is the following one.
mymodule.my_custom_search_form:
  path: '/my/custom/search/modal/{nojs}'
  defaults:
    _form:  '\Drupal\mymodule\Form\MyCustomSearchForm'
    _title: 'My Custom search'
    nojs: ajax
  requirements:
    _user_is_logged_in: 'TRUE'

How do I redirect users to the login page when the anonymous user tries to access /my/custom/search/modal/{nojs} via an AJAX call?
There are a couple of modules that do this

Redirect 403 to Login: https://www.drupal.org/project/r4032login
Anonymous Login: https://www.drupal.org/project/anonymous_login
You could probably also do it with the Rules module
Simplest: You can also go to admin/config/system/site-information and set the &quot;Default 403 (access denied) page&quot; to &quot;/user/login&quot;

This page was old, but looked like it had been updated along the way for Drupal 8.