16848: Adds expiration date to the "Get API Token" dialog.
[arvados-workbench2.git] / src / store / auth / auth-reducer.ts
index d55e8301df50713625a1cf13861018779661b0db..7459b7ac0c3a651e239a9b560baebd795d1884e0 100644 (file)
@@ -12,7 +12,9 @@ import { Config, mockConfig } from '~/common/config';
 export interface AuthState {
     user?: User;
     apiToken?: string;
+    apiTokenExpiration?: Date;
     extraApiToken?: string;
+    extraApiTokenExpiration?: Date;
     sshKeys: SshKeyResource[];
     sessions: Session[];
     localCluster: string;
@@ -26,7 +28,9 @@ export interface AuthState {
 const initialState: AuthState = {
     user: undefined,
     apiToken: undefined,
+    apiTokenExpiration: undefined,
     extraApiToken: undefined,
+    extraApiTokenExpiration: undefined,
     sshKeys: [],
     sessions: [],
     localCluster: "",
@@ -39,70 +43,66 @@ const initialState: AuthState = {
 
 export const authReducer = (services: ServiceRepository) => (state = initialState, action: AuthAction) => {
     return authActions.match(action, {
-        SET_CONFIG: ({ config }) => {
-            return {
+        SET_CONFIG: ({ config }) =>
+            ({
                 ...state,
                 config,
                 localCluster: config.uuidPrefix,
-                remoteHosts: { ...config.remoteHosts, [config.uuidPrefix]: new URL(config.rootUrl).host },
+                remoteHosts: {
+                    ...config.remoteHosts,
+                    [config.uuidPrefix]: new URL(config.rootUrl).host
+                },
                 homeCluster: config.loginCluster || config.uuidPrefix,
                 loginCluster: config.loginCluster,
-                remoteHostsConfig: { ...state.remoteHostsConfig, [config.uuidPrefix]: config }
-            };
-        },
-        REMOTE_CLUSTER_CONFIG: ({ config }) => {
-            return {
+                remoteHostsConfig: {
+                    ...state.remoteHostsConfig,
+                    [config.uuidPrefix]: config
+                }
+            }),
+        REMOTE_CLUSTER_CONFIG: ({ config }) =>
+            ({
                 ...state,
-                remoteHostsConfig: { ...state.remoteHostsConfig, [config.uuidPrefix]: config },
-            };
-        },
-        SET_EXTRA_TOKEN: ({ extraToken }) => ({ ...state, extraApiToken: extraToken }),
-        INIT_USER: ({ user, token }) => {
-            return { ...state, user, apiToken: token, homeCluster: user.uuid.substr(0, 5) };
-        },
-        LOGIN: () => {
-            return state;
-        },
-        LOGOUT: () => {
-            return { ...state, apiToken: undefined };
-        },
-        USER_DETAILS_SUCCESS: (user: User) => {
-            return { ...state, user, homeCluster: user.uuid.substr(0, 5) };
-        },
-        SET_SSH_KEYS: (sshKeys: SshKeyResource[]) => {
-            return { ...state, sshKeys };
-        },
-        ADD_SSH_KEY: (sshKey: SshKeyResource) => {
-            return { ...state, sshKeys: state.sshKeys.concat(sshKey) };
-        },
-        REMOVE_SSH_KEY: (uuid: string) => {
-            return { ...state, sshKeys: state.sshKeys.filter((sshKey) => sshKey.uuid !== uuid) };
-        },
-        SET_HOME_CLUSTER: (homeCluster: string) => {
-            return { ...state, homeCluster };
-        },
-        SET_SESSIONS: (sessions: Session[]) => {
-            return { ...state, sessions };
-        },
-        ADD_SESSION: (session: Session) => {
-            return { ...state, sessions: state.sessions.concat(session) };
-        },
-        REMOVE_SESSION: (clusterId: string) => {
-            return {
+                remoteHostsConfig: {
+                    ...state.remoteHostsConfig,
+                    [config.uuidPrefix]: config
+                },
+            }),
+        SET_EXTRA_TOKEN: ({ extraApiToken, extraApiTokenExpiration }) =>
+            ({ ...state, extraApiToken, extraApiTokenExpiration }),
+        INIT_USER: ({ user, token, tokenExpiration }) =>
+            ({ ...state,
+                user,
+                apiToken: token,
+                apiTokenExpiration: tokenExpiration,
+                homeCluster: user.uuid.substr(0, 5)
+            }),
+        LOGIN: () => state,
+        LOGOUT: () => ({ ...state, apiToken: undefined }),
+        USER_DETAILS_SUCCESS: (user: User) =>
+            ({ ...state, user, homeCluster: user.uuid.substr(0, 5) }),
+        SET_SSH_KEYS: (sshKeys: SshKeyResource[]) => ({ ...state, sshKeys }),
+        ADD_SSH_KEY: (sshKey: SshKeyResource) =>
+            ({ ...state, sshKeys: state.sshKeys.concat(sshKey) }),
+        REMOVE_SSH_KEY: (uuid: string) =>
+            ({ ...state, sshKeys: state.sshKeys.filter((sshKey) => sshKey.uuid !== uuid) }),
+        SET_HOME_CLUSTER: (homeCluster: string) => ({ ...state, homeCluster }),
+        SET_SESSIONS: (sessions: Session[]) => ({ ...state, sessions }),
+        ADD_SESSION: (session: Session) =>
+            ({ ...state, sessions: state.sessions.concat(session) }),
+        REMOVE_SESSION: (clusterId: string) =>
+            ({
                 ...state,
                 sessions: state.sessions.filter(
                     session => session.clusterId !== clusterId
                 )
-            };
-        },
-        UPDATE_SESSION: (session: Session) => {
-            return {
+            }),
+        UPDATE_SESSION: (session: Session) =>
+            ({
                 ...state,
                 sessions: state.sessions.map(
                     s => s.clusterId === session.clusterId ? session : s
                 )
-            };
-        },
+            }),
         default: () => state
     });
 };