import { ChangeDetectionStrategy, ChangeDetectorRef, Component, EventEmitter, Input, OnChanges, OnInit, Output, SimpleChanges, TemplateRef, ViewChild } from '@angular/core';
import { Router } from '@angular/router';
import { AuthProcessService } from './../../services/auth-process.service';
interface VerifyEmailContext {
email: string;
goBackURL: string;
verifyEmailTitleText: string;
verifyEmailConfirmationText: string;
verifyEmailGoBackText: string;
messageOnEmailConfirmationSuccess: string;
messageOnError: string;
}
const defaultTranslations = {
verifyEmailTitleText: 'Confirm your e-mail address!',
verifyEmailConfirmationText: 'A confirmation e-mail has been sent. Check your inbox and click on the link "Confirm my e-mail" to confirm your e-mail address.',
verifyEmailGoBackText: 'Go back',
sendNewVerificationEmailText: 'Send new confirmation e-mail',
signOutText: 'Sign out',
messageOnEmailConfirmationSuccess: 'A new confirmation e-mail has been sent. Please check your inbox.',
};
@Component({
selector: 'ngx-auth-firebaseui-email-confirmation',
templateUrl: './email-confirmation.component.html',
styleUrls: ['./email-confirmation.component.scss'],
changeDetection: ChangeDetectionStrategy.OnPush
})
export class EmailConfirmationComponent implements OnInit, OnChanges {
@Input() email: string;
@Input() goBackURL: string;
// i18n translations to use in default template
@Input() verifyEmailTitleText: string;
@Input() verifyEmailConfirmationText: string;
@Input() verifyEmailGoBackText: string;
@Input() sendNewVerificationEmailText: string;
@Input() signOutText: string;
@Input() messageOnEmailConfirmationSuccess: string;
// Template to use in place of default template
@Input() template: TemplateRef<any>;
@Output() signOut = new EventEmitter();
// Final template
verifyEmailTemplate: TemplateRef<any>;
// Context hash to use for verifyEmailTemplate.
verifyEmailContext: VerifyEmailContext;
isLoading: boolean;
@ViewChild('defaultVerifyEmail', {static: true}) defaultTemplate: TemplateRef<any>;
constructor(public authProcess: AuthProcessService, private _router: Router, private _cdr: ChangeDetectorRef) {}
ngOnChanges(changes: SimpleChanges): void {
if (changes.verifyEmailTemplate && changes.verifyEmailTemplate.currentValue == null) {
this.verifyEmailTemplate = this.defaultTemplate;
console.log('ngOnChanges - defaultTemplate:', this.verifyEmailTemplate);
}
this.verifyEmailContext = this.createTemplateContext();
}
ngOnInit(): void {
if (!this.verifyEmailTemplate) {
console.log('ngOnInit - defaultTemplate');
this.verifyEmailTemplate = this.defaultTemplate;
}
this.verifyEmailContext = this.createTemplateContext();
console.log('verifyEmailTemplate:', this.verifyEmailTemplate);
console.log('verifyEmailContext:', this.verifyEmailContext);
}
async continue() {
try {
await this.authProcess.reloadUserInfo();
await this._router.navigate([this.goBackURL]);
} catch (error) {
this.authProcess.notifyError(error);
}
}
async sendNewVerificationEmail() {
try {
this.isLoading = true;
this._cdr.markForCheck();
await this.authProcess.sendNewVerificationEmail();
this.authProcess.showToast(this.verifyEmailContext.messageOnEmailConfirmationSuccess);
} catch (error) {
this.authProcess.notifyError(error);
} finally {
this.isLoading = false;
this._cdr.markForCheck();
}
}
private createTemplateContext(): any {
const context = {
email: this.email,
goBackURL: this.goBackURL,
verifyEmailTitleText: this.verifyEmailTitleText || defaultTranslations.verifyEmailTitleText,
verifyEmailConfirmationText: this.verifyEmailConfirmationText || defaultTranslations.verifyEmailConfirmationText,
verifyEmailGoBackText: this.verifyEmailGoBackText || defaultTranslations.verifyEmailGoBackText,
sendNewVerificationEmailText: this.sendNewVerificationEmailText || defaultTranslations.sendNewVerificationEmailText,
signOutText: this.signOutText || defaultTranslations.signOutText,
messageOnEmailConfirmationSuccess: this.messageOnEmailConfirmationSuccess || defaultTranslations.messageOnEmailConfirmationSuccess
};
return context;
}
}
<ng-container *ngTemplateOutlet="verifyEmailTemplate; context: verifyEmailContext"></ng-container>
<ng-template #defaultVerifyEmail let-email="email" let-goBackURL="goBackURL" let-verifyEmailTitleText="verifyEmailTitleText" let-verifyEmailConfirmationText="verifyEmailConfirmationText" let-verifyEmailGoBackText="verifyEmailGoBackText" let-signOutText="signOutText" let-sendNewVerificationEmailText="sendNewVerificationEmailText">
<mat-card class="verify-email">
<mat-card-content fxLayout="column" fxLayoutAlign="center center">
<mat-icon>email</mat-icon>
<p class="title" fxLayout="column" fxLayoutAlign="center center">
<span class="mat-subheading-2">{{ verifyEmailTitleText }}</span>
<span class="mat-body-2">{{ email }}</span>
</p>
<p class="subtitle">{{ verifyEmailConfirmationText }}</p>
<mat-progress-bar *ngIf="isLoading" mode="indeterminate"></mat-progress-bar>
</mat-card-content>
<mat-card-actions fxLayout="column" fxLayoutAlign="center center">
<button *ngIf="goBackURL" mat-stroked-button (click)="continue()" class="go-back-button action-button">
{{ verifyEmailGoBackText }}
</button>
<button mat-stroked-button (click)="sendNewVerificationEmail()" class="send-new-mail-button action-button">{{ sendNewVerificationEmailText }}</button>
<button mat-stroked-button color="warn" (click)="this.signOut.emit()" class="sign-out-button action-button">{{ signOutText }}</button>
</mat-card-actions>
</mat-card>
</ng-template>
.material-icons {
font-size: 4rem;
}
.verify-email {
width: 360px;
.mat-icon {
height: 4rem;
width: 4rem;
color: #444;
}
.title {
margin-top: 16px;
.mat-subheading-2 {
margin-bottom: 0;
}
}
.subtitle {
margin: 16px auto;
text-align: justify;
}
p {
display: block;
-webkit-margin-before: 1em;
-webkit-margin-after: 1em;
-webkit-margin-start: 0px;
-webkit-margin-end: 0px;
}
mat-card-actions {
text-align: center;
margin-top: 1rem;
.action-button {
width: 100%;
}
.action-button + .action-button {
margin-top: 1rem;
}
}
}