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, ProgressFn } 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';
17 export interface UserDetailsResponse {
26 export class AuthService {
29 protected apiClient: AxiosInstance,
30 protected baseUrl: string,
31 protected actions: ApiActions) { }
33 public saveApiToken(token: string) {
34 localStorage.setItem(API_TOKEN_KEY, token);
37 public removeApiToken() {
38 localStorage.removeItem(API_TOKEN_KEY);
41 public getApiToken() {
42 return localStorage.getItem(API_TOKEN_KEY) || undefined;
46 return localStorage.getItem(USER_UUID_KEY) || undefined;
49 public getOwnerUuid() {
50 return localStorage.getItem(USER_OWNER_UUID_KEY) || undefined;
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);
60 return email && firstName && lastName && uuid && ownerUuid
61 ? { email, firstName, lastName, uuid, ownerUuid }
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);
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);
82 const currentUrl = `${window.location.protocol}//${window.location.host}/token`;
83 window.location.assign(`${this.baseUrl || ""}/login?return_to=${currentUrl}`);
87 const currentUrl = `${window.location.protocol}//${window.location.host}`;
88 window.location.assign(`${this.baseUrl || ""}/logout?return_to=${currentUrl}`);
91 public getUserDetails = (): Promise<User> => {
93 this.actions.progressFn(reqId, true);
95 .get<UserDetailsResponse>('/users/current')
97 this.actions.progressFn(reqId, false);
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
107 this.actions.progressFn(reqId, false);
108 this.actions.errorFn(reqId, e);
113 public getRootUuid() {
114 const uuid = this.getOwnerUuid();
115 const uuidParts = uuid ? uuid.split('-') : [];
116 return uuidParts.length > 1 ? `${uuidParts[0]}-${uuidParts[1]}` : undefined;