Add progress indicator for services
[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 { User } from "~/models/user";
6 import { AxiosInstance } from "axios";
7 import { ProgressFn } from "~/services/api/api-progress";
8 import * as uuid from "uuid/v4";
9
10 export const API_TOKEN_KEY = 'apiToken';
11 export const USER_EMAIL_KEY = 'userEmail';
12 export const USER_FIRST_NAME_KEY = 'userFirstName';
13 export const USER_LAST_NAME_KEY = 'userLastName';
14 export const USER_UUID_KEY = 'userUuid';
15 export const USER_OWNER_UUID_KEY = 'userOwnerUuid';
16
17 export interface UserDetailsResponse {
18     email: string;
19     first_name: string;
20     last_name: string;
21     uuid: string;
22     owner_uuid: string;
23     is_admin: boolean;
24 }
25
26 export class AuthService {
27
28     constructor(
29         protected apiClient: AxiosInstance,
30         protected baseUrl: string,
31         protected progressFn: ProgressFn) { }
32
33     public saveApiToken(token: string) {
34         localStorage.setItem(API_TOKEN_KEY, token);
35     }
36
37     public removeApiToken() {
38         localStorage.removeItem(API_TOKEN_KEY);
39     }
40
41     public getApiToken() {
42         return localStorage.getItem(API_TOKEN_KEY) || undefined;
43     }
44
45     public getUuid() {
46         return localStorage.getItem(USER_UUID_KEY) || undefined;
47     }
48
49     public getOwnerUuid() {
50         return localStorage.getItem(USER_OWNER_UUID_KEY) || undefined;
51     }
52
53     public getUser(): User | undefined {
54         const email = localStorage.getItem(USER_EMAIL_KEY);
55         const firstName = localStorage.getItem(USER_FIRST_NAME_KEY);
56         const lastName = localStorage.getItem(USER_LAST_NAME_KEY);
57         const uuid = localStorage.getItem(USER_UUID_KEY);
58         const ownerUuid = localStorage.getItem(USER_OWNER_UUID_KEY);
59
60         return email && firstName && lastName && uuid && ownerUuid
61             ? { email, firstName, lastName, uuid, ownerUuid }
62             : undefined;
63     }
64
65     public saveUser(user: User) {
66         localStorage.setItem(USER_EMAIL_KEY, user.email);
67         localStorage.setItem(USER_FIRST_NAME_KEY, user.firstName);
68         localStorage.setItem(USER_LAST_NAME_KEY, user.lastName);
69         localStorage.setItem(USER_UUID_KEY, user.uuid);
70         localStorage.setItem(USER_OWNER_UUID_KEY, user.ownerUuid);
71     }
72
73     public removeUser() {
74         localStorage.removeItem(USER_EMAIL_KEY);
75         localStorage.removeItem(USER_FIRST_NAME_KEY);
76         localStorage.removeItem(USER_LAST_NAME_KEY);
77         localStorage.removeItem(USER_UUID_KEY);
78         localStorage.removeItem(USER_OWNER_UUID_KEY);
79     }
80
81     public login() {
82         const currentUrl = `${window.location.protocol}//${window.location.host}/token`;
83         window.location.assign(`${this.baseUrl || ""}/login?return_to=${currentUrl}`);
84     }
85
86     public logout() {
87         const currentUrl = `${window.location.protocol}//${window.location.host}`;
88         window.location.assign(`${this.baseUrl || ""}/logout?return_to=${currentUrl}`);
89     }
90
91     public getUserDetails = (): Promise<User> => {
92         const reqId = uuid();
93         this.progressFn(reqId, true);
94         return this.apiClient
95             .get<UserDetailsResponse>('/users/current')
96             .then(resp => {
97                 this.progressFn(reqId, false);
98                 return {
99                     email: resp.data.email,
100                     firstName: resp.data.first_name,
101                     lastName: resp.data.last_name,
102                     uuid: resp.data.uuid,
103                     ownerUuid: resp.data.owner_uuid
104                 };
105             })
106             .catch(e => {
107                 this.progressFn(reqId, false);
108                 throw e;
109             });
110     }
111
112     public getRootUuid() {
113         const uuid = this.getOwnerUuid();
114         const uuidParts = uuid ? uuid.split('-') : [];
115         return uuidParts.length > 1 ? `${uuidParts[0]}-${uuidParts[1]}` : undefined;
116     }
117 }