15803: Move LOGOUT side effects to middleware
[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 } 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         CONFIG: ({ config }) => {
39             return {
40                 ...state,
41                 localCluster: config.uuidPrefix,
42                 remoteHosts: { ...config.remoteHosts, [config.uuidPrefix]: new URL(config.rootUrl).host },
43                 homeCluster: config.loginCluster || config.uuidPrefix,
44                 loginCluster: config.loginCluster,
45                 remoteHostsConfig: { ...state.remoteHostsConfig, [config.uuidPrefix]: config }
46             };
47         },
48         REMOTE_CLUSTER_CONFIG: ({ config }) => {
49             return {
50                 ...state,
51                 remoteHostsConfig: { ...state.remoteHostsConfig, [config.uuidPrefix]: config },
52             };
53         },
54         INIT: ({ user, token }) => {
55             return { ...state, user, apiToken: token, homeCluster: user.uuid.substr(0, 5) };
56         },
57         LOGIN: () => {
58             return state;
59         },
60         LOGOUT: () => {
61             return { ...state, apiToken: undefined };
62         },
63         USER_DETAILS_SUCCESS: (user: User) => {
64             return { ...state, user };
65         },
66         SET_SSH_KEYS: (sshKeys: SshKeyResource[]) => {
67             return { ...state, sshKeys };
68         },
69         ADD_SSH_KEY: (sshKey: SshKeyResource) => {
70             return { ...state, sshKeys: state.sshKeys.concat(sshKey) };
71         },
72         REMOVE_SSH_KEY: (uuid: string) => {
73             return { ...state, sshKeys: state.sshKeys.filter((sshKey) => sshKey.uuid !== uuid) };
74         },
75         SET_HOME_CLUSTER: (homeCluster: string) => {
76             return { ...state, homeCluster };
77         },
78         SET_SESSIONS: (sessions: Session[]) => {
79             return { ...state, sessions };
80         },
81         ADD_SESSION: (session: Session) => {
82             return { ...state, sessions: state.sessions.concat(session) };
83         },
84         REMOVE_SESSION: (clusterId: string) => {
85             return {
86                 ...state,
87                 sessions: state.sessions.filter(
88                     session => session.clusterId !== clusterId
89                 )
90             };
91         },
92         UPDATE_SESSION: (session: Session) => {
93             return {
94                 ...state,
95                 sessions: state.sessions.map(
96                     s => s.clusterId === session.clusterId ? session : s
97                 )
98             };
99         },
100         default: () => state
101     });
102 };