1

this is a newbie question. I've worked through the Firebase tutorial for adding Google Authentication and that works fine. I've also worked through the angular-cli quickstart tutorial and that's also fine. However, in the Firebase tutorial, you add the authentication script (below) to the index.html page and in the angular-cli app, you're not supposed to modify the index.html file. So what's the best practice for where to put the Firebase authentication script?

<script src="https://www.gstatic.com/firebasejs/3.0.5/firebase.js"></script>
  <script>
    // Initialize Firebase
    // TODO: Replace with your project's customized code snippet
    var config = {
      apiKey: "apiKey",
      authDomain: "projectId.firebaseapp.com",
      databaseURL: "https://databaseName.firebaseio.com",
      storageBucket: "bucket.appspot.com",
    };
    firebase.initializeApp(config);
  </script>

2 Answers 2

3

If you don't want to change CLI file structure, you can download and add the script to angular-cli-builds.js -> vendorNpmFiles; add map/package info to src/system-config.ts:

/*******************************************************************************
 * User Configuration.
 *******************************************************************************/
/** Map relative paths to URLs. */
const map: any = {
  'firebase': 'vendor/firebase.js'
};

/** User packages configuration. */
const packages: any = {
  // not sure if this is needed
  // or what type of module the file is...
  'firebase': { defaultExtension: 'js' } 
};

And then add the init code to main.ts:

// imports and stuff
...

import * as firebase from 'firebase';
// import 'firebase'; // or this?
bootstrap(AppComponent).then(() => {
    // Initialize Firebase
    // TODO: Replace with your project's customized code snippet
    var config = {
      apiKey: "apiKey",
      authDomain: "projectId.firebaseapp.com",
      databaseURL: "https://databaseName.firebaseio.com",
      storageBucket: "bucket.appspot.com",
    };
    firebase.initializeApp(config);
});

More info here: 3rd party lib installation.

Sign up to request clarification or add additional context in comments.

Comments

1

I found the doc page here for AugularFire2 user authentication: https://github.com/angular/angularfire2/blob/master/docs/5-user-authentication.md

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.