import { HttpClient } from '@angular/common/http' ; import { Injectable } from '@angular/core' ; import { GetUserSuccessPayload , LoginPayload } from '@pimp-my-pr/pmp-web/auth/domain' ; import { LoginSuccessPayload , LoginSuccessResponse , UserInfoResponse } from '@pimp-my-pr/shared/domain' ; import { urlFactory } from '@valueadd/typed-urls' ; import { Observable } from 'rxjs' ; import { map } from 'rxjs/operators' ; @ Injectable ( ) export class AuthDataService { readonly endpoints = { getAccessToken : urlFactory ( '/api/auth/access-token' ) , getUser : urlFactory ( '/api/user-info' ) } ; readonly TOKEN_KEY = 'TOKEN' ; constructor ( private http ) { } clearSavedToken ( ) { localStorage . removeItem ( this . TOKEN_KEY ) ; } login ( payload ) { return this . http . post < LoginSuccessResponse > ( this . endpoints . getAccessToken . url ( ) , { code : payload . code , platform : payload . platform } ) . pipe ( map ( ( res ) => res . data ) ) ; } getToken ( ) { return JSON . parse ( localStorage . getItem ( this . TOKEN_KEY ) ) ; } saveToken ( authToken ) { localStorage . setItem ( this . TOKEN_KEY , JSON . stringify ( authToken ) ) ; } getUser ( ) { return this . http . get < UserInfoResponse > ( this . endpoints . getUser . url ( ) ) . pipe ( map ( ( res ) => ( { data : res . data } ) ) ) ; } }