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/api/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();
26 setServerApiAuthorizationHeader(token);
28 return {user, apiToken: token};
35 authService.removeApiToken();
36 authService.removeUser();
37 removeServerApiAuthorizationHeader();
39 return {...state, apiToken: undefined};
41 USER_DETAILS_SUCCESS: (user: User) => {
42 authService.saveUser(user);
43 return {...state, user};
49 export default authReducer;