Add auth service, getting api token, login/logout actions
[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
10 type AuthState = User | {};
11
12 const authReducer = (state: AuthState = {}, action: AuthAction) => {
13     switch (action.type) {
14         case getType(actions.saveApiToken): {
15             authService.saveApiToken(action.payload);
16             return {...state, apiToken: action.payload};
17         }
18         case getType(actions.login): {
19             authService.login();
20             return state;
21         }
22         case getType(actions.logout): {
23             authService.logout();
24             return {...state, apiToken: null };
25         }
26         default:
27             return state;
28     }
29 };
30
31 export default authReducer;