datahub/wherehows-web/app/utils/api/authentication.ts
Seyi Adebajo ef065d0e1d refactors login flow. adds profile avatar. updates dependencies
refactors comments feature: initial get comments and create comment

updates tsconfig to support es2017 object prototype methods: values, entries, creates user-avatar component, adds styles, converts constants files to ts

updates comment editor. updates styling. adds auto focus on click

adds comment deletion

adds confirmation dialog for comment deletion

adds string union generic constraint type. adds comment stream header. adds comment update feature. adds style modifications and additions for comment components. refactors actions for comments using strategy pattern to allow for abstraction. adds dataset-constants constants module
2017-09-13 14:18:24 -07:00

41 lines
1.3 KiB
TypeScript

import Ember from 'ember';
import { ApiRoot, 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 = `${ApiRoot}/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 };