Repository: evennia/evennia

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.


Discussion:
@strikaco Any ideas on this one off the cuff? 
Hmm. Looking at the webclient view, I notice it copy/pastes the same
shared_login function that was implemented on the website. So off the cuff
I'm wondering if this is causing some sort of conflict with the middleware.

I'll take a look today.

On Oct 28, 2018 10:15 AM, "Griatch" <notifications@github.com> wrote:

> @strikaco <https://github.com/strikaco> Any ideas on this one off the
> cuff?
>
> —
> You are receiving this because you were mentioned.
> Reply to this email directly, view it on GitHub
> <https://github.com/evennia/evennia/issues/1723#issuecomment-433723659>,
> or mute the thread
> <https://github.com/notifications/unsubscribe-auth/AHyB9I1KTu1TMqeLUy0kOZh5UyFptnj_ks5upeYfgaJpZM4X-Em0>
> .
>

Ok, so while there is duplicated code and both methods are being executed unnecessarily, the reason for this issue is likely my getting the order of operations wrong when calling the middleware-- I need to formalize testing but I think make_shared_login must be called before rendering the view.

Resolving patch:
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 @@ def __call__(self, request):
         # 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}
 


Test patch: