jangular-cli
Version:
A powerful CLI tool for rapidly bootstrapping Angular 17 & Spring Boot (Java 21) applications with integrated security, services, and enterprise-ready best practices.
60 lines (50 loc) • 1.63 kB
text/typescript
import { Component, OnInit } from '@angular/core';
import { FormBuilder, FormGroup, ReactiveFormsModule, Validators } from '@angular/forms';
import { AuthService } from '../../../services/auth/auth.service';
import { Router, RouterModule } from '@angular/router';
import { NgIf } from '@angular/common';
export class LoginComponent implements OnInit {
loginForm!: FormGroup;
errorMessage: string = '';
isLoading: boolean = false;
constructor(
private formBuilder: FormBuilder,
private authService: AuthService,
private router: Router
) { }
ngOnInit(): void {
// Redirect if already logged in
if (this.authService.isAuthenticated()) {
this.router.navigate(['/dashboard']);
}
this.loginForm = this.formBuilder.group({
username: ['', [Validators.required]],
password: ['', [Validators.required, Validators.minLength(6)]]
});
}
onSubmit(): void {
if (this.loginForm.invalid) {
return;
}
this.isLoading = true;
this.errorMessage = '';
const { username, password } = this.loginForm.value;
this.authService.login(username, password).subscribe({
next: (response) => {
this.isLoading = false;
this.router.navigate(['/dashboard']);
},
error: (error) => {
this.isLoading = false;
this.errorMessage = error.error?.message || 'Login failed. Please check your credentials.';
}
});
}
}