mirror of
https://github.com/datahub-project/datahub.git
synced 2025-08-03 06:47:55 +00:00

* Releases updated version of datahub-web client UI code * Fix typo in yarn lock * Change yarn lock to match yarn registry directories * Previous commit missed some paths * Even more changes to yarnlock missing in previous commit * Include codegen file for typings * Add files to get parity for datahub-web and current OS datahub-midtier * Add in typo fix from previous commit - change to proper license * Implement proper OS fix for person entity picture url * Workarounds for open source DH issues * Fixes institutional memory api and removes unopensourced tabs for datasets * Fixes search dataset deprecation and user search issue as a result of changes * Remove internal only options in the avatar menu
42 lines
1.3 KiB
TypeScript
42 lines
1.3 KiB
TypeScript
import { ApiStatus, getApiRoot } from '@datahub/utils/api/shared';
|
|
import { IAuthenticateResponse, ICurrentUserResponse } from 'datahub-web/typings/api/authentication/user';
|
|
import $ from 'jquery';
|
|
import { IUser } from '@datahub/metadata-types/types/common/user';
|
|
|
|
const { getJSON, get } = $;
|
|
const logoutUrl = '/logout';
|
|
const loginUrl = '/authenticate';
|
|
const currentUserUrl = `${getApiRoot()}/user/me`;
|
|
|
|
const castGetJSONToPromise = <T>(url: string): Promise<T> => Promise.resolve(getJSON(url));
|
|
|
|
/**
|
|
* Requests the currently logged in user and if the response is ok,
|
|
* returns the user, otherwise throws
|
|
* @return {Promise<IUser>}
|
|
*/
|
|
const currentUser = async (): Promise<IUser> => {
|
|
const response = await castGetJSONToPromise<ICurrentUserResponse>(currentUserUrl);
|
|
const { status = ApiStatus.FAILED, user } = response;
|
|
if (status === ApiStatus.OK) {
|
|
return user;
|
|
}
|
|
|
|
throw new Error(`Exception: ${status}`);
|
|
};
|
|
|
|
/**
|
|
* Calls the logout endpoint to log out the currently logged in user
|
|
* @return {Promise<void>}
|
|
*/
|
|
const logout = (): Promise<void> => Promise.resolve(get(logoutUrl));
|
|
|
|
/**
|
|
* Calls the login endpoint to
|
|
* @return {Promise<IAuthenticateResponse>}
|
|
* TODO: use in ESA authenticator
|
|
*/
|
|
const login = (): Promise<IAuthenticateResponse> => castGetJSONToPromise<IAuthenticateResponse>(loginUrl);
|
|
|
|
export { currentUser, logout, login };
|