From 9326dfa5f12e796b0c80eef214d7fd1205d5bbdc Mon Sep 17 00:00:00 2001
From: Christoph Reiter <reiter.christoph@gmail.com>
Date: Thu, 5 Dec 2019 11:43:56 +0100
Subject: [PATCH] Send a logging-out event when transitioning from logged-in to
 logged-out

This can happen when the session expires, for example if the user has logged
out in another window.

This is useful whenn you want to use "logging-out" to clear user related data.
---
 packages/auth/src/vpu-auth.js | 9 ++++++++-
 1 file changed, 8 insertions(+), 1 deletion(-)

diff --git a/packages/auth/src/vpu-auth.js b/packages/auth/src/vpu-auth.js
index aa1e549a..5078d714 100644
--- a/packages/auth/src/vpu-auth.js
+++ b/packages/auth/src/vpu-auth.js
@@ -85,6 +85,9 @@ class VPUAuth extends VPULitElement {
             this.personId = personId;
             this._setLoginStatus(LoginStatus.LOGGED_IN, tokenChanged);
         } else {
+            if (this._loginStatus === LoginStatus.LOGGED_IN) {
+                this._setLoginStatus(LoginStatus.LOGGING_OUT);
+            }
             this.name = "";
             this.token = "";
             this.subject = "";
@@ -217,7 +220,11 @@ class VPUAuth extends VPULitElement {
     }
 
     onLogoutClicked(e) {
-        this._setLoginStatus(LoginStatus.LOGGING_OUT);
+        // Keycloak will redirect right away without emitting events, so we have
+        // to do this manually here
+        if (this._loginStatus === LoginStatus.LOGGED_IN) {
+            this._setLoginStatus(LoginStatus.LOGGING_OUT);
+        }
         this._kcwrapper.logout();
     }
 
-- 
GitLab