@@ -20,7 +20,7 @@ import {deepCopy} from '../utils/deep-copy';
20
20
import { FirebaseApp } from '../firebase-app' ;
21
21
import { AuthClientErrorCode , FirebaseAuthError , FirebaseError } from '../utils/error' ;
22
22
import {
23
- HttpMethod , SignedApiRequestHandler , ApiSettings ,
23
+ ApiSettings , AuthorizedHttpClient , HttpRequestConfig , HttpError ,
24
24
} from '../utils/api-request' ;
25
25
import { CreateRequest , UpdateRequest } from './user-record' ;
26
26
import {
@@ -429,12 +429,8 @@ export const FIREBASE_AUTH_SIGN_UP_NEW_USER = new ApiSettings('signupNewUser', '
429
429
* Class that provides mechanism to send requests to the Firebase Auth backend endpoints.
430
430
*/
431
431
export 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 ;
438
434
439
435
/**
440
436
* @param {any } response The response to check for errors.
@@ -449,7 +445,7 @@ export class FirebaseAuthRequestHandler {
449
445
* @constructor
450
446
*/
451
447
constructor ( app : FirebaseApp ) {
452
- this . signedApiRequestHandler = new SignedApiRequestHandler ( app ) ;
448
+ this . httpClient = new AuthorizedHttpClient ( app ) ;
453
449
}
454
450
455
451
/**
@@ -805,38 +801,35 @@ export class FirebaseAuthRequestHandler {
805
801
* @return {Promise<object> } A promise that resolves with the response.
806
802
*/
807
803
private invokeRequestHandler ( apiSettings : ApiSettings , requestData : object ) : Promise < object > {
808
- const path : string = this . path + apiSettings . getEndpoint ( ) ;
809
- const httpMethod : HttpMethod = apiSettings . getHttpMethod ( ) ;
810
804
return Promise . resolve ( )
811
805
. then ( ( ) => {
812
806
// Validate request.
813
807
const requestValidator = apiSettings . getRequestValidator ( ) ;
814
808
requestValidator ( requestData ) ;
815
809
// 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 ) ;
818
818
} )
819
819
. 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
- }
825
820
// Validate response.
826
821
const responseValidator = apiSettings . getResponseValidator ( ) ;
827
- responseValidator ( response ) ;
822
+ responseValidator ( response . data ) ;
828
823
// Return entire response.
829
- return response ;
824
+ return response . data ;
830
825
} )
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 ) ;
836
831
}
837
-
838
- const errorCode = FirebaseAuthRequestHandler . getErrorCode ( error ) ;
839
- throw FirebaseAuthError . fromServerError ( errorCode , /* message */ undefined , error ) ;
832
+ throw err ;
840
833
} ) ;
841
834
}
842
835
}
0 commit comments