Calling Firebase `validatePassword` outside of an Injection context
- Langage dominant
- TypeScript
- Étoiles
- 7.8k
- Forks
- 2.2k
- Merge moyen
- 22 h 28 min
- PR mergées (30 j)
- 6
Description
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.
Guide de contribution
Ouvrir le guide de contribution
Piste de recherche
Commencez par le point d’entrée validatePassword et le wrapper runInInjectionContext présenté, puis suivez la manière dont AngularFire gère l’instance Auth et le contexte d’injection. Déterminez si l’avertissement est attendu et si le wrapper est nécessaire ; le travail est terminé lorsque le modèle d’appel correct est documenté ou que la validation est simplifiée sans l’avertissement.
Rédigé par le modèle d'indexation à partir du texte de l'issue.
Évaluation
- Stack technique
- angular, firebase, typescript
- Domaine
- authentication
- Type d'issue
- Bug
- Difficulté
- 4/5
- Temps estimé
- 3-5 jours
- Activité
- À l'abandon
- Clarté
- Plutôt claire
- Accessibilité débutants
- 42/100