X-Git-Url: https://git.arvados.org/arvados-workbench2.git/blobdiff_plain/ec2ce1a965e15cb272737251b56bca2c1529ebff..56069f1f9fa111b9756e9bfa7a3b0c9dba2e9a7a:/src/store/auth/auth-action.ts diff --git a/src/store/auth/auth-action.ts b/src/store/auth/auth-action.ts index baf80595..c088418a 100644 --- a/src/store/auth/auth-action.ts +++ b/src/store/auth/auth-action.ts @@ -10,8 +10,10 @@ import { ServiceRepository } from "~/services/services"; import { SshKeyResource } from '~/models/ssh-key'; import { User } from "~/models/user"; import { Session } from "~/models/session"; -import { Config } from '~/common/config'; +import { getDiscoveryURL, Config } from '~/common/config'; import { initSessions } from "~/store/auth/auth-action-session"; +import Axios from "axios"; +import { AxiosError } from "axios"; export const authActions = unionize({ SAVE_API_TOKEN: ofType(), @@ -28,7 +30,8 @@ export const authActions = unionize({ SET_SESSIONS: ofType(), ADD_SESSION: ofType(), REMOVE_SESSION: ofType(), - UPDATE_SESSION: ofType() + UPDATE_SESSION: ofType(), + REMOTE_CLUSTER_CONFIG: ofType<{ config: Config }>(), }); function setAuthorizationHeader(services: ServiceRepository, token: string) { @@ -47,17 +50,30 @@ function removeAuthorizationHeader(client: AxiosInstance) { export const initAuth = (config: Config) => (dispatch: Dispatch, getState: () => RootState, services: ServiceRepository) => { const user = services.authService.getUser(); const token = services.authService.getApiToken(); + const homeCluster = services.authService.getHomeCluster(); if (token) { setAuthorizationHeader(services, token); } dispatch(authActions.CONFIG({ config })); + dispatch(authActions.SET_HOME_CLUSTER(homeCluster || config.uuidPrefix)); if (token && user) { dispatch(authActions.INIT({ user, token })); dispatch(initSessions(services.authService, config, user)); dispatch(getUserDetails()).then((user: User) => { dispatch(authActions.INIT({ user, token })); + }).catch((err: AxiosError) => { + if (err.response) { + // Bad token + if (err.response.status === 401) { + logout()(dispatch, getState, services); + } + } }); } + Object.keys(config.remoteHosts).map((k) => { + Axios.get(getDiscoveryURL(config.remoteHosts[k])) + .then(response => dispatch(authActions.REMOTE_CLUSTER_CONFIG({ config: response.data }))); + }); }; export const saveApiToken = (token: string) => (dispatch: Dispatch, getState: () => RootState, services: ServiceRepository) => { @@ -66,8 +82,8 @@ export const saveApiToken = (token: string) => (dispatch: Dispatch, getState: () dispatch(authActions.SAVE_API_TOKEN(token)); }; -export const login = (uuidPrefix: string, homeCluster: string) => (dispatch: Dispatch, getState: () => RootState, services: ServiceRepository) => { - services.authService.login(uuidPrefix, homeCluster); +export const login = (uuidPrefix: string, homeCluster: string, remoteHosts: { [key: string]: string }) => (dispatch: Dispatch, getState: () => RootState, services: ServiceRepository) => { + services.authService.login(uuidPrefix, homeCluster, remoteHosts); dispatch(authActions.LOGIN()); };