This repository has been archived on 2025-06-18. You can view files and clone it, but you cannot make any changes to its state, such as pushing and creating new issues, pull requests or comments.
casino/frontend/src/app/feature/auth/login/login.component.ts
Phan Huy Tran 64b2e28566
All checks were successful
CI / Get Changed Files (pull_request) Successful in 8s
CI / Checkstyle Main (pull_request) Has been skipped
CI / Docker backend validation (pull_request) Has been skipped
CI / oxlint (pull_request) Successful in 27s
CI / eslint (pull_request) Successful in 34s
CI / Docker frontend validation (pull_request) Successful in 46s
CI / prettier (pull_request) Successful in 24s
CI / test-build (pull_request) Successful in 31s
style: run quality tools
2025-05-14 09:54:24 +02:00

59 lines
1.5 KiB
TypeScript

import { Component, signal } from '@angular/core';
import { FormBuilder, FormGroup, ReactiveFormsModule, Validators } from '@angular/forms';
import { Router, RouterLink } from '@angular/router';
import { LoginRequest } from '../../../model/auth/LoginRequest';
import { AuthService } from '@service/auth.service';
import { CommonModule } from '@angular/common';
@Component({
selector: 'app-login',
standalone: true,
imports: [CommonModule, ReactiveFormsModule, RouterLink],
templateUrl: './login.component.html',
})
export class LoginComponent {
loginForm: FormGroup;
errorMessage = signal('');
isLoading = signal(false);
constructor(
private fb: FormBuilder,
private authService: AuthService,
private router: Router
) {
this.loginForm = this.fb.group({
usernameOrEmail: ['', [Validators.required]],
password: ['', [Validators.required]],
});
}
get form() {
return this.loginForm.controls;
}
onSubmit(): void {
if (this.loginForm.invalid) {
return;
}
this.isLoading.set(true);
this.errorMessage.set('');
const loginRequest: LoginRequest = {
usernameOrEmail: this.form['usernameOrEmail'].value,
password: this.form['password'].value,
};
this.authService.login(loginRequest).subscribe({
next: () => {
this.router.navigate(['/home']);
},
error: (err) => {
this.isLoading.set(false);
this.errorMessage.set(
err.error?.message || 'Failed to login. Please check your credentials.'
);
},
});
}
}