Add server api instance
[arvados.git] / src / store / auth-reducer.ts
1 // Copyright (C) The Arvados Authors. All rights reserved.
2 //
3 // SPDX-License-Identifier: AGPL-3.0
4
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";
10
11 type AuthState = User | {};
12
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};
20         }
21         case getType(actions.login): {
22             authService.login();
23             return state;
24         }
25         case getType(actions.logout): {
26             authService.removeApiToken();
27             removeServerApiAuthorizationHeader();
28             authService.logout();
29             return {...state, apiToken: null };
30         }
31         default:
32             return state;
33     }
34 };
35
36 export default authReducer;