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";
11 export interface AuthState {
14 sshKeys: SshKeyResource[];
18 remoteHosts: { [key: string]: string };
21 const initialState: AuthState = {
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 };
36 CONFIG: ({ config }) => {
39 localCluster: config.uuidPrefix,
40 remoteHosts: { ...config.remoteHosts, [config.uuidPrefix]: new URL(config.rootUrl).host },
41 homeCluster: config.uuidPrefix
44 INIT: ({ user, token }) => {
45 return { ...state, user, apiToken: token, homeCluster: user.uuid.substr(0, 5) };
51 return { ...state, apiToken: undefined };
53 USER_DETAILS_SUCCESS: (user: User) => {
54 return { ...state, user };
56 SET_SSH_KEYS: (sshKeys: SshKeyResource[]) => {
57 return { ...state, sshKeys };
59 ADD_SSH_KEY: (sshKey: SshKeyResource) => {
60 return { ...state, sshKeys: state.sshKeys.concat(sshKey) };
62 REMOVE_SSH_KEY: (uuid: string) => {
63 return { ...state, sshKeys: state.sshKeys.filter((sshKey) => sshKey.uuid !== uuid) };
65 SET_HOME_CLUSTER: (homeCluster: string) => {
66 return { ...state, homeCluster };
68 SET_SESSIONS: (sessions: Session[]) => {
69 return { ...state, sessions };
71 ADD_SESSION: (session: Session) => {
72 return { ...state, sessions: state.sessions.concat(session) };
74 REMOVE_SESSION: (clusterId: string) => {
77 sessions: state.sessions.filter(
78 session => session.clusterId !== clusterId
82 UPDATE_SESSION: (session: Session) => {
85 sessions: state.sessions.map(
86 s => s.clusterId === session.clusterId ? session : s