prompt: You will be provided with a partial code base and an issue statement explaining a problem to resolve.
  <issue>
  develop: Website not correctly logging in when logged in in-game
  #### Brief summary of issue / Description of requested feature:
  
  When logging in in-game, the website does not detect it, but if you try to log in you still get a warning saying that you are already logged in.
  
  #### Steps to reproduce the issue / Reasons for adding feature:
  
  1. Don't log into the website but open the web client
  2. Log into the webclient as usual.
  3. Go back to the website - you are not shown as logged in, but clicking `Log in` will still give you an error. 
  
  #### Error output / Expected result of feature
  
  When logged into the game, this should be reflected by the web site. See closed #1063. 
  
  #### Extra information, such as Evennia revision/repo/branch, operating system and ideas for how to solve / implement:
  
  This is a regression, probably from changes in the session handling/sharing between client and website.
  
  </issue>
  <code>
  [start of evennia/web/utils/middleware.py]
  1 from django.contrib.auth import authenticate, login
  2 from evennia.accounts.models import AccountDB
  3 from evennia.utils import logger
  4 
  5 class SharedLoginMiddleware(object):
  6     """
  7     Handle the shared login between website and webclient.
  8 
  9     """
  10     def __init__(self, get_response):
  11         # One-time configuration and initialization.
  12         self.get_response = get_response
  13         
  14     def __call__(self, request):
  15         # Code to be executed for each request before
  16         # the view (and later middleware) are called.
  17         
  18         # Process view
  19         response = self.get_response(request)
  20 
  21         # Code to be executed for each request/response after
  22         # the view is called.
  23         
  24         # Synchronize credentials
  25         self.make_shared_login(request)
  26         
  27         # Return processed view
  28         return response
  29         
  30     @classmethod
  31     def make_shared_login(cls, request):
  32         csession = request.session
  33         account = request.user
  34         website_uid = csession.get("website_authenticated_uid", None)
  35         webclient_uid = csession.get("webclient_authenticated_uid", None)
  36     
  37         if not csession.session_key:
  38             # this is necessary to build the sessid key
  39             csession.save()
  40     
  41         if account.is_authenticated():
  42             # Logged into website
  43             if not website_uid:
  44                 # fresh website login (just from login page)
  45                 csession["website_authenticated_uid"] = account.id
  46                 if webclient_uid is None:
  47                     # auto-login web client
  48                     csession["webclient_authenticated_uid"] = account.id
  49     
  50         elif webclient_uid:
  51             # Not logged into website, but logged into webclient
  52             if not website_uid:
  53                 csession["website_authenticated_uid"] = account.id
  54                 account = AccountDB.objects.get(id=webclient_uid)
  55                 try:
  56                     # calls our custom authenticate, in web/utils/backend.py
  57                     authenticate(autologin=account)
  58                     login(request, account)
  59                 except AttributeError:
  60                     logger.log_trace()
  [end of evennia/web/utils/middleware.py]
  [start of evennia/web/webclient/views.py]
  1 
  2 """
  3 This contains a simple view for rendering the webclient
  4 page and serve it eventual static content.
  5 
  6 """
  7 from __future__ import print_function
  8 from django.shortcuts import render
  9 from django.contrib.auth import login, authenticate
  10 
  11 from evennia.accounts.models import AccountDB
  12 from evennia.utils import logger
  13 
  14 
  15 def _shared_login(request):
  16     """
  17     Handle the shared login between website and webclient.
  18 
  19     """
  20     csession = request.session
  21     account = request.user
  22     # these can have 3 values:
  23     #   None - previously unused (auto-login)
  24     #   False - actively logged out (don't auto-login)
  25     #   <uid> - logged in User/Account id
  26     website_uid = csession.get("website_authenticated_uid", None)
  27     webclient_uid = csession.get("webclient_authenticated_uid", None)
  28 
  29     # check if user has authenticated to website
  30     if not csession.session_key:
  31         # this is necessary to build the sessid key
  32         csession.save()
  33 
  34     if webclient_uid:
  35         # The webclient has previously registered a login to this browser_session
  36         if not account.is_authenticated() and not website_uid:
  37             try:
  38                 account = AccountDB.objects.get(id=webclient_uid)
  39             except AccountDB.DoesNotExist:
  40                 # this can happen e.g. for guest accounts or deletions
  41                 csession["website_authenticated_uid"] = False
  42                 csession["webclient_authenticated_uid"] = False
  43                 return
  44             try:
  45                 # calls our custom authenticate in web/utils/backends.py
  46                 account = authenticate(autologin=account)
  47                 login(request, account)
  48                 csession["website_authenticated_uid"] = webclient_uid
  49             except AttributeError:
  50                 logger.log_trace()
  51 
  52 
  53 def webclient(request):
  54     """
  55     Webclient page template loading.
  56 
  57     """
  58     # handle webclient-website shared login
  59     _shared_login(request)
  60 
  61     # make sure to store the browser session's hash so the webclient can get to it!
  62     pagevars = {'browser_sessid': request.session.session_key}
  63 
  64     return render(request, 'webclient.html', pagevars)
  65
  [end of evennia/web/webclient/views.py]
  </code>
  
  I need you to solve this issue by generating a single patch file that I can apply directly to this repository using git apply. Please respond with a single patch file in the following format.
  <patch>
  diff --git a/file.py b/file.py
  --- a/file.py
  +++ b/file.py
  @@ -1,27 +1,35 @@
   def euclidean(a, b):
  -    while b:
  -        a, b = b, a % b
  -    return a
  +    if b == 0:
  +        return a
  +    return euclidean(b, a % b)
   
   
   def bresenham(x0, y0, x1, y1):
       points = []
       dx = abs(x1 - x0)
       dy = abs(y1 - y0)
  -    sx = 1 if x0 < x1 else -1
  -    sy = 1 if y0 < y1 else -1
  -    err = dx - dy
  +    x, y = x0, y0
  +    sx = -1 if x0 > x1 else 1
  +    sy = -1 if y0 > y1 else 1
   
  -    while True:
  -        points.append((x0, y0))
  -        if x0 == x1 and y0 == y1:
  -            break
  -        e2 = 2 * err
  -        if e2 > -dy:
  -            err -= dy
  -            x0 += sx
  -        if e2 < dx:
  -            err += dx
  -            y0 += sy
  +    if dx > dy:
  +        err = dx / 2.0
  +        while x != x1:
  +            points.append((x, y))
  +            err -= dy
  +            if err < 0:
  +                y += sy
  +                err += dx
  +            x += sx
  +    else:
  +        err = dy / 2.0
  +        while y != y1:
  +            points.append((x, y))
  +            err -= dx
  +            if err < 0:
  +                x += sx
  +                err += dy
  +            y += sy
  +
  +    points.append((x, y))
       return points
  
  </patch>
golden_diff: diff --git a/evennia/web/utils/middleware.py b/evennia/web/utils/middleware.py
  --- a/evennia/web/utils/middleware.py
  +++ b/evennia/web/utils/middleware.py
  @@ -15,15 +15,16 @@
           # Code to be executed for each request before
           # the view (and later middleware) are called.
           
  +        # Synchronize credentials between webclient and website
  +        # Must be performed *before* rendering the view (issue #1723)
  +        self.make_shared_login(request)
  +        
           # Process view
           response = self.get_response(request)
   
           # Code to be executed for each request/response after
           # the view is called.
           
  -        # Synchronize credentials
  -        self.make_shared_login(request)
  -        
           # Return processed view
           return response
           
  diff --git a/evennia/web/webclient/views.py b/evennia/web/webclient/views.py
  --- a/evennia/web/webclient/views.py
  +++ b/evennia/web/webclient/views.py
  @@ -12,52 +12,13 @@
   from evennia.utils import logger
   
   
  -def _shared_login(request):
  -    """
  -    Handle the shared login between website and webclient.
  -
  -    """
  -    csession = request.session
  -    account = request.user
  -    # these can have 3 values:
  -    #   None - previously unused (auto-login)
  -    #   False - actively logged out (don't auto-login)
  -    #   <uid> - logged in User/Account id
  -    website_uid = csession.get("website_authenticated_uid", None)
  -    webclient_uid = csession.get("webclient_authenticated_uid", None)
  -
  -    # check if user has authenticated to website
  -    if not csession.session_key:
  -        # this is necessary to build the sessid key
  -        csession.save()
  -
  -    if webclient_uid:
  -        # The webclient has previously registered a login to this browser_session
  -        if not account.is_authenticated() and not website_uid:
  -            try:
  -                account = AccountDB.objects.get(id=webclient_uid)
  -            except AccountDB.DoesNotExist:
  -                # this can happen e.g. for guest accounts or deletions
  -                csession["website_authenticated_uid"] = False
  -                csession["webclient_authenticated_uid"] = False
  -                return
  -            try:
  -                # calls our custom authenticate in web/utils/backends.py
  -                account = authenticate(autologin=account)
  -                login(request, account)
  -                csession["website_authenticated_uid"] = webclient_uid
  -            except AttributeError:
  -                logger.log_trace()
  -
  -
   def webclient(request):
       """
       Webclient page template loading.
   
       """
  -    # handle webclient-website shared login
  -    _shared_login(request)
  -
  +    # auto-login is now handled by evennia.web.utils.middleware
  +    
       # make sure to store the browser session's hash so the webclient can get to it!
       pagevars = {'browser_sessid': request.session.session_key}
  
verification_info: {"golden_diff": "diff --git a/evennia/web/utils/middleware.py b/evennia/web/utils/middleware.py\n--- a/evennia/web/utils/middleware.py\n+++ b/evennia/web/utils/middleware.py\n@@ -15,15 +15,16 @@\n         # Code to be executed for each request before\n         # the view (and later middleware) are called.\n         \n+        # Synchronize credentials between webclient and website\n+        # Must be performed *before* rendering the view (issue #1723)\n+        self.make_shared_login(request)\n+        \n         # Process view\n         response = self.get_response(request)\n \n         # Code to be executed for each request/response after\n         # the view is called.\n         \n-        # Synchronize credentials\n-        self.make_shared_login(request)\n-        \n         # Return processed view\n         return response\n         \ndiff --git a/evennia/web/webclient/views.py b/evennia/web/webclient/views.py\n--- a/evennia/web/webclient/views.py\n+++ b/evennia/web/webclient/views.py\n@@ -12,52 +12,13 @@\n from evennia.utils import logger\n \n \n-def _shared_login(request):\n-    \"\"\"\n-    Handle the shared login between website and webclient.\n-\n-    \"\"\"\n-    csession = request.session\n-    account = request.user\n-    # these can have 3 values:\n-    #   None - previously unused (auto-login)\n-    #   False - actively logged out (don't auto-login)\n-    #   <uid> - logged in User/Account id\n-    website_uid = csession.get(\"website_authenticated_uid\", None)\n-    webclient_uid = csession.get(\"webclient_authenticated_uid\", None)\n-\n-    # check if user has authenticated to website\n-    if not csession.session_key:\n-        # this is necessary to build the sessid key\n-        csession.save()\n-\n-    if webclient_uid:\n-        # The webclient has previously registered a login to this browser_session\n-        if not account.is_authenticated() and not website_uid:\n-            try:\n-                account = AccountDB.objects.get(id=webclient_uid)\n-            except AccountDB.DoesNotExist:\n-                # this can happen e.g. for guest accounts or deletions\n-                csession[\"website_authenticated_uid\"] = False\n-                csession[\"webclient_authenticated_uid\"] = False\n-                return\n-            try:\n-                # calls our custom authenticate in web/utils/backends.py\n-                account = authenticate(autologin=account)\n-                login(request, account)\n-                csession[\"website_authenticated_uid\"] = webclient_uid\n-            except AttributeError:\n-                logger.log_trace()\n-\n-\n def webclient(request):\n     \"\"\"\n     Webclient page template loading.\n \n     \"\"\"\n-    # handle webclient-website shared login\n-    _shared_login(request)\n-\n+    # auto-login is now handled by evennia.web.utils.middleware\n+    \n     # make sure to store the browser session's hash so the webclient can get to it!\n     pagevars = {'browser_sessid': request.session.session_key}\n", "issue": "develop: Website not correctly logging in when logged in in-game\n#### Brief summary of issue / Description of requested feature:\r\n\r\nWhen logging in in-game, the website does not detect it, but if you try to log in you still get a warning saying that you are already logged in.\r\n\r\n#### Steps to reproduce the issue / Reasons for adding feature:\r\n\r\n1. Don't log into the website but open the web client\r\n2. Log into the webclient as usual.\r\n3. Go back to the website - you are not shown as logged in, but clicking `Log in` will still give you an error. \r\n\r\n#### Error output / Expected result of feature\r\n\r\nWhen logged into the game, this should be reflected by the web site. See closed #1063. \r\n\r\n#### Extra information, such as Evennia revision/repo/branch, operating system and ideas for how to solve / implement:\r\n\r\nThis is a regression, probably from changes in the session handling/sharing between client and website.\n", "before_files": [{"content": "from django.contrib.auth import authenticate, login\nfrom evennia.accounts.models import AccountDB\nfrom evennia.utils import logger\n\nclass SharedLoginMiddleware(object):\n    \"\"\"\n    Handle the shared login between website and webclient.\n\n    \"\"\"\n    def __init__(self, get_response):\n        # One-time configuration and initialization.\n        self.get_response = get_response\n        \n    def __call__(self, request):\n        # Code to be executed for each request before\n        # the view (and later middleware) are called.\n        \n        # Process view\n        response = self.get_response(request)\n\n        # Code to be executed for each request/response after\n        # the view is called.\n        \n        # Synchronize credentials\n        self.make_shared_login(request)\n        \n        # Return processed view\n        return response\n        \n    @classmethod\n    def make_shared_login(cls, request):\n        csession = request.session\n        account = request.user\n        website_uid = csession.get(\"website_authenticated_uid\", None)\n        webclient_uid = csession.get(\"webclient_authenticated_uid\", None)\n    \n        if not csession.session_key:\n            # this is necessary to build the sessid key\n            csession.save()\n    \n        if account.is_authenticated():\n            # Logged into website\n            if not website_uid:\n                # fresh website login (just from login page)\n                csession[\"website_authenticated_uid\"] = account.id\n                if webclient_uid is None:\n                    # auto-login web client\n                    csession[\"webclient_authenticated_uid\"] = account.id\n    \n        elif webclient_uid:\n            # Not logged into website, but logged into webclient\n            if not website_uid:\n                csession[\"website_authenticated_uid\"] = account.id\n                account = AccountDB.objects.get(id=webclient_uid)\n                try:\n                    # calls our custom authenticate, in web/utils/backend.py\n                    authenticate(autologin=account)\n                    login(request, account)\n                except AttributeError:\n                    logger.log_trace()", "path": "evennia/web/utils/middleware.py"}, {"content": "\n\"\"\"\nThis contains a simple view for rendering the webclient\npage and serve it eventual static content.\n\n\"\"\"\nfrom __future__ import print_function\nfrom django.shortcuts import render\nfrom django.contrib.auth import login, authenticate\n\nfrom evennia.accounts.models import AccountDB\nfrom evennia.utils import logger\n\n\ndef _shared_login(request):\n    \"\"\"\n    Handle the shared login between website and webclient.\n\n    \"\"\"\n    csession = request.session\n    account = request.user\n    # these can have 3 values:\n    #   None - previously unused (auto-login)\n    #   False - actively logged out (don't auto-login)\n    #   <uid> - logged in User/Account id\n    website_uid = csession.get(\"website_authenticated_uid\", None)\n    webclient_uid = csession.get(\"webclient_authenticated_uid\", None)\n\n    # check if user has authenticated to website\n    if not csession.session_key:\n        # this is necessary to build the sessid key\n        csession.save()\n\n    if webclient_uid:\n        # The webclient has previously registered a login to this browser_session\n        if not account.is_authenticated() and not website_uid:\n            try:\n                account = AccountDB.objects.get(id=webclient_uid)\n            except AccountDB.DoesNotExist:\n                # this can happen e.g. for guest accounts or deletions\n                csession[\"website_authenticated_uid\"] = False\n                csession[\"webclient_authenticated_uid\"] = False\n                return\n            try:\n                # calls our custom authenticate in web/utils/backends.py\n                account = authenticate(autologin=account)\n                login(request, account)\n                csession[\"website_authenticated_uid\"] = webclient_uid\n            except AttributeError:\n                logger.log_trace()\n\n\ndef webclient(request):\n    \"\"\"\n    Webclient page template loading.\n\n    \"\"\"\n    # handle webclient-website shared login\n    _shared_login(request)\n\n    # make sure to store the browser session's hash so the webclient can get to it!\n    pagevars = {'browser_sessid': request.session.session_key}\n\n    return render(request, 'webclient.html', pagevars)\n", "path": "evennia/web/webclient/views.py"}]}