enhance web pages for flow where we request the user first
authorAlex Heneveld <alex.heneveld@cloudsoftcorp.com>
Mon, 28 Jan 2019 01:42:36 +0000 (01:42 +0000)
committerAlex Heneveld <alex.heneveld@cloudsoftcorp.com>
Mon, 28 Jan 2019 01:42:36 +0000 (01:42 +0000)
ui-modules/logout/app/views/main/main.controller.js
ui-modules/logout/app/views/main/main.template.html

index 8e554fb..357bb89 100644 (file)
@@ -31,7 +31,7 @@ export const mainState = {
     name: 'main',
     url: '/',
     template: require('ejs-html!./main.template.html'),
-    controller: ['$scope', mainStateController],
+    controller: ['$scope', '$http', mainStateController],
     controllerAs: 'vm'
 };
 
@@ -39,9 +39,61 @@ export function mainStateConfig($stateProvider) {
     $stateProvider.state(mainState);
 }
 
-export function mainStateController($scope) {
+export function mainStateController($scope, $http) {
+    $scope.state = { status: "checking", message: "Preparing to log out" };
     $scope.$emit(HIDE_INTERSTITIAL_SPINNER_EVENT);
 
+    function clearLocalCache() {
+        let ua = window.navigator.userAgent;
+        if (ua.indexOf('MSIE ') >= 0 || ua.indexOf(' Edge/') >= 0 || ua.indexOf(' Trident/') >= 0) {
+            document.execCommand('ClearAuthenticationCache', 'false');
+        }
+    }
+
+    function handleError(phase, response) {
+        if (response && response.status >= 300 && response.status < 500) {
+            // auth required
+            $scope.state = { status: "already-logged-out" };
+        } else if (response && response.status && response.status>0) {
+            console.log("Server failure "+phase, response);
+            $scope.state = { status: "failed", message: "server failure ("+response.status+") "+phase+
+                (response.message ? ": "+response.message : "") };
+        } else {
+            console.log("Connection failure "+phase, response);
+            $scope.state = { status: "failed", message: "connection failure "+phase };
+        }
+        clearLocalCache();
+    }
+        
+    function getUserThen(f) {
+        $http.get('v1/server/user').then(response => {
+            console.log("User check response", response);
+            $scope.state = { status: "logging-out", user: response.data };
+            f(response.data);
+            clearLocalCache();
+            
+        }, error => {
+            handleError("processing logged-on user check", error);
+        });
+    }
+    
+    function postLogout(user) {
+        console.log("posting to "+'v1/logout/'+user);
+        $http.post('v1/logout'+(user ? '/'+user : '')).then(response => {
+            console.log("Logout response", response);
+            $scope.state = { status: "just-logged-out" };
+            clearLocalCache();
+            
+        }, error => {
+            handleError("logging out", error);
+        });
+    }
+    
+    $scope.logout = () => getUserThen(postLogout);
+    $scope.logout();
+    
+    return;
+    
     let userRequest = new XMLHttpRequest();
     userRequest.onreadystatechange = function () {
         if (this.readyState === 4 && this.status === 200) {
@@ -51,15 +103,9 @@ export function mainStateController($scope) {
     userRequest.open('GET', '/v1/server/user', true);
     userRequest.send('');
 
-    /**
-     * Logout the supplied user
-     * @param user
-     */
-    function logout(user) {
-        let ua = window.navigator.userAgent;
-        if (ua.indexOf('MSIE ') >= 0 || ua.indexOf(' Edge/') >= 0 || ua.indexOf(' Trident/') >= 0) {
-            document.execCommand('ClearAuthenticationCache', 'false');
-        }
+    
+    function logout2(user) {
+        
         let logoutRequest = new XMLHttpRequest();
         logoutRequest.onreadystatechange = function () {
             if (this.readyState === 4) {
index 752ac93..fc12f8f 100644 (file)
 -->
 <div class="container-fluid">
     <div class="row">
-        <div class="col-md-12 text-center">
-            <h2>Thank you for using <%= getBrandedText('product.name') %>, see you soon</h2>
-            <p><a class="btn btn-lg btn-primary" href="/">Log back in</a></p>
+        <div class="col-md-12 text-center" ng-switch="state.status">
+            <div ng-switch-when="checking">
+                <h2>Logout in progress.../h2>
+                <p><a class="btn btn-lg btn-primary" ng-click="retry()">Retry</a></p>
+            </div>
+            <div ng-switch-when="logging-out">
+                <h2>Logout of {{ state.user }} in progress...</h2>
+                <p><a class="btn btn-lg btn-primary" ng-click="retry()">Retry</a></p>
+            </div>
+            <div ng-switch-when="just-logged-out">
+                <h2>Logout successful</h2>
+                <p>Thank you for using <%= getBrandedText('product.name') %>, see you soon</p>
+                <p><a class="btn btn-lg btn-primary" href="/">Log back in</a></p>
+                <p><a class="btn btn-lg btn-outline" ng-click="checkUnauthorized()">Clear local cached auth</a></p>
+            </div>
+            <div ng-switch-when="already-logged-out">
+                <h2>Already logged out</h2>
+                <p>Thank you for using <%= getBrandedText('product.name') %>, see you soon</p>
+                <p><a class="btn btn-lg btn-primary" href="/">Log back in</a></p>
+                <p><a class="btn btn-lg btn-outline" ng-click="checkUnauthorized()">Clear local cached auth</a></p>
+            </div>
+            <div ng-switch-default>
+                <h2>Log out failed<span ng-if="state.message">: {{ state.message }}</span</h2>
+                <p>
+                <p><a class="btn btn-lg btn-primary" ng-click="retry()">Try logging out again</a></p>
+                <p><a class="btn btn-lg btn-outline" href="/">Return to app (login if needed)</a></p>
+            </div>
         </div>
     </div>
 </div>