import { Component, OnInit } from '@angular/core'; import { Router } from '@angular/router'; import { AppService } from '../../app.service'; import { environment } from "../../../environments/environment"; import { AuthService } from 'src/app/core/service/auth/auth.service'; import { lastValueFrom } from 'rxjs'; import { EAction, EText } from 'src/app/@config/app'; import { CathayAuthService } from 'src/app/core/service/auth/cathay-auth.service'; import { ROLE_ADMIN } from 'src/app/@config/menus'; import { SathonCathayPayService } from 'src/app/sathon-cathay-pay.service'; @Component({ selector: 'app-login', templateUrl: './login.component.html', styles: [] }) export class LoginComponent implements OnInit { dataForm: any = {}; cathayForm: any = { mobileDeviceId: "1234", userName: 'admin', password: 'P@ssword1' }; isLoading = false; constructor( private router: Router, private appService: AppService, private authService: AuthService, private cathayAuthService: CathayAuthService, private sathonSV: SathonCathayPayService ) { } ngOnInit() { // if (!environment.production) { // this.dataForm = { // username: 'admin', // password: 'admin', // } // } } async onSubmit(form: any) { if (!form.valid) return false; try { // const result = await lastValueFrom(this.authService.login(this.dataForm)); let cathayResult: any = await lastValueFrom(this.cathayAuthService.login(this.cathayForm)); let sathonToken: any = await lastValueFrom(this.cathayAuthService.genToken()); let isAdmin = cathayResult.userName == ROLE_ADMIN ? true : false cathayResult = { ...cathayResult, isAdmin: isAdmin } this.appService.setToken(cathayResult.token.token) this.appService.setSathonToken(sathonToken.token) this.appService.setAuth(cathayResult); if (isAdmin) { return this.router.navigate(['/pages']); } if (!isAdmin) { return this.router.navigate(['/pages/report/transactions']); } } catch (err) { return this.appService.message(EAction.ERROR, EText.NO_DATA); } } public isFieldValid(form: any, field: any) { return field.errors && (field.dirty || field.touched || form.submitted); } }