We are currently solving the following issue within our repository. Here is the issue text:
--- BEGIN 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.

--- END ISSUE ---

Below are some code segments, each from a relevant file. One or more of these files may contain bugs.
--- BEGIN FILES ---
Path: `evennia/web/utils/middleware.py`
Content:
```
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()
```

Path: `evennia/web/webclient/views.py`
Content:
```
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 FILES ---

Please first localize the bug based on the issue statement, and then generate a patch according to the `git diff` format fenced by three backticks.

Here is an example:

```diff
diff --git a/examples/server_async.py b/examples/server_async.py
--- a/examples/server_async.py
+++ b/examples/server_async.py
@@ -313,4 +313,4 @@
 
 
 if __name__ == "__main__":
-    asyncio.run(run_async_server("."), debug=True)
+    asyncio.run(run_async_server(), debug=True)
diff --git a/examples/server_sync.py b/examples/server_sync.py
--- a/examples/server_sync.py
+++ b/examples/server_sync.py
@@ -313,5 +313,5 @@
 
 
 if __name__ == "__main__":
-    server = run_sync_server(".")
+    server = run_sync_server()
     server.shutdown()

```

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}

{"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"}], "after_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        # 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        # 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 webclient(request):\n    \"\"\"\n    Webclient page template loading.\n\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\n    return render(request, 'webclient.html', pagevars)\n", "path": "evennia/web/webclient/views.py"}]}