angular / angular/angularfire

Calling Firebase `validatePassword` outside of an Injection context

未關閉
#3,614 3 則留言 0 個 reaction 已指派 0 人 在 GitHub 檢視
主要語言
TypeScript
星號
7.8k
分支
2.2k
平均合併
22 小時 28 分鐘
30 天內合併 PR
6

描述

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.

貢獻指南

開啟貢獻指南

研究方向

從 validatePassword 進入點和所示的 runInInjectionContext 包裝器開始,接著追蹤 AngularFire 如何處理 Auth 執行個體和注入內容。判斷此警告是否為預期行為,以及是否需要此包裝器;完成條件是記錄正確的呼叫模式,或在不產生警告的情況下簡化驗證。

由索引模型根據 Issue 內容生成。

評估

技術堆疊
angular, firebase, typescript
領域
authentication
Issue 類型
缺陷
難度
4/5
預估耗時
3-5 天
活躍度
停滯
描述清晰度
基本清楚
新手友好度
42/100

把新 issue 寄到你的電子郵件信箱

精選適合新手參與的 GitHub issue 摘要。