1 // Copyright (C) The Arvados Authors. All rights reserved.
3 // SPDX-License-Identifier: AGPL-3.0
5 import { getType } from "typesafe-actions";
6 import actions, { AuthAction } from "./auth-action";
7 import { User } from "../models/user";
8 import { authService } from "../services/services";
9 import { removeServerApiAuthorizationHeader, serverApi, setServerApiAuthorizationHeader } from "../common/server-api";
11 type AuthState = User | {};
13 const authReducer = (state: AuthState = {}, action: AuthAction) => {
14 switch (action.type) {
15 case getType(actions.saveApiToken): {
16 authService.saveApiToken(action.payload);
17 setServerApiAuthorizationHeader(action.payload);
18 serverApi.get('/users/current');
19 return {...state, apiToken: action.payload};
21 case getType(actions.login): {
25 case getType(actions.logout): {
26 authService.removeApiToken();
27 removeServerApiAuthorizationHeader();
29 return {...state, apiToken: null };
36 export default authReducer;