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, UserResource } 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 SAVE_USER: (user: UserResource) => {
40 return { ...state, user};
42 CONFIG: ({ config }) => {
45 localCluster: config.uuidPrefix,
46 remoteHosts: { ...config.remoteHosts, [config.uuidPrefix]: new URL(config.rootUrl).host },
47 homeCluster: config.uuidPrefix
50 REMOTE_CLUSTER_CONFIG: ({ config }) => {
53 remoteHostsConfig: { ...state.remoteHostsConfig, [config.uuidPrefix]: config },
56 INIT: ({ user, token }) => {
57 return { ...state, user, apiToken: token, homeCluster: user.uuid.substr(0, 5) };
63 return { ...state, apiToken: undefined };
65 USER_DETAILS_SUCCESS: (user: User) => {
66 return { ...state, user };
68 SET_SSH_KEYS: (sshKeys: SshKeyResource[]) => {
69 return { ...state, sshKeys };
71 ADD_SSH_KEY: (sshKey: SshKeyResource) => {
72 return { ...state, sshKeys: state.sshKeys.concat(sshKey) };
74 REMOVE_SSH_KEY: (uuid: string) => {
75 return { ...state, sshKeys: state.sshKeys.filter((sshKey) => sshKey.uuid !== uuid) };
77 SET_HOME_CLUSTER: (homeCluster: string) => {
78 return { ...state, homeCluster };
80 SET_SESSIONS: (sessions: Session[]) => {
81 return { ...state, sessions };
83 ADD_SESSION: (session: Session) => {
84 return { ...state, sessions: state.sessions.concat(session) };
86 REMOVE_SESSION: (clusterId: string) => {
89 sessions: state.sessions.filter(
90 session => session.clusterId !== clusterId
94 UPDATE_SESSION: (session: Session) => {
97 sessions: state.sessions.map(
98 s => s.clusterId === session.clusterId ? session : s