All files / app/identity/verify-email verify-email.service.ts

100% Statements 10/10
100% Branches 2/2
100% Functions 1/1
100% Lines 9/9

Press n or j to go to the next uncovered block, b, p or k for the previous block.

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                                          1x 4x             4x 3x 3x 2x   1x 1x       1x      
import { inject, Injectable } from '@angular/core';
import { applyActionCode, Auth } from '@angular/fire/auth';
 
import { getErrorCode } from '../error-code';
 
/**
 * Results of verifying an email address with Firebase.
 */
export interface VerifyEmailResult {
  /** Destination after verifying email. */
  readonly continueUrl: string;
  /** Error that prevented verification, if verfied is false. */
  readonly errorCode?: string;
  /** Was the email verified successfully. */
  readonly verified: boolean;
}
 
/**
 * Wrapper for verifying an email with Firebase. This is mostly just to make unit testing easier.
 */
@Injectable({ providedIn: 'root' })
export class VerifyEmailService {
  private readonly _auth: Auth = inject(Auth);
 
  /**
   * Apply the oobCode to verify the user account's email address.
   * Catches errors and wraps in the results interface for use in the template.
   */
  public async verifyEmail(oobCode: string | undefined, continueUrl: string = '/'): Promise<VerifyEmailResult> {
    if (oobCode) {
      try {
        await applyActionCode(this._auth, oobCode);
        return { continueUrl, verified: true };
      } catch (err: unknown) {
        const errorCode = getErrorCode(err);
        return { continueUrl, errorCode, verified: false };
      }
    }
 
    return { continueUrl, errorCode: 'oobCode not found', verified: false };
  }
}