feat: update app version and build details; refactor auth service and components for improved functionality
This commit is contained in:
@@ -1,4 +1,5 @@
|
||||
import { IAuthResponse, TRoles } from '@/core';
|
||||
import { AuthService } from '@/core/services/auth.service';
|
||||
import { ToastService } from '@/core/services/toast.service';
|
||||
import { Component, EventEmitter, inject, Input, Output, signal } from '@angular/core';
|
||||
import { Router } from '@angular/router';
|
||||
@@ -19,10 +20,11 @@ export class AuthComponent {
|
||||
@Input() role?: TRoles;
|
||||
@Input() defaultStep: TSteps = 'login';
|
||||
|
||||
@Output() submit = new EventEmitter<Promise<boolean>>();
|
||||
@Output() onSubmit = new EventEmitter<boolean>();
|
||||
|
||||
private readonly toastService = inject(ToastService);
|
||||
private readonly router = inject(Router);
|
||||
private readonly authService = inject(AuthService);
|
||||
|
||||
readonly logo = images.logo;
|
||||
readonly authVector = images.login;
|
||||
@@ -36,7 +38,7 @@ export class AuthComponent {
|
||||
this.activeStep.set('signup');
|
||||
};
|
||||
|
||||
onLoggedIn = (data: IAuthResponse) => {
|
||||
onLoggedIn = async (data: IAuthResponse) => {
|
||||
this.toastService.success({ text: 'شما با موفقیت وارد شدید.' });
|
||||
|
||||
this.selectedRole.set(data.account.type.toUpperCase() as TRoles);
|
||||
@@ -46,15 +48,18 @@ export class AuthComponent {
|
||||
// return;
|
||||
// }
|
||||
|
||||
if (this.submit.observed) {
|
||||
return this.submit.emit(Promise.resolve(true));
|
||||
if (this.onSubmit.observed) {
|
||||
const isReady = await this.authService.waitForAuthReady();
|
||||
return this.onSubmit.emit(isReady);
|
||||
} else {
|
||||
this.redirectToDashboard();
|
||||
}
|
||||
this.redirectToDashboard();
|
||||
};
|
||||
|
||||
onModifyLoginInfo = () => {
|
||||
if (this.submit.observed) {
|
||||
return this.submit.emit(Promise.resolve(true));
|
||||
onModifyLoginInfo = async () => {
|
||||
if (this.onSubmit.observed) {
|
||||
const isReady = await this.authService.waitForAuthReady();
|
||||
return this.onSubmit.emit(isReady);
|
||||
}
|
||||
this.redirectToDashboard();
|
||||
};
|
||||
|
||||
@@ -101,9 +101,11 @@ export class LoginComponent {
|
||||
next: (data) => {
|
||||
this.errorMessage.set('');
|
||||
if (this.redirectUrl) {
|
||||
this.router.navigateByUrl(this.redirectUrl.replace(/\/[^/]*$/, ''));
|
||||
this.navigateAfterAuth(this.redirectUrl.replace(/\/[^/]*$/, ''));
|
||||
} else {
|
||||
this.onSuccessfullySubmit.emit(data);
|
||||
this.waitForAuthState().then(() => {
|
||||
this.onSuccessfullySubmit.emit(data);
|
||||
});
|
||||
}
|
||||
},
|
||||
error: (error) => {
|
||||
@@ -113,4 +115,25 @@ export class LoginComponent {
|
||||
},
|
||||
});
|
||||
}
|
||||
|
||||
private navigateAfterAuth(url: string) {
|
||||
this.waitForAuthState().then(() => {
|
||||
this.router.navigateByUrl(url);
|
||||
});
|
||||
}
|
||||
|
||||
private waitForAuthState(maxRetries = 10): Promise<void> {
|
||||
return new Promise((resolve) => {
|
||||
let retries = 0;
|
||||
const check = () => {
|
||||
if (this.authService.getToken() || retries >= maxRetries) {
|
||||
resolve();
|
||||
return;
|
||||
}
|
||||
retries += 1;
|
||||
setTimeout(check, 0);
|
||||
};
|
||||
check();
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user