|
| 1 | +/** |
| 2 | + * Copyright 2025 Google LLC |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | + |
| 17 | +import { Component, inject, Input, OnInit } from "@angular/core"; |
| 18 | +import { CommonModule } from "@angular/common"; |
| 19 | +import { injectForm, TanStackField } from "@tanstack/angular-form"; |
| 20 | +import { FirebaseUI } from "../../../provider"; |
| 21 | +import { ButtonComponent } from "../../../components/button/button.component"; |
| 22 | +import { TermsAndPrivacyComponent } from "../../../components/terms-and-privacy/terms-and-privacy.component"; |
| 23 | +import { |
| 24 | + createEmailLinkFormSchema, |
| 25 | + FirebaseUIError, |
| 26 | + completeEmailLinkSignIn, |
| 27 | + sendSignInLinkToEmail, |
| 28 | + FirebaseUI, |
| 29 | +} from "@firebase-ui/core"; |
| 30 | +import { firstValueFrom } from "rxjs"; |
| 31 | + |
| 32 | +@Component({ |
| 33 | + selector: "fui-email-link-form", |
| 34 | + standalone: true, |
| 35 | + imports: [CommonModule, TanStackField, ButtonComponent, TermsAndPrivacyComponent], |
| 36 | + template: ` |
| 37 | + <div *ngIf="emailSent" class="fui-form"> |
| 38 | + {{ emailSentMessage | async }} |
| 39 | + </div> |
| 40 | + <form *ngIf="!emailSent" (submit)="handleSubmit($event)" class="fui-form"> |
| 41 | + <fieldset> |
| 42 | + <ng-container [tanstackField]="form" name="email" #email="field"> |
| 43 | + <label [for]="email.api.name"> |
| 44 | + <span>{{ emailLabel | async }}</span> |
| 45 | + <input |
| 46 | + type="email" |
| 47 | + [id]="email.api.name" |
| 48 | + [name]="email.api.name" |
| 49 | + [value]="email.api.state.value" |
| 50 | + (blur)="email.api.handleBlur()" |
| 51 | + (input)="email.api.handleChange($any($event).target.value)" |
| 52 | + [attr.aria-invalid]="!!email.api.state.meta.errors.length" |
| 53 | + /> |
| 54 | + <span role="alert" aria-live="polite" class="fui-form__error" *ngIf="!!email.api.state.meta.errors.length"> |
| 55 | + {{ email.api.state.meta.errors.join(", ") }} |
| 56 | + </span> |
| 57 | + </label> |
| 58 | + </ng-container> |
| 59 | + </fieldset> |
| 60 | +
|
| 61 | + <fui-terms-and-privacy></fui-terms-and-privacy> |
| 62 | +
|
| 63 | + <fieldset> |
| 64 | + <fui-button type="submit"> |
| 65 | + {{ sendSignInLinkLabel | async }} |
| 66 | + </fui-button> |
| 67 | + <div class="fui-form__error" *ngIf="formError">{{ formError }}</div> |
| 68 | + </fieldset> |
| 69 | + </form> |
| 70 | + `, |
| 71 | +}) |
| 72 | +export class EmailLinkFormComponent implements OnInit { |
| 73 | + private ui = inject(FirebaseUI); |
| 74 | + |
| 75 | + formError: string | null = null; |
| 76 | + emailSent = false; |
| 77 | + private formSchema: any; |
| 78 | + private config: FirebaseUI; |
| 79 | + |
| 80 | + form = injectForm({ |
| 81 | + defaultValues: { |
| 82 | + email: "", |
| 83 | + }, |
| 84 | + }); |
| 85 | + |
| 86 | + async ngOnInit() { |
| 87 | + try { |
| 88 | + this.config = await firstValueFrom(this.ui.config()); |
| 89 | + |
| 90 | + this.formSchema = createEmailLinkFormSchema(this.config); |
| 91 | + |
| 92 | + this.form.update({ |
| 93 | + validators: { |
| 94 | + onSubmit: this.formSchema, |
| 95 | + onBlur: this.formSchema, |
| 96 | + }, |
| 97 | + }); |
| 98 | + |
| 99 | + this.completeSignIn(); |
| 100 | + } catch (error) { |
| 101 | + this.formError = await firstValueFrom(this.ui.translation("errors", "unknownError")); |
| 102 | + } |
| 103 | + } |
| 104 | + |
| 105 | + private async completeSignIn() { |
| 106 | + try { |
| 107 | + await completeEmailLinkSignIn(await firstValueFrom(this.ui.config()), window.location.href); |
| 108 | + } catch (error) { |
| 109 | + if (error instanceof FirebaseUIError) { |
| 110 | + this.formError = error.message; |
| 111 | + } |
| 112 | + } |
| 113 | + } |
| 114 | + |
| 115 | + async handleSubmit(event: SubmitEvent) { |
| 116 | + event.preventDefault(); |
| 117 | + event.stopPropagation(); |
| 118 | + |
| 119 | + const email = this.form.state.values.email; |
| 120 | + |
| 121 | + if (!email) { |
| 122 | + return; |
| 123 | + } |
| 124 | + |
| 125 | + await this.sendSignInLink(email); |
| 126 | + } |
| 127 | + |
| 128 | + async sendSignInLink(email: string) { |
| 129 | + this.formError = null; |
| 130 | + |
| 131 | + try { |
| 132 | + const validationResult = this.formSchema.safeParse({ |
| 133 | + email, |
| 134 | + }); |
| 135 | + |
| 136 | + if (!validationResult.success) { |
| 137 | + const validationErrors = validationResult.error.format(); |
| 138 | + |
| 139 | + if (validationErrors.email?._errors?.length) { |
| 140 | + this.formError = validationErrors.email._errors[0]; |
| 141 | + return; |
| 142 | + } |
| 143 | + |
| 144 | + this.formError = await firstValueFrom(this.ui.translation("errors", "unknownError")); |
| 145 | + return; |
| 146 | + } |
| 147 | + |
| 148 | + await sendSignInLinkToEmail(await firstValueFrom(this.ui.config()), email); |
| 149 | + |
| 150 | + this.emailSent = true; |
| 151 | + } catch (error) { |
| 152 | + if (error instanceof FirebaseUIError) { |
| 153 | + this.formError = error.message; |
| 154 | + return; |
| 155 | + } |
| 156 | + |
| 157 | + this.formError = await firstValueFrom(this.ui.translation("errors", "unknownError")); |
| 158 | + } |
| 159 | + } |
| 160 | + |
| 161 | + get emailLabel() { |
| 162 | + return this.ui.translation("labels", "emailAddress"); |
| 163 | + } |
| 164 | + |
| 165 | + get sendSignInLinkLabel() { |
| 166 | + return this.ui.translation("labels", "sendSignInLink"); |
| 167 | + } |
| 168 | + |
| 169 | + get emailSentMessage() { |
| 170 | + return this.ui.translation("messages", "signInLinkSent"); |
| 171 | + } |
| 172 | +} |
0 commit comments