1 // Copyright (C) The Arvados Authors. All rights reserved.
3 // SPDX-License-Identifier: AGPL-3.0
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';
12 export interface AuthState {
15 sshKeys: SshKeyResource[];
19 remoteHosts: { [key: string]: string };
20 remoteHostsConfig: { [key: string]: Config };
23 const initialState: AuthState = {
34 export const authReducer = (services: ServiceRepository) => (state = initialState, action: AuthAction) => {
35 return authActions.match(action, {
36 SAVE_API_TOKEN: (token: string) => {
37 return { ...state, apiToken: token };
39 CONFIG: ({ config }) => {
42 localCluster: config.uuidPrefix,
43 remoteHosts: { ...config.remoteHosts, [config.uuidPrefix]: new URL(config.rootUrl).host },
44 homeCluster: config.uuidPrefix
47 REMOTE_CLUSTER_CONFIG: ({ config }) => {
50 remoteHostsConfig: { ...state.remoteHostsConfig, [config.uuidPrefix]: config },
53 INIT: ({ user, token }) => {
54 return { ...state, user, apiToken: token, homeCluster: user.uuid.substr(0, 5) };
60 return { ...state, apiToken: undefined };
62 USER_DETAILS_SUCCESS: (user: User) => {
63 return { ...state, user };
65 SET_SSH_KEYS: (sshKeys: SshKeyResource[]) => {
66 return { ...state, sshKeys };
68 ADD_SSH_KEY: (sshKey: SshKeyResource) => {
69 return { ...state, sshKeys: state.sshKeys.concat(sshKey) };
71 REMOVE_SSH_KEY: (uuid: string) => {
72 return { ...state, sshKeys: state.sshKeys.filter((sshKey) => sshKey.uuid !== uuid) };
74 SET_HOME_CLUSTER: (homeCluster: string) => {
75 return { ...state, homeCluster };
77 SET_SESSIONS: (sessions: Session[]) => {
78 return { ...state, sessions };
80 ADD_SESSION: (session: Session) => {
81 return { ...state, sessions: state.sessions.concat(session) };
83 REMOVE_SESSION: (clusterId: string) => {
86 sessions: state.sessions.filter(
87 session => session.clusterId !== clusterId
91 UPDATE_SESSION: (session: Session) => {
94 sessions: state.sessions.map(
95 s => s.clusterId === session.clusterId ? session : s