import Ember from 'ember'; import { getApiRoot, ApiStatus } from 'wherehows-web/utils/api/shared'; import { IAuthenticateResponse, ICurrentUserResponse, IUser } from 'wherehows-web/typings/api/authentication/user'; const { $: { getJSON, get } } = Ember; const logoutUrl = '/logout'; const loginUrl = '/authenticate'; const currentUserUrl = `${getApiRoot()}/user/me`; const castGetJSONToPromise = (url: string): Promise => Promise.resolve(getJSON(url)); /** * Requests the currently logged in user and if the response is ok, * returns the user, otherwise throws * @return {Promise} */ const currentUser = async (): Promise => { const response = await castGetJSONToPromise(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} */ const logout = (): Promise => Promise.resolve(get(logoutUrl)); /** * Calls the login endpoint to * @return {Promise} * TODO: use in ESA authenticator */ const login = (): Promise => castGetJSONToPromise(loginUrl); export { currentUser, logout, login };