當用戶輸入 2FA 代碼時,我嘗試進行重定向,將請求發送到我的 API Rest 以檢查代碼是否有效,發送回一個 Promise,在第一次呼叫此請求時,其值未定義,即使令牌由用戶發送,在第二次呼叫時,根據用戶輸入的代碼,該值變為預期值。
我在component.ts中嘗試了幾種方法,通過訂閱請求并檢索值但沒有成功
previousUrl!: string;
isValid!: boolean;
onSubmit() {
this.tfaServices.verifyAuth(this.token).subscribe(v => this.isValid = v);
// isValid = undefined in first call
if (this.isValid)
this.router.navigateByUrl(this.previousUrl);
但也可以在 html 中通過直接訂閱 observable
// Need to click twice on the button to get redirect
<button class="btn btn-lg w-100 mt-3 mb-4 py-3" (click)="onSubmit()" [ngClass]="btnStatus" [routerLink]="[(isValid$ | async) ? '../view' : []]">{{ this.inputDigitLeft }}</button>
但是沒有任何變化,我的值始終未定義,并強制用戶單擊兩次以考慮代碼
環境 Angular NestJS Docker
uj5u.com熱心網友回復:
您需要像這樣檢查承諾中的值:
onSubmit() {
this.tfaServices.verifyAuth(this.token).subscribe(v => {
this.isValid = v;
if (v) {
this.router.navigateByUrl(this.previousUrl);
}
});
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/ruanti/515371.html
標籤:有角度的
