title: Multiple `login` endpoints for `Flask-User`?
body: <p>I'm working on a Flask app that needs user management. However, I have multiple parts of my site, let's call them <code>/site/&lt;name&gt;/</code> with each of them having their own users (linked in the DB to <code>name</code>).</p>
  
  <p>So, what I want to achieve is:</p>
  
  <ol>
  <li>Any unauthorised access to anything <code>/site/&lt;name&gt;/&lt;path&gt;</code> gets redirected to <code>/site/&lt;name&gt;/login</code> (so, I cannot simply redefine <code>login_view_function</code>).</li>
  <li>When the user logs in, the argument <code>name</code> is also used (so, a user from <code>/site/name1/</code> cannot log in at <code>/site/name2/</code>).</li>
  <li>A login from <code>/site/name1/login</code> works inside <code>/site/name1/&lt;path&gt;</code>, but not in <code>/site/name2/&lt;path&gt;</code> (the user is unauthorised there; can also get logged out of <code>name1</code>... I don't particularly care what exactly happens).</li>
  <li>Whatever happens outside of <code>/site/&lt;name&gt;/</code> (or for invalid <code>name</code>) is irrelevant for now.</li>
  </ol>
  
  <p>I tried redefining URL rules in a <code>before_request</code> decorated function:</p>
  
  <pre class="lang-py prettyprint-override"><code>site_manager = UserManager(db_adapter, app)
  ...
  @app.before_request
  def config_flask_user():
      m = re.match(r'https?://[^/]+/site/(?P&lt;name&gt;[^:/]*)', request.url)
      if m:
          name = m.group('name')
          flask.g.app_theme = name
          if not name_ok(name):
              flask.abort(404)
          # Make a no-arguments node for the current site
          app.debug = False
          app.add_url_rule(
              '/site/%s/' % name,
              'site.current',
              site,
              methods=['GET', 'POST'],
              defaults={'name': name},
          )
          app.debug = DEBUG
          # Use it as the post-logout endpoint, so that it leads to a page
          # where re-login works properly
          site_manager.after_logout_endpoint = 'site.current'
          # Override existing `login_url` and `logout_url` routes, to fit
          # the current site path
          for action in ('login', 'logout'):
              rule = '/site/%s/%s/' % (name, action)
              endpoint = getattr(site_manager, action + "_url")
              view_func = getattr(site_manager, action + "_view_function")
              for r in app.url_map._rules:
                  if r.rule == endpoint:
                      print("Redefining rule", endpoint, "as", rule)
                      r.rule = rule
                      r.refresh()
  </code></pre>
  
  <p>This seemed to work fine, but it actually breaks if the server is restarted between loading the Login page and actually submitting user's login and password, seemingly due to that spot being too late to redefine these paths, and I'm worried that it's the wrong approach (maybe it can cause collisions between different requests to different "sites"?), especially for the intended multi-worker environment.</p>
  
  <p><strong>TL;DR:</strong> How to get <code>/site/&lt;name&gt;/</code> paths to have <code>Flask-User</code> login dependent on the <code>name</code> argument?</p>