14720: Better login button, tell you what cluster you're logging in to.
[arvados.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
11 export interface AuthState {
12     user?: User;
13     apiToken?: string;
14     sshKeys: SshKeyResource[];
15     sessions: Session[];
16     localCluster: string;
17     homeCluster: string;
18     remoteHosts: { [key: string]: string };
19 }
20
21 const initialState: AuthState = {
22     user: undefined,
23     apiToken: undefined,
24     sshKeys: [],
25     sessions: [],
26     localCluster: "",
27     homeCluster: "",
28     remoteHosts: {}
29 };
30
31 export const authReducer = (services: ServiceRepository) => (state = initialState, action: AuthAction) => {
32     return authActions.match(action, {
33         SAVE_API_TOKEN: (token: string) => {
34             return { ...state, apiToken: token };
35         },
36         CONFIG: ({ uuidPrefix, remoteHosts }) => {
37             return {
38                 ...state, localCluster: uuidPrefix, remoteHosts, homeCluster: uuidPrefix
39             };
40         },
41         INIT: ({ user, token }) => {
42             return { ...state, user, apiToken: token, homeCluster: user.uuid.substr(0, 5) };
43         },
44         LOGIN: () => {
45             return state;
46         },
47         LOGOUT: () => {
48             return { ...state, apiToken: undefined };
49         },
50         USER_DETAILS_SUCCESS: (user: User) => {
51             return { ...state, user };
52         },
53         SET_SSH_KEYS: (sshKeys: SshKeyResource[]) => {
54             return { ...state, sshKeys };
55         },
56         ADD_SSH_KEY: (sshKey: SshKeyResource) => {
57             return { ...state, sshKeys: state.sshKeys.concat(sshKey) };
58         },
59         REMOVE_SSH_KEY: (uuid: string) => {
60             return { ...state, sshKeys: state.sshKeys.filter((sshKey) => sshKey.uuid !== uuid) };
61         },
62         SET_HOME_CLUSTER: (homeCluster: string) => {
63             return { ...state, homeCluster };
64         },
65         SET_SESSIONS: (sessions: Session[]) => {
66             return { ...state, sessions };
67         },
68         ADD_SESSION: (session: Session) => {
69             return { ...state, sessions: state.sessions.concat(session) };
70         },
71         REMOVE_SESSION: (clusterId: string) => {
72             return {
73                 ...state,
74                 sessions: state.sessions.filter(
75                     session => session.clusterId !== clusterId
76                 )
77             };
78         },
79         UPDATE_SESSION: (session: Session) => {
80             return {
81                 ...state,
82                 sessions: state.sessions.map(
83                     s => s.clusterId === session.clusterId ? session : s
84                 )
85             };
86         },
87         default: () => state
88     });
89 };