angular / angular/angularfire

Calling Firebase `validatePassword` outside of an Injection context

Offen
#3,614 3 Kommentare 0 Reaktionen 0 zugewiesene Personen Auf GitHub ansehen
Vorherrschende Sprache
TypeScript
Sterne
7.8k
Forks
2.2k
Ø Merge
22 Std. 28 Min.
Gemergte PRs (30 T.)
6

Beschreibung

Is the running outside of injection context warning legitimate when using `validatePassword`?

My code:

```ts
import { EnvironmentInjector, inject, runInInjectionContext } from '@angular/core';
import { Auth, validatePassword } from '@angular/fire/auth';
import type { PasswordValidationStatus } from '@angular/fire/auth';
import type { AbstractControl, AsyncValidatorFn, ValidationErrors } from '@angular/forms';

import { getPasswordControlValue } from './util';

/**
* Validate against the Firebase Project Authentication Password Policy.
*
* Note: at this time there is no actual need for this because the policy only enforces length and
* other Validators already check for that. However it is nice to know how to do this.
*/
export const passwordFirebaseValidator = (): AsyncValidatorFn => {
const auth: Auth = inject(Auth);
const environmentInjector = inject(EnvironmentInjector);

return async (control: AbstractControl): Promise => {
const value = getPasswordControlValue(control);

// Like Validators.email, rely on Validators.required to check for blank passwords.
if (value == undefined) {
return null; // eslint-disable-line unicorn/no-null -- ValidatorFn returns null
}

const { passwordPolicy: _, ...status } = await runInInjectionContext(
environmentInjector,
async (): Promise => validatePassword(auth, value),
);

return status.isValid ? null : { firebasevalidator: status }; // eslint-disable-line unicorn/no-null
};
};
```

I added `EnvironmentInjector` and `runInInjectionContext` and that got rid of the warning, but is all this necessary? It would be nice if validation was simplier.

Beitragsleitfaden

Beitragsleitfaden öffnen

Rechercherichtung

Beginne am validatePassword-Einstiegspunkt und am gezeigten runInInjectionContext-Wrapper und verfolge dann, wie AngularFire die Auth-Instanz und den Injektionskontext behandelt. Ermittle, ob die Warnung erwartet wird und ob der Wrapper erforderlich ist; abgeschlossen ist die Aufgabe, wenn das korrekte Aufrufmuster dokumentiert oder die Validierung ohne die Warnung vereinfacht wurde.

Vom Indexierungsmodell aus dem Issue-Text verfasst.

Bewertung

Tech-Stack
angular, firebase, typescript
Bereich
authentication
Issue-Typ
Bug
Schwierigkeit
4/5
Geschätzter Aufwand
3-5 Tage
Aktivitätsstatus
Veraltet
Klarheit
Größtenteils klar
Anfängerfreundlichkeit
42/100

Neue Issues direkt in Ihr Postfach

Eine kurze Übersicht über anfängerfreundliche GitHub-Issues.