All files / app/identity/confirm-email confirm-email.component.ts

100% Statements 11/11
100% Branches 0/0
100% Functions 2/2
100% Lines 11/11

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 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62                                                        1x                 7x 7x     7x             2x 2x   2x 2x   1x 1x     2x      
import { AsyncPipe } from '@angular/common';
import {
  ChangeDetectionStrategy,
  Component,
  inject,
  signal,
} from '@angular/core';
import type { WritableSignal } from '@angular/core';
import { sendEmailVerification } from '@angular/fire/auth';
import type { User } from '@angular/fire/auth';
 
import { USER$ } from '@app/core/user.token';
import type { MaybeUser$ } from '@app/core/user.token';
import { SpinnerComponent } from '@app/shared/spinner/spinner.component';
 
import { AuthErrorMessagesComponent } from '../auth-error-messages/auth-error-messages.component';
import { getErrorCode } from '../error-code';
import type { SendVerifyEmailStatuses } from './send-confirm-email';
 
/**
 * Sends the user an email to confirm access to the email address.
 */
@Component({
  changeDetection: ChangeDetectionStrategy.OnPush,
  imports: [ AsyncPipe, AuthErrorMessagesComponent, SpinnerComponent ],
  selector: 'app-confirm-email',
  templateUrl: './confirm-email.component.html',
})
export class ConfirmEmailComponent {
  /** Firebase response error code */
  public readonly $errorCode: WritableSignal<string>;
  /** Triggers different stages in the view for email verification sending. */
  public readonly $verificationStatus: WritableSignal<SendVerifyEmailStatuses>;
  /** Currently logged in Firebase User. */
  public readonly user$: MaybeUser$;
 
  constructor() {
    this.$errorCode = signal<string>('');
    this.$verificationStatus = signal<SendVerifyEmailStatuses>('unsent');
 
    // Not handling non-logged in users because the Route guards should.
    this.user$ = inject(USER$);
  }
 
  /**
   * Sends email to User's email address in Firebase Authentication to verify control of the address.
   */
  public async sendConfirmEmail(user: User): Promise<void> {
    this.$errorCode.set(''); // Clear out any existing errors
    this.$verificationStatus.set('sending');
 
    try {
      await sendEmailVerification(user);
    } catch (err: unknown) {
      const code = getErrorCode(err);
      this.$errorCode.set(code);
    }
 
    this.$verificationStatus.set('sent');
  }
}