15530: Set homeCluster to LoginCluster
[arvados-workbench2.git] / src / store / auth / auth-reducer.ts
1 // Copyright (C) The Arvados Authors. All rights reserved.
2 //
3 // SPDX-License-Identifier: AGPL-3.0
4
5 import { authActions, AuthAction } from "./auth-action";
6 import { User, UserResource } from "~/models/user";
7 import { ServiceRepository } from "~/services/services";
8 import { SshKeyResource } from '~/models/ssh-key';
9 import { Session } from "~/models/session";
10 import { Config } from '~/common/config';
11
12 export interface AuthState {
13     user?: User;
14     apiToken?: string;
15     sshKeys: SshKeyResource[];
16     sessions: Session[];
17     localCluster: string;
18     homeCluster: string;
19     loginCluster: string;
20     remoteHosts: { [key: string]: string };
21     remoteHostsConfig: { [key: string]: Config };
22 }
23
24 const initialState: AuthState = {
25     user: undefined,
26     apiToken: undefined,
27     sshKeys: [],
28     sessions: [],
29     localCluster: "",
30     homeCluster: "",
31     loginCluster: "",
32     remoteHosts: {},
33     remoteHostsConfig: {}
34 };
35
36 export const authReducer = (services: ServiceRepository) => (state = initialState, action: AuthAction) => {
37     return authActions.match(action, {
38         SAVE_API_TOKEN: (token: string) => {
39             return { ...state, apiToken: token };
40         },
41         SAVE_USER: (user: UserResource) => {
42             return { ...state, user };
43         },
44         CONFIG: ({ config }) => {
45             return {
46                 ...state,
47                 localCluster: config.uuidPrefix,
48                 remoteHosts: { ...config.remoteHosts, [config.uuidPrefix]: new URL(config.rootUrl).host },
49                 homeCluster: config.loginCluster || config.uuidPrefix,
50                 loginCluster: config.loginCluster
51             };
52         },
53         REMOTE_CLUSTER_CONFIG: ({ config }) => {
54             return {
55                 ...state,
56                 remoteHostsConfig: { ...state.remoteHostsConfig, [config.uuidPrefix]: config },
57             };
58         },
59         INIT: ({ user, token }) => {
60             return { ...state, user, apiToken: token, homeCluster: user.uuid.substr(0, 5) };
61         },
62         LOGIN: () => {
63             return state;
64         },
65         LOGOUT: () => {
66             return { ...state, apiToken: undefined };
67         },
68         USER_DETAILS_SUCCESS: (user: User) => {
69             return { ...state, user };
70         },
71         SET_SSH_KEYS: (sshKeys: SshKeyResource[]) => {
72             return { ...state, sshKeys };
73         },
74         ADD_SSH_KEY: (sshKey: SshKeyResource) => {
75             return { ...state, sshKeys: state.sshKeys.concat(sshKey) };
76         },
77         REMOVE_SSH_KEY: (uuid: string) => {
78             return { ...state, sshKeys: state.sshKeys.filter((sshKey) => sshKey.uuid !== uuid) };
79         },
80         SET_HOME_CLUSTER: (homeCluster: string) => {
81             return { ...state, homeCluster };
82         },
83         SET_SESSIONS: (sessions: Session[]) => {
84             return { ...state, sessions };
85         },
86         ADD_SESSION: (session: Session) => {
87             return { ...state, sessions: state.sessions.concat(session) };
88         },
89         REMOVE_SESSION: (clusterId: string) => {
90             return {
91                 ...state,
92                 sessions: state.sessions.filter(
93                     session => session.clusterId !== clusterId
94                 )
95             };
96         },
97         UPDATE_SESSION: (session: Session) => {
98             return {
99                 ...state,
100                 sessions: state.sessions.map(
101                     s => s.clusterId === session.clusterId ? session : s
102                 )
103             };
104         },
105         default: () => state
106     });
107 };