Replace typesafe-actions with unionize, add fetching user details action
[arvados-workbench2.git] / src / services / auth-service / auth-service.ts
1 // Copyright (C) The Arvados Authors. All rights reserved.
2 //
3 // SPDX-License-Identifier: AGPL-3.0
4
5 import Axios from "axios";
6 import { API_HOST } from "../../common/server-api";
7
8 const API_TOKEN_KEY = 'api_token';
9
10 export default class AuthService {
11
12     public saveApiToken(token: string) {
13         localStorage.setItem(API_TOKEN_KEY, token);
14     }
15
16     public removeApiToken() {
17         localStorage.removeItem(API_TOKEN_KEY);
18     }
19
20     public getApiToken() {
21         return localStorage.getItem(API_TOKEN_KEY);
22     }
23
24     public isUserLoggedIn() {
25         return this.getApiToken() !== null;
26     }
27
28     public login() {
29         const currentUrl = `${window.location.protocol}//${window.location.host}/token`;
30         window.location.href = `${API_HOST}/login?return_to=${currentUrl}`;
31     }
32
33     public logout(): Promise<any> {
34         const currentUrl = `${window.location.protocol}//${window.location.host}`;
35         return Axios.get(`${API_HOST}/logout?return_to=${currentUrl}`);
36     }
37 }