import { get } from '../utils' ; import * as storage from '../localStorage' ; import { User } from '../types' ; type DatabaseUser = User & { password ? : string ; } ; export function login ( username , password ) { return get < DatabaseUser [ ] > ( `/users?username=${ username }&password=${ password }` ) . then ( ( results ) => { if ( results . length > 0 ) { const user = results [ 0 ] ; delete user . password ; storage . login ( user ) ; return user ; } else { return Promise . reject ( 'User not found' ) ; } } ) ; } export function getAuthenticatedUser ( ) { const user = storage . getAuthenticatedUser ( ) ; return Promise . resolve ( user ) ; } export function logout ( ) { storage . logout ( ) ; return Promise . resolve ( ) ; } export function getGitHubUser ( username ) { return fetch ( `https://api.github.com/users/${ username }` ) . then ( ( res ) => res . json ( ) ) ; }