2025-12-04 21:07:18 +03:30
|
|
|
import { IAuthResponse, TRoles } from '@/core';
|
|
|
|
|
import { ToastService } from '@/core/services/toast.service';
|
|
|
|
|
import { Component, EventEmitter, inject, Input, Output, signal } from '@angular/core';
|
|
|
|
|
import { Router } from '@angular/router';
|
|
|
|
|
import images from 'src/assets/images';
|
|
|
|
|
import { LoginComponent } from './login/login.component';
|
|
|
|
|
|
|
|
|
|
type TSteps = 'login' | 'otp' | 'modifyLoginInfo' | 'signup';
|
|
|
|
|
|
|
|
|
|
@Component({
|
|
|
|
|
selector: 'app-auth',
|
|
|
|
|
templateUrl: './auth.component.html',
|
2026-03-10 13:36:45 +03:30
|
|
|
imports: [LoginComponent],
|
2025-12-04 21:07:18 +03:30
|
|
|
})
|
|
|
|
|
export class AuthComponent {
|
|
|
|
|
@Input() redirectUrl!: string;
|
|
|
|
|
@Input() loginApiUrl!: string;
|
|
|
|
|
@Input() role?: TRoles;
|
|
|
|
|
@Input() defaultStep: TSteps = 'login';
|
|
|
|
|
|
|
|
|
|
@Output() submit = new EventEmitter<Promise<boolean>>();
|
|
|
|
|
|
|
|
|
|
private readonly toastService = inject(ToastService);
|
|
|
|
|
private readonly router = inject(Router);
|
|
|
|
|
|
|
|
|
|
readonly logo = images.logo;
|
|
|
|
|
readonly authVector = images.login;
|
|
|
|
|
|
|
|
|
|
activeStep = signal<TSteps>(this.defaultStep);
|
|
|
|
|
|
|
|
|
|
selectedRole = signal<TRoles | undefined>(this.role);
|
|
|
|
|
|
|
|
|
|
toSignUp = () => {
|
|
|
|
|
this.activeStep.set('signup');
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
onLoggedIn = (data: IAuthResponse) => {
|
|
|
|
|
this.toastService.success({ text: 'شما با موفقیت وارد شدید.' });
|
2026-03-10 13:36:45 +03:30
|
|
|
this.selectedRole.set(data.account.type.toUpperCase() as TRoles);
|
|
|
|
|
// if (data.mustChangePassword) {
|
|
|
|
|
// this.activeStep.set('modifyLoginInfo');
|
|
|
|
|
// return;
|
|
|
|
|
// }
|
2025-12-04 21:07:18 +03:30
|
|
|
|
|
|
|
|
if (this.submit.observed) {
|
|
|
|
|
return this.submit.emit(Promise.resolve(true));
|
|
|
|
|
}
|
|
|
|
|
this.redirectToDashboard();
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
onModifyLoginInfo = () => {
|
|
|
|
|
if (this.submit.observed) {
|
|
|
|
|
return this.submit.emit(Promise.resolve(true));
|
|
|
|
|
}
|
|
|
|
|
this.redirectToDashboard();
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
private redirectToDashboard() {
|
|
|
|
|
let redirectUrl = this.redirectUrl;
|
|
|
|
|
|
|
|
|
|
if (!redirectUrl) {
|
|
|
|
|
switch (this.selectedRole()) {
|
|
|
|
|
case 'ADMIN':
|
2026-03-11 20:42:15 +03:30
|
|
|
redirectUrl = '/super_admin';
|
2025-12-04 21:07:18 +03:30
|
|
|
break;
|
2026-03-16 00:35:34 +03:30
|
|
|
case 'PROVIDER':
|
|
|
|
|
redirectUrl = '/provider';
|
2025-12-04 21:07:18 +03:30
|
|
|
break;
|
2026-03-10 13:36:45 +03:30
|
|
|
case 'PARTNER':
|
|
|
|
|
redirectUrl = '/partner';
|
2025-12-04 21:07:18 +03:30
|
|
|
break;
|
2026-03-16 00:35:34 +03:30
|
|
|
case 'CONSUMER':
|
|
|
|
|
redirectUrl = '/consumer';
|
|
|
|
|
break;
|
2025-12-04 21:07:18 +03:30
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
this.router.navigateByUrl(redirectUrl);
|
|
|
|
|
}
|
|
|
|
|
}
|