1 // Copyright (C) The Arvados Authors. All rights reserved.
3 // SPDX-License-Identifier: AGPL-3.0
5 import { API_HOST, serverApi } from "../../common/api/server-api";
6 import { User } from "../../models/user";
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';
15 export interface UserDetailsResponse {
24 export default class AuthService {
26 public saveApiToken(token: string) {
27 localStorage.setItem(API_TOKEN_KEY, token);
30 public removeApiToken() {
31 localStorage.removeItem(API_TOKEN_KEY);
34 public getApiToken() {
35 return localStorage.getItem(API_TOKEN_KEY) || undefined;
38 public getOwnerUuid() {
39 return localStorage.getItem(USER_OWNER_UUID_KEY) || undefined;
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);
49 return email && firstName && lastName && uuid && ownerUuid
50 ? { email, firstName, lastName, uuid, ownerUuid }
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);
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);
71 const currentUrl = `${window.location.protocol}//${window.location.host}/token`;
72 window.location.assign(`${API_HOST}/login?return_to=${currentUrl}`);
76 const currentUrl = `${window.location.protocol}//${window.location.host}`;
77 window.location.assign(`${API_HOST}/logout?return_to=${currentUrl}`);
80 public getUserDetails = (): Promise<User> => {
82 .get<UserDetailsResponse>('/users/current')
84 email: resp.data.email,
85 firstName: resp.data.first_name,
86 lastName: resp.data.last_name,
88 ownerUuid: resp.data.owner_uuid
92 public getRootUuid() {
93 const uuid = this.getOwnerUuid();
94 const uuidParts = uuid ? uuid.split('-') : [];
95 return uuidParts.length > 1 ? `${uuidParts[0]}-${uuidParts[1]}` : undefined;