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