1 // Copyright (C) The Arvados Authors. All rights reserved.
3 // SPDX-License-Identifier: AGPL-3.0
5 import { Middleware } from "redux";
6 import { authActions, } from "./auth-action";
7 import { ServiceRepository, setAuthorizationHeader, removeAuthorizationHeader } from "~/services/services";
8 import { initSessions } from "~/store/auth/auth-action-session";
9 import { User } from "~/models/user";
10 import { RootState } from '~/store/store';
12 export const authMiddleware = (services: ServiceRepository): Middleware => store => next => action => {
13 authActions.match(action, {
14 INIT: ({ user, token }) => {
16 const state: RootState = store.getState();
17 if (state.auth.user) {
18 services.authService.saveUser(state.auth.user);
20 services.authService.removeUser();
22 if (state.auth.apiToken) {
23 services.authService.saveApiToken(state.auth.apiToken);
24 setAuthorizationHeader(services, state.auth.apiToken);
26 services.authService.removeApiToken();
27 removeAuthorizationHeader(services);
30 store.dispatch<any>(initSessions(services.authService, state.auth.remoteHostsConfig[state.auth.localCluster], user));
32 services.userService.activate(user.uuid).then((user: User) => {
33 store.getState().dispatch(authActions.INIT({ user, token }));
37 CONFIG: ({ config }) => {
38 document.title = `Arvados Workbench (${config.uuidPrefix})`;
41 default: () => next(action)