55 lines
1.9 KiB
TypeScript
Executable File
55 lines
1.9 KiB
TypeScript
Executable File
import { Component, ViewChild, OnInit, AfterViewInit, ElementRef } from '@angular/core';
|
|
import { FormGroup, FormControl, Validators, FormsModule, ReactiveFormsModule } from '@angular/forms';
|
|
import { Router, RouterModule } from '@angular/router';
|
|
|
|
import { LoginService } from 'app/login/login.service';
|
|
import { AccountService } from 'app/core/auth/account.service';
|
|
import { SharedModule } from 'app/shared/shared.module';
|
|
|
|
@Component({
|
|
selector: 'jhi-login',
|
|
standalone: true,
|
|
imports: [SharedModule, FormsModule, ReactiveFormsModule, RouterModule],
|
|
templateUrl: './login.component.html',
|
|
})
|
|
export default class LoginComponent implements OnInit, AfterViewInit {
|
|
@ViewChild('username', { static: false })
|
|
username!: ElementRef;
|
|
|
|
authenticationError = false;
|
|
|
|
loginForm = new FormGroup({
|
|
username: new FormControl('', { nonNullable: true, validators: [Validators.required] }),
|
|
password: new FormControl('', { nonNullable: true, validators: [Validators.required] }),
|
|
rememberMe: new FormControl(false, { nonNullable: true, validators: [Validators.required] }),
|
|
});
|
|
|
|
constructor(private accountService: AccountService, private loginService: LoginService, private router: Router) {}
|
|
|
|
ngOnInit(): void {
|
|
// if already authenticated then navigate to home page
|
|
this.accountService.identity().subscribe(() => {
|
|
if (this.accountService.isAuthenticated()) {
|
|
this.router.navigate(['']);
|
|
}
|
|
});
|
|
}
|
|
|
|
ngAfterViewInit(): void {
|
|
this.username.nativeElement.focus();
|
|
}
|
|
|
|
login(): void {
|
|
this.loginService.login(this.loginForm.getRawValue()).subscribe({
|
|
next: () => {
|
|
this.authenticationError = false;
|
|
if (!this.router.getCurrentNavigation()) {
|
|
// There were no routing during login (eg from navigationToStoredUrl)
|
|
this.router.navigate(['']);
|
|
}
|
|
},
|
|
error: () => (this.authenticationError = true),
|
|
});
|
|
}
|
|
}
|