Merge branch 'master' into 14452-my-account
[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
10 export interface AuthState {
11     user?: User;
12     apiToken?: string;
13     sshKeys: SshKeyResource[];
14 }
15
16 const initialState: AuthState = {
17     user: undefined,
18     apiToken: undefined,
19     sshKeys: []
20 };
21
22 export const authReducer = (services: ServiceRepository) => (state = initialState, action: AuthAction) => {
23     return authActions.match(action, {
24         SAVE_API_TOKEN: (token: string) => {
25             return {...state, apiToken: token};
26         },
27         INIT: ({ user, token }) => {
28             return { ...state, user, apiToken: token };
29         },
30         LOGIN: () => {
31             return state;
32         },
33         LOGOUT: () => {
34             return {...state, apiToken: undefined};
35         },
36         USER_DETAILS_SUCCESS: (user: User) => {
37             return {...state, user};
38         },
39         SET_SSH_KEYS: (sshKeys: SshKeyResource[]) => {
40             return {...state, sshKeys};
41         },
42         ADD_SSH_KEY: (sshKey: SshKeyResource) => {
43             return { ...state, sshKeys: state.sshKeys.concat(sshKey) };
44         },
45         REMOVE_SSH_KEY: (uuid: string) => {
46             return { ...state, sshKeys: state.sshKeys.filter((sshKey) => sshKey.uuid !== uuid )};
47         },
48         default: () => state
49     });
50 };