title: Multiple Django apps, shared authentication
body: <p><b>Two answers to this question, depending on whether sharing is across different sites or different subdomains </b> Second answer: <a href="https://stackoverflow.com/questions/18230202/multiple-django-apps-shared-authentication/18248640#18248640">Multiple Django apps, shared authentication</a></p>
  
  <p>A user goes to site1.com and logs in. Now, if he goes to site2.com, then he should already be logged in (authenticated) at that site. </p>
  
  <p>site1.com and site2.com are handled by different Django apps on the same sever.</p>
  
  <p>I get that the sites can share the database containing the authentication tables. What I don't get is how the session data is handled. After logging in to site1, the user goes to site2. Here he always has request.user = "AnonymousUser: AnonymousUser" instead of a user_id. </p>
  
  <p>I have set it up as here: <a href="https://docs.djangoproject.com/en/dev/topics/db/multi-db/" rel="noreferrer">https://docs.djangoproject.com/en/dev/topics/db/multi-db/</a> :</p>
  
  <p>site1's settings has a single database that contains the auth models as well as some other data tables. site2's settings has 2 databases. One with its own data tables and also the one used by user1. I essentially copied class AuthRouter and set up the database router.</p>
  
  <p>Is it not possible what I am trying to do? I don't actually understand how the two sites can share session data.  Do I need something special outside of Django? Or should this work? I can include my code here, but don't want to confuse the issue if my basic thinking about this is wrong. </p>
  
  <p>EDIT: here is my setup. I am trying this on localhost.</p>
  
  <p>Site1 is running on localhost:8080</p>
  
  <p>site2 is running on localhost:8000</p>
  
  <p>SITE2 APP:</p>
  
  <p>db_router.py:</p>
  
  <pre><code>class AuthRouter(object):
      def db_for_read(self, model, **hints):
          if model._meta.app_label == 'auth':
              return 'the_ui'
          return None
      # same for write
  
      def allow_syncdb(self, db, model):
          if db == 'the_ui':
              return model._meta.app_label == 'auth'
          elif model._meta.app_label == 'auth':
              return False
          return None
  
  class OtherRouter(object):
      def db_for_read(self, model, **hints):
          return "default"
      # same for write, relation, syncdb
  </code></pre>
  
  <p>settings.py:</p>
  
  <pre><code>DATABASE_ROUTERS = ['site2_app.db_router.AuthRouter', 'site2_app.db_router.OtherRouter']
  SESSION_COOKIE_DOMAIN = 'http://localhost:8080'
  SESSION_ENGINE = "django.contrib.sessions.backends.signed_cookies"
  
  DATABASES = {
      'default': {
          # ...
      },
      'the_ui': {
          # ...
      }
  }
  </code></pre>
  
  <p>SITE 1 APP:</p>
  
  <pre><code># no router
  # only single database, same as the "the_ui" used in site2
  SESSION_ENGINE = "django.contrib.sessions.backends.signed_cookies"
  </code></pre>