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";
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 SAVE_USER: (user: UserResource) => {
37 return { ...state, user};
39 CONFIG: ({ config }) => {
42 localCluster: config.uuidPrefix,
43 remoteHosts: { ...config.remoteHosts, [config.uuidPrefix]: new URL(config.rootUrl).host },
44 homeCluster: config.uuidPrefix
47 INIT: ({ user, token }) => {
48 return { ...state, user, apiToken: token, homeCluster: user.uuid.substr(0, 5) };
54 return { ...state, apiToken: undefined };
56 USER_DETAILS_SUCCESS: (user: User) => {
57 return { ...state, user };
59 SET_SSH_KEYS: (sshKeys: SshKeyResource[]) => {
60 return { ...state, sshKeys };
62 ADD_SSH_KEY: (sshKey: SshKeyResource) => {
63 return { ...state, sshKeys: state.sshKeys.concat(sshKey) };
65 REMOVE_SSH_KEY: (uuid: string) => {
66 return { ...state, sshKeys: state.sshKeys.filter((sshKey) => sshKey.uuid !== uuid) };
68 SET_HOME_CLUSTER: (homeCluster: string) => {
69 return { ...state, homeCluster };
71 SET_SESSIONS: (sessions: Session[]) => {
72 return { ...state, sessions };
74 ADD_SESSION: (session: Session) => {
75 return { ...state, sessions: state.sessions.concat(session) };
77 REMOVE_SESSION: (clusterId: string) => {
80 sessions: state.sessions.filter(
81 session => session.clusterId !== clusterId
85 UPDATE_SESSION: (session: Session) => {
88 sessions: state.sessions.map(
89 s => s.clusterId === session.clusterId ? session : s