Decoupled services from redux
[arvados.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 { API_HOST, serverApi } from "../../common/api/server-api";
6 import { User } from "../../models/user";
7
8 export const API_TOKEN_KEY = 'apiToken';
9 export const USER_EMAIL_KEY = 'userEmail';
10 export const USER_FIRST_NAME_KEY = 'userFirstName';
11 export const USER_LAST_NAME_KEY = 'userLastName';
12 export const USER_UUID_KEY = 'userUuid';
13 export const USER_OWNER_UUID_KEY = 'userOwnerUuid';
14
15 export interface UserDetailsResponse {
16     email: string;
17     first_name: string;
18     last_name: string;
19     uuid: string;
20     owner_uuid: string;
21     is_admin: boolean;
22 }
23
24 export default class AuthService {
25
26     public saveApiToken(token: string) {
27         localStorage.setItem(API_TOKEN_KEY, token);
28     }
29
30     public removeApiToken() {
31         localStorage.removeItem(API_TOKEN_KEY);
32     }
33
34     public getApiToken() {
35         return localStorage.getItem(API_TOKEN_KEY) || undefined;
36     }
37
38     public getOwnerUuid() {
39         return localStorage.getItem(USER_OWNER_UUID_KEY) || undefined;
40     }
41
42     public getUser(): User | undefined {
43         const email = localStorage.getItem(USER_EMAIL_KEY);
44         const firstName = localStorage.getItem(USER_FIRST_NAME_KEY);
45         const lastName = localStorage.getItem(USER_LAST_NAME_KEY);
46         const uuid = localStorage.getItem(USER_UUID_KEY);
47         const ownerUuid = localStorage.getItem(USER_OWNER_UUID_KEY);
48
49         return email && firstName && lastName && uuid && ownerUuid
50             ? { email, firstName, lastName, uuid, ownerUuid }
51             : undefined;
52     }
53
54     public saveUser(user: User) {
55         localStorage.setItem(USER_EMAIL_KEY, user.email);
56         localStorage.setItem(USER_FIRST_NAME_KEY, user.firstName);
57         localStorage.setItem(USER_LAST_NAME_KEY, user.lastName);
58         localStorage.setItem(USER_UUID_KEY, user.uuid);
59         localStorage.setItem(USER_OWNER_UUID_KEY, user.ownerUuid);
60     }
61
62     public removeUser() {
63         localStorage.removeItem(USER_EMAIL_KEY);
64         localStorage.removeItem(USER_FIRST_NAME_KEY);
65         localStorage.removeItem(USER_LAST_NAME_KEY);
66         localStorage.removeItem(USER_UUID_KEY);
67         localStorage.removeItem(USER_OWNER_UUID_KEY);
68     }
69
70     public login() {
71         const currentUrl = `${window.location.protocol}//${window.location.host}/token`;
72         window.location.assign(`${API_HOST}/login?return_to=${currentUrl}`);
73     }
74
75     public logout() {
76         const currentUrl = `${window.location.protocol}//${window.location.host}`;
77         window.location.assign(`${API_HOST}/logout?return_to=${currentUrl}`);
78     }
79
80     public getUserDetails = (): Promise<User> => {
81         return serverApi
82             .get<UserDetailsResponse>('/users/current')
83             .then(resp => ({
84                 email: resp.data.email,
85                 firstName: resp.data.first_name,
86                 lastName: resp.data.last_name,
87                 uuid: resp.data.uuid,
88                 ownerUuid: resp.data.owner_uuid
89             }));
90     }
91
92     public getRootUuid() {
93         const uuid = this.getOwnerUuid();
94         const uuidParts = uuid ? uuid.split('-') : [];
95         return uuidParts.length > 1 ? `${uuidParts[0]}-${uuidParts[1]}` : undefined;
96     }
97 }