All files / core AuthTokenProvider.ts

47.83% Statements 11/23
0% Branches 0/8
57.14% Functions 4/7
45.45% Lines 10/22
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86                                    12x         12x       43x           12x 30x                               12x     43x     12x       12x                                               12x  
/**
 * Copyright 2017 Google Inc.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *   http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
 
import { FirebaseApp } from '@firebase/app-types';
import { FirebaseAuthTokenData } from '@firebase/app-types/private';
import { log, warn } from './util/util';
 
/**
 * Abstraction around FirebaseApp's token fetching capabilities.
 */
export class AuthTokenProvider {
  /**
   * @param {!FirebaseApp} app_
   */
  constructor(private app_: FirebaseApp) {}
 
  /**
   * @param {boolean} forceRefresh
   * @return {!Promise<FirebaseAuthTokenData>}
   */
  getToken(forceRefresh: boolean): Promise<FirebaseAuthTokenData> {
    return this.app_['INTERNAL']['getToken'](forceRefresh).then(
      null,
      // .catch
      function(error) {
        // TODO: Need to figure out all the cases this is raised and whether
        // this makes sense.
        if (error && error.code === 'auth/token-not-initialized') {
          log('Got auth/token-not-initialized error.  Treating as null token.');
          return null;
        } else {
          return Promise.reject(error);
        }
      }
    );
  }
 
  addTokenChangeListener(listener: (token: string | null) => void) {
    // TODO: We might want to wrap the listener and call it with no args to
    // avoid a leaky abstraction, but that makes removing the listener harder.
    this.app_['INTERNAL']['addAuthTokenListener'](listener);
  }
 
  removeTokenChangeListener(listener: (token: string | null) => void) {
    this.app_['INTERNAL']['removeAuthTokenListener'](listener);
  }
 
  notifyForInvalidToken() {
    let errorMessage =
      'Provided authentication credentials for the app named "' +
      this.app_.name +
      '" are invalid. This usually indicates your app was not ' +
      'initialized correctly. ';
    if ('credential' in this.app_.options) {
      errorMessage +=
        'Make sure the "credential" property provided to initializeApp() ' +
        'is authorized to access the specified "databaseURL" and is from the correct ' +
        'project.';
    } else if ('serviceAccount' in this.app_.options) {
      errorMessage +=
        'Make sure the "serviceAccount" property provided to initializeApp() ' +
        'is authorized to access the specified "databaseURL" and is from the correct ' +
        'project.';
    } else {
      errorMessage +=
        'Make sure the "apiKey" and "databaseURL" properties provided to ' +
        'initializeApp() match the values provided for your app at ' +
        'https://console.firebase.google.com/.';
    }
    warn(errorMessage);
  }
}