Firebase Authentication In Angular

Introduction

 
Firebase provides a simple way to set up authentication in your Angular project. Here we’ll explain how to set up an authentication workflow. Please follow the below steps.
 
Requirements for the application,
  • NodeJs
  • Angular CLI
  • Visual Studio code IDE
Step 1
 
The command to create a new Angular app is "ng new <appname>". Let's create an Angular app using the following commands.
 
ng new "<Your App Name>" 
 
Step 2
 
Open the above project that you have created in Visual Studio code and install the npm using this command,
 
npm install @angular/fire firebase --save
 
Step 3
 
Once you install the npm file, we must import the reference to your app.module.ts file.
  1. import { AngularFireModule } from '@angular/fire';  
  2. import { AngularFirestoreModule } from '@angular/fire/firestore';  
  3. import { AngularFireAuthModule } from '@angular/fire/auth';  
 We have declared module import; see below.
  1. imports: [  
  2.    AngularFirestoreModule,   
  3.    AngularFireAuthModule,  
  4. ],  
Step 4
 
To create a Firebase application, please visit Firebase and follow the instructions given in the link.
 
Step 5 - How to get Firebase key
 
Select your cretaed project;  refer to the  below screenshot.
 
Firebase Authentication In Angular
 
Add firebase web app, and choose the project settings .
 
Firebase Authentication In Angular 
 
Copy to your project sdk file.
 
Firebase Authentication In Angular 
 
Step 6
 
Open environment.ts file. We need to connect firebase to Angular project, so add the Firebase project credentials.
 
Firebase Authentication In Angular
 
Step 7
 
Open app.module.ts file and add the environment reference file .
  1. import { environment } from '../environments/environment';  
  2.   
  3. imports: [  
  4.    AngularFireModule.initializeApp(environment.firebaseConfig),  
  5. ],  
Firebase Authentication In Angular 
 
Step 8 - Setting of Firebase login
 
Next we are going to integrate Angular project with social sign-in providers. We have enabled Facebook, Twitter and Gmail as social sign- in options for our Angular project.
 
Firebase Authentication In Angular
 
For each sign-in provider we want to enable, we have some configuration setup steps.
 
Firebase providers such as Twitter and Facebook require us to provide client app id and appsecret, and also use the provided Uauth URI which redirects the URI from your Twitter or facebook app.
 

Google Setup

 
Next step we have enable the google option a login without need to add any keys.
 
Firebase Authentication In Angular
 

Twitter Setup

  1. We have to create a Twitter application with the Twitter developer management or check this  link.
  2. Once you have created the Twitter app you will to get app id and secret key.
  3. Next we will go to the sign-in methods on the "enable Twitter" option and add the Twitter app key and the app secret.
Firebase Authentication In Angular
 

Facebook Setup

  1. We have to create a Facebook application in the facebbok developer console or check this  link .
  2. Please follow the instructions given in the link.
  3. Once you have created the Facebook app you will to get app id and secret key.
  4. Next we will go to the sign-in methods on the enable Facebook option and add the Facebook app key and the app secret.
Firebase Authentication In Angular
 
Next step - a valid OAuth redirect URL  will be automatically generated, it must be entered in the  application settings.
 
Go to => Selected project => Open Settings 
 
Firebase Authentication In Angular
 
Step 9
 
Open app.compoanent.html file we can write the button code using the below code.
  1. <button (click)="GoogleLogin()">Google</button>  
  2. <button (click)="FacebookLogin()">Facebook</button>  
  3. <button (click)="TwitterLogin()">Twitter</button>  
Step 10
 
Next step is to open app.component.ts file, Google login request from Google authentication
  1. GoogleLogin() {  
  2.     return new Promise < any > ((resolve, reject) => {  
  3.         let provider = new firebase.auth.GoogleAuthProvider();  
  4.         provider.addScope('profile');  
  5.         provider.addScope('email');  
  6.         this.auth.auth.signInWithPopup(provider).then(res => {  
  7.                 resolve(res);  
  8.             }),  
  9.             error => {  
  10.                 reject(error)  
  11.             }  
  12.     })  
  13. }  
  14. FacebookLogin() {  
  15.     return new Promise < any > ((resolve, reject) => {  
  16.         let provider = new firebase.auth.FacebookAuthProvider();  
  17.         this.auth.auth.signInWithPopup(provider).then(res => {  
  18.             resolve(res);  
  19.         }, error => {  
  20.             reject(error);  
  21.         })  
  22.     })  
  23. }  
  24. TwitterLogin() {  
  25.     return new Promise < any > ((resolve, reject) => {  
  26.         let provider = new firebase.auth.TwitterAuthProvider();  
  27.         this.auth.auth.signInWithPopup(provider).then(res => {  
  28.             resolve(res);  
  29.         }, error => {  
  30.             reject(error);  
  31.         })  
  32.     })  
  33. }  
  34. }  

Summary

 
I hope in this article was useful, if you have any questions please post in the comments.


Similar Articles