angular firebase angularfire phone authentication sms verification
0 likes

Angular + Firebase Phone Authentication with AngularFire


Angular + Firebase Phone Authentication with AngularFire

Firebase Phone Authentication is a method by which users can sign in by receiving an SMS on their mobile device. When combined with AngularFire, setting up phone authentication in Angular applications is streamlined.

In this guide, we'll cover the implementation of phone number sign-in using Angular, Firebase, and AngularFire.

1. Set Up:

Install AngularFire & Firebase:

Copy Code
ng add @angular/fire

This command will prompt you for Firebase configuration settings. These can be found in the Firebase Console, under "Project Settings > General > Your apps > Firebase SDK snippet > Config."

2. Configuration:

Navigate to the Firebase Console, head over to "Authentication", and enable "Phone" as the sign-in method.

Next, integrate Firebase authentication services in your Angular module:

Copy Code
import { AngularFireModule } from '@angular/fire';
import { AngularFireAuthModule } from '@angular/fire/auth';
import { environment } from '../environments/environment';

@NgModule({
  imports: [
    AngularFireModule.initializeApp(environment.firebaseConfig),
    AngularFireAuthModule
  ],
  declarations: [...],
})
export class AppModule { }

3. Phone Number Authentication:

Start the Sign In Process:

You need to provide a way for users to enter their phone number. Here's a simplified approach:

Copy Code
<input [(ngModel)]="phoneNumber" placeholder="Enter phone number">
<button (click)="startPhoneNumberVerification()">Verify Phone Number</button>

Handle the click event:

Copy Code
import { AngularFireAuth } from '@angular/fire/auth';
import * as firebase from 'firebase/app';

constructor(private auth: AngularFireAuth) {}

startPhoneNumberVerification() {
  const appVerifier = new firebase.auth.RecaptchaVerifier('sign-in-button'); // make sure you have a DOM element with id 'sign-in-button'
  this.auth.signInWithPhoneNumber(this.phoneNumber, appVerifier)
    .then(confirmationResult => {
      // Store confirmationResult to use for the final verification.
      this.windowRef.confirmationResult = confirmationResult;
    })
    .catch(error => {
      console.error('SMS not sent', error);
    });
}
Verify the Code:

The user will receive an SMS with a code. Provide an input for them to type it in:

Copy Code
<input [(ngModel)]="verificationCode" placeholder="Enter verification code">
<button (click)="verifyPhoneNumber()">Submit</button>

Handle the click event:

Copy Code
verifyPhoneNumber() {
  this.windowRef.confirmationResult.confirm(this.verificationCode)
    .then(result => {
      // User successfully signed in.
      const user = result.user;
    })
    .catch(error => {
      console.error('Verification code is incorrect', error);
    });
}

4. Important Considerations:

  • Recaptcha: Firebase uses reCAPTCHA to ensure there's no abuse. You'll see a reCAPTCHA challenge before sending the SMS. Ensure that you've set it up correctly.

  • Quotas: Firebase provides a limited number of free SMS for Phone Authentication. Check Firebase pricing for more details.

Conclusion:

This guide provides a basic understanding of integrating Firebase Phone Authentication in Angular applications using AngularFire. Always keep in mind to secure your application from abuse, manage quotas effectively, and provide feedback to users during the authentication flow.

0 comments