1 // Copyright (C) The Arvados Authors. All rights reserved.
3 // SPDX-License-Identifier: AGPL-3.0
5 import { User } from "~/models/user";
6 import { AxiosInstance } from "axios";
7 import { ApiActions } from "~/services/api/api-actions";
8 import * as uuid from "uuid/v4";
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 export const USER_IS_ADMIN = 'isAdmin';
18 export interface UserDetailsResponse {
27 export class AuthService {
30 protected apiClient: AxiosInstance,
31 protected baseUrl: string,
32 protected actions: ApiActions) { }
34 public saveApiToken(token: string) {
35 localStorage.setItem(API_TOKEN_KEY, token);
38 public removeApiToken() {
39 localStorage.removeItem(API_TOKEN_KEY);
42 public getApiToken() {
43 return localStorage.getItem(API_TOKEN_KEY) || undefined;
47 return localStorage.getItem(USER_UUID_KEY) || undefined;
50 public getOwnerUuid() {
51 return localStorage.getItem(USER_OWNER_UUID_KEY) || undefined;
54 public getIsAdmin(): boolean {
55 return localStorage.getItem(USER_IS_ADMIN) === 'true';
58 public getUser(): User | undefined {
59 const email = localStorage.getItem(USER_EMAIL_KEY);
60 const firstName = localStorage.getItem(USER_FIRST_NAME_KEY);
61 const lastName = localStorage.getItem(USER_LAST_NAME_KEY);
62 const uuid = this.getUuid();
63 const ownerUuid = this.getOwnerUuid();
64 const isAdmin = this.getIsAdmin();
66 return email && firstName && lastName && uuid && ownerUuid
67 ? { email, firstName, lastName, uuid, ownerUuid, isAdmin }
71 public saveUser(user: User) {
72 localStorage.setItem(USER_EMAIL_KEY, user.email);
73 localStorage.setItem(USER_FIRST_NAME_KEY, user.firstName);
74 localStorage.setItem(USER_LAST_NAME_KEY, user.lastName);
75 localStorage.setItem(USER_UUID_KEY, user.uuid);
76 localStorage.setItem(USER_OWNER_UUID_KEY, user.ownerUuid);
77 localStorage.setItem(USER_IS_ADMIN, JSON.stringify(user.isAdmin));
81 localStorage.removeItem(USER_EMAIL_KEY);
82 localStorage.removeItem(USER_FIRST_NAME_KEY);
83 localStorage.removeItem(USER_LAST_NAME_KEY);
84 localStorage.removeItem(USER_UUID_KEY);
85 localStorage.removeItem(USER_OWNER_UUID_KEY);
86 localStorage.removeItem(USER_IS_ADMIN);
90 const currentUrl = `${window.location.protocol}//${window.location.host}/token`;
91 window.location.assign(`${this.baseUrl || ""}/login?return_to=${currentUrl}`);
95 const currentUrl = `${window.location.protocol}//${window.location.host}`;
96 window.location.assign(`${this.baseUrl || ""}/logout?return_to=${currentUrl}`);
99 public getUserDetails = (): Promise<User> => {
100 const reqId = uuid();
101 this.actions.progressFn(reqId, true);
102 return this.apiClient
103 .get<UserDetailsResponse>('/users/current')
105 this.actions.progressFn(reqId, false);
107 email: resp.data.email,
108 firstName: resp.data.first_name,
109 lastName: resp.data.last_name,
110 uuid: resp.data.uuid,
111 ownerUuid: resp.data.owner_uuid,
112 isAdmin: resp.data.is_admin
116 this.actions.progressFn(reqId, false);
117 this.actions.errorFn(reqId, e);
122 public getRootUuid() {
123 const uuid = this.getOwnerUuid();
124 const uuidParts = uuid ? uuid.split('-') : [];
125 return uuidParts.length > 1 ? `${uuidParts[0]}-${uuidParts[1]}` : undefined;