Add auth service, getting api token, login/logout actions
[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 const API_TOKEN_KEY = 'api_token';
6 const API_HOST = 'https://qr1hi.arvadosapi.com';
7
8 export default class AuthService {
9
10     public saveApiToken(token: string) {
11         localStorage.setItem(API_TOKEN_KEY, token);
12     }
13
14     public removeApiToken() {
15         localStorage.removeItem(API_TOKEN_KEY);
16     }
17
18     public getApiToken() {
19         return localStorage.getItem(API_TOKEN_KEY);
20     }
21
22     public isUserLoggedIn() {
23         return this.getApiToken() !== null;
24     }
25
26     public login() {
27         const currentUrl = `${window.location.protocol}//${window.location.host}/token`;
28         window.location.href = `${API_HOST}/login?return_to=${currentUrl}`;
29     }
30
31     public logout() {
32         this.removeApiToken();
33     }
34 }