refs #13563 Merge branch '13563-login-service'
[arvados-workbench2.git] / src / store / auth / auth-action.ts
1 // Copyright (C) The Arvados Authors. All rights reserved.
2 //
3 // SPDX-License-Identifier: AGPL-3.0
4
5 import { serverApi } from "../../common/server-api";
6 import { ofType, default as unionize, UnionOf } from "unionize";
7 import { Dispatch } from "redux";
8
9 export interface UserDetailsResponse {
10     email: string;
11     first_name: string;
12     last_name: string;
13     is_admin: boolean;
14 }
15
16 const actions = unionize({
17     SAVE_API_TOKEN: ofType<string>(),
18     LOGIN: {},
19     LOGOUT: {},
20     INIT: {},
21     USER_DETAILS_REQUEST: {},
22     USER_DETAILS_SUCCESS: ofType<UserDetailsResponse>()
23 }, {
24     tag: 'type',
25     value: 'payload'
26 });
27
28 export type AuthAction = UnionOf<typeof actions>;
29 export default actions;
30
31 export const getUserDetails = () => (dispatch: Dispatch) => {
32     dispatch(actions.USER_DETAILS_REQUEST());
33     serverApi
34         .get<UserDetailsResponse>('/users/current')
35         .then(resp => {
36             dispatch(actions.USER_DETAILS_SUCCESS(resp.data));
37         })
38         // .catch(err => {
39         // });
40 };
41
42