@@ -20,7 +20,7 @@ import {deepCopy} from '../utils/deep-copy';
2020import { FirebaseApp } from '../firebase-app' ;
2121import { AuthClientErrorCode , FirebaseAuthError , FirebaseError } from '../utils/error' ;
2222import {
23- HttpMethod , SignedApiRequestHandler , ApiSettings ,
23+ ApiSettings , AuthorizedHttpClient , HttpRequestConfig , HttpError ,
2424} from '../utils/api-request' ;
2525import { CreateRequest , UpdateRequest } from './user-record' ;
2626import {
@@ -429,12 +429,8 @@ export const FIREBASE_AUTH_SIGN_UP_NEW_USER = new ApiSettings('signupNewUser', '
429429 * Class that provides mechanism to send requests to the Firebase Auth backend endpoints.
430430 */
431431export class FirebaseAuthRequestHandler {
432- private host : string = FIREBASE_AUTH_HOST ;
433- private port : number = FIREBASE_AUTH_PORT ;
434- private path : string = FIREBASE_AUTH_PATH ;
435- private headers : object = FIREBASE_AUTH_HEADER ;
436- private timeout : number = FIREBASE_AUTH_TIMEOUT ;
437- private signedApiRequestHandler : SignedApiRequestHandler ;
432+ private baseUrl : string = `https://${ FIREBASE_AUTH_HOST } ${ FIREBASE_AUTH_PATH } ` ;
433+ private httpClient : AuthorizedHttpClient ;
438434
439435 /**
440436 * @param {any } response The response to check for errors.
@@ -449,7 +445,7 @@ export class FirebaseAuthRequestHandler {
449445 * @constructor
450446 */
451447 constructor ( app : FirebaseApp ) {
452- this . signedApiRequestHandler = new SignedApiRequestHandler ( app ) ;
448+ this . httpClient = new AuthorizedHttpClient ( app ) ;
453449 }
454450
455451 /**
@@ -805,38 +801,35 @@ export class FirebaseAuthRequestHandler {
805801 * @return {Promise<object> } A promise that resolves with the response.
806802 */
807803 private invokeRequestHandler ( apiSettings : ApiSettings , requestData : object ) : Promise < object > {
808- const path : string = this . path + apiSettings . getEndpoint ( ) ;
809- const httpMethod : HttpMethod = apiSettings . getHttpMethod ( ) ;
810804 return Promise . resolve ( )
811805 . then ( ( ) => {
812806 // Validate request.
813807 const requestValidator = apiSettings . getRequestValidator ( ) ;
814808 requestValidator ( requestData ) ;
815809 // Process request.
816- return this . signedApiRequestHandler . sendRequest (
817- this . host , this . port , path , httpMethod , requestData , this . headers , this . timeout ) ;
810+ const req : HttpRequestConfig = {
811+ method : apiSettings . getHttpMethod ( ) ,
812+ url : `${ this . baseUrl } ${ apiSettings . getEndpoint ( ) } ` ,
813+ headers : FIREBASE_AUTH_HEADER ,
814+ data : requestData ,
815+ timeout : FIREBASE_AUTH_TIMEOUT ,
816+ } ;
817+ return this . httpClient . send ( req ) ;
818818 } )
819819 . then ( ( response ) => {
820- // Check for backend errors in the response.
821- const errorCode = FirebaseAuthRequestHandler . getErrorCode ( response ) ;
822- if ( errorCode ) {
823- throw FirebaseAuthError . fromServerError ( errorCode , /* message */ undefined , response ) ;
824- }
825820 // Validate response.
826821 const responseValidator = apiSettings . getResponseValidator ( ) ;
827- responseValidator ( response ) ;
822+ responseValidator ( response . data ) ;
828823 // Return entire response.
829- return response ;
824+ return response . data ;
830825 } )
831- . catch ( ( response ) => {
832- const error = ( typeof response === 'object' && 'statusCode' in response ) ?
833- response . error : response ;
834- if ( error instanceof FirebaseError ) {
835- throw error ;
826+ . catch ( ( err ) => {
827+ if ( err instanceof HttpError ) {
828+ const error = err . response . data ;
829+ const errorCode = FirebaseAuthRequestHandler . getErrorCode ( error ) ;
830+ throw FirebaseAuthError . fromServerError ( errorCode , /* message */ undefined , error ) ;
836831 }
837-
838- const errorCode = FirebaseAuthRequestHandler . getErrorCode ( error ) ;
839- throw FirebaseAuthError . fromServerError ( errorCode , /* message */ undefined , error ) ;
832+ throw err ;
840833 } ) ;
841834 }
842835}
0 commit comments