Merge branch '16159-logout-request-with-token'
[arvados-workbench2.git] / src / services / auth-service / auth-service.ts
index 9fc738bc5d96549b6ea12de6859543772876a205..bd4bc1969c5004d1506f39b3d5a233d748d2fa57 100644 (file)
@@ -10,6 +10,7 @@ import { Session, SessionStatus } from "~/models/session";
 import { Config } from "~/common/config";
 import { uniqBy } from "lodash";
 
+export const TARGET_URL = 'targetURL';
 export const API_TOKEN_KEY = 'apiToken';
 export const USER_EMAIL_KEY = 'userEmail';
 export const USER_FIRST_NAME_KEY = 'userFirstName';
@@ -57,6 +58,14 @@ export class AuthService {
         }
     }
 
+    public removeTargetURL() {
+        this.getStorage().removeItem(TARGET_URL);
+    }
+
+    public getTargetURL() {
+        return this.getStorage().getItem(TARGET_URL);
+    }
+
     public removeApiToken() {
         this.getStorage().removeItem(API_TOKEN_KEY);
     }
@@ -69,6 +78,10 @@ export class AuthService {
         return this.getStorage().getItem(HOME_CLUSTER) || undefined;
     }
 
+    public getApiClient() {
+        return this.apiClient;
+    }
+
     public removeUser() {
         this.getStorage().removeItem(USER_EMAIL_KEY);
         this.getStorage().removeItem(USER_FIRST_NAME_KEY);
@@ -79,17 +92,20 @@ export class AuthService {
         this.getStorage().removeItem(USER_IS_ACTIVE);
         this.getStorage().removeItem(USER_USERNAME);
         this.getStorage().removeItem(USER_PREFS);
+        this.getStorage().removeItem(TARGET_URL);
     }
 
     public login(uuidPrefix: string, homeCluster: string, loginCluster: string, remoteHosts: { [key: string]: string }) {
         const currentUrl = `${window.location.protocol}//${window.location.host}/token`;
         const homeClusterHost = remoteHosts[homeCluster];
+        const rd = new URL(window.location.href);
+        this.getStorage().setItem(TARGET_URL, rd.pathname + rd.search);
         window.location.assign(`https://${homeClusterHost}/login?${(uuidPrefix !== homeCluster && homeCluster !== loginCluster) ? "remote=" + uuidPrefix + "&" : ""}return_to=${currentUrl}`);
     }
 
-    public logout() {
+    public logout(expireToken: string) {
         const currentUrl = `${window.location.protocol}//${window.location.host}`;
-        window.location.assign(`${this.baseUrl || ""}/logout?return_to=${currentUrl}`);
+        window.location.assign(`${this.baseUrl || ""}/logout?api_token=${expireToken}&return_to=${currentUrl}`);
     }
 
     public getUserDetails = (showErrors?: boolean): Promise<User> => {
@@ -141,8 +157,9 @@ export class AuthService {
             clusterId: cfg.uuidPrefix,
             remoteHost: cfg.rootUrl,
             baseUrl: cfg.baseUrl,
-            name: user ? getUserDisplayName(user): '',
+            name: user ? getUserDisplayName(user) : '',
             email: user ? user.email : '',
+            userIsActive: user ? user.isActive : false,
             token: this.getApiToken(),
             loggedIn: true,
             active: true,