X-Git-Url: https://git.arvados.org/arvados-workbench2.git/blobdiff_plain/47e0dc87fa82bac593c53518e556ba7c55410288..785a62a8934dc439cbd201d9011775ccbcbb2c24:/src/store/auth/auth-reducer.ts diff --git a/src/store/auth/auth-reducer.ts b/src/store/auth/auth-reducer.ts index 366385d5..a8e4340a 100644 --- a/src/store/auth/auth-reducer.ts +++ b/src/store/auth/auth-reducer.ts @@ -3,45 +3,48 @@ // SPDX-License-Identifier: AGPL-3.0 import { authActions, AuthAction } from "./auth-action"; -import { User } from "../../models/user"; -import { authService } from "../../services/services"; -import { removeServerApiAuthorizationHeader, setServerApiAuthorizationHeader } from "../../common/api/server-api"; +import { User } from "~/models/user"; +import { ServiceRepository } from "~/services/services"; +import { SshKeyResource } from '~/models/ssh-key'; export interface AuthState { user?: User; apiToken?: string; + sshKeys: SshKeyResource[]; } -export const authReducer = (state: AuthState = {}, action: AuthAction) => { +const initialState: AuthState = { + user: undefined, + apiToken: undefined, + sshKeys: [] +}; + +export const authReducer = (services: ServiceRepository) => (state = initialState, action: AuthAction) => { return authActions.match(action, { SAVE_API_TOKEN: (token: string) => { - authService.saveApiToken(token); - setServerApiAuthorizationHeader(token); return {...state, apiToken: token}; }, - INIT: () => { - const user = authService.getUser(); - const token = authService.getApiToken(); - if (token) { - setServerApiAuthorizationHeader(token); - } - return {user, apiToken: token}; + INIT: ({ user, token }) => { + return { ...state, user, apiToken: token }; }, LOGIN: () => { - authService.login(); return state; }, LOGOUT: () => { - authService.removeApiToken(); - authService.removeUser(); - removeServerApiAuthorizationHeader(); - authService.logout(); return {...state, apiToken: undefined}; }, USER_DETAILS_SUCCESS: (user: User) => { - authService.saveUser(user); return {...state, user}; }, + SET_SSH_KEYS: (sshKeys: SshKeyResource[]) => { + return {...state, sshKeys}; + }, + ADD_SSH_KEY: (sshKey: SshKeyResource) => { + return { ...state, sshKeys: state.sshKeys.concat(sshKey) }; + }, + REMOVE_SSH_KEY: (uuid: string) => { + return { ...state, sshKeys: state.sshKeys.filter((sshKey) => sshKey.uuid !== uuid )}; + }, default: () => state }); };