X-Git-Url: https://git.arvados.org/arvados-workbench2.git/blobdiff_plain/3ad7e33a2d50e9ed2298624ea5cac7cb5cd21a6f..a1e2b8ba77e4a7273940a3fc542bc42e282618a7:/src/store/auth/auth-reducer.ts diff --git a/src/store/auth/auth-reducer.ts b/src/store/auth/auth-reducer.ts index 57a17ae5..a8e4340a 100644 --- a/src/store/auth/auth-reducer.ts +++ b/src/store/auth/auth-reducer.ts @@ -2,54 +2,49 @@ // // SPDX-License-Identifier: AGPL-3.0 -import actions, { AuthAction } from "./auth-action"; -import { User } from "../../models/user"; -import { authService } from "../../services/services"; -import { removeServerApiAuthorizationHeader, setServerApiAuthorizationHeader } from "../../common/api/server-api"; -import { UserDetailsResponse } from "../../services/auth-service/auth-service"; +import { authActions, AuthAction } from "./auth-action"; +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[]; +} + +const initialState: AuthState = { + user: undefined, + apiToken: undefined, + sshKeys: [] }; -const authReducer = (state: AuthState = {}, action: AuthAction) => { - return actions.match(action, { +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: (ud: UserDetailsResponse) => { - const user = { - email: ud.email, - firstName: ud.first_name, - lastName: ud.last_name - }; - authService.saveUser(user); + USER_DETAILS_SUCCESS: (user: 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 }); }; - -export default authReducer;