1 // Copyright (C) The Arvados Authors. All rights reserved.
3 // SPDX-License-Identifier: AGPL-3.0
5 import actions, { AuthAction, UserDetailsResponse } from "./auth-action";
6 import { User } from "../../models/user";
7 import { authService } from "../../services/services";
8 import { removeServerApiAuthorizationHeader, setServerApiAuthorizationHeader } from "../../common/server-api";
10 export interface AuthState {
15 const authReducer = (state: AuthState = {}, action: AuthAction) => {
16 return actions.match(action, {
17 SAVE_API_TOKEN: (token: string) => {
18 authService.saveApiToken(token);
19 setServerApiAuthorizationHeader(token);
20 return {...state, apiToken: token};
23 const user = authService.getUser();
24 const token = authService.getApiToken();
25 return {user, apiToken: token};
32 authService.removeApiToken();
33 authService.removeUser();
34 removeServerApiAuthorizationHeader();
36 return {...state, apiToken: undefined};
38 USER_DETAILS_SUCCESS: (ud: UserDetailsResponse) => {
41 firstName: ud.first_name,
42 lastName: ud.last_name
44 authService.saveUser(user);
45 return {...state, user};
51 export default authReducer;