61 lines
1.6 KiB
TypeScript
61 lines
1.6 KiB
TypeScript
import {Component, OnInit, ChangeDetectionStrategy} from '@angular/core';
|
|
import {AuthService} from '../shared/services/auth.service';
|
|
import {Router} from '@angular/router';
|
|
import {FormsModule} from '@angular/forms';
|
|
import {ViewportService} from '../shared/services/viewport.service';
|
|
import {ParallaxComponent} from '../shared/parallax/parallax.component';
|
|
import {SeoService} from '../shared/services/seo.service';
|
|
|
|
@Component({
|
|
selector: 'app-login',
|
|
templateUrl: './login.component.html',
|
|
styleUrls: ['./login.component.css'],
|
|
changeDetection: ChangeDetectionStrategy.Eager,
|
|
imports: [
|
|
FormsModule,
|
|
ParallaxComponent
|
|
]
|
|
})
|
|
|
|
export class LoginComponent implements OnInit {
|
|
username = '';
|
|
password = '';
|
|
errorMessage = '';
|
|
isNarrow = false;
|
|
|
|
constructor(
|
|
private authService: AuthService,
|
|
private router: Router,
|
|
private deviceService: ViewportService,
|
|
private seo: SeoService,
|
|
) {}
|
|
|
|
ngOnInit() {
|
|
this.seo.update({
|
|
title: 'Admin Panel',
|
|
desc: 'Management & Administration Panel',
|
|
buttons: [{ label: 'Log-in', url: this.seo.getpath() }]
|
|
})
|
|
this.isNarrow = this.deviceService.isNarrow;
|
|
|
|
if (this.authService.token) {
|
|
void this.router.navigate(['/admin']);
|
|
}
|
|
}
|
|
|
|
onLogin() {
|
|
this.errorMessage = '';
|
|
|
|
this.authService.login(this.username.trim(), this.password).subscribe({
|
|
next: () => {
|
|
void this.router.navigate(['/admin']);
|
|
},
|
|
error: (err) => {
|
|
this.errorMessage =
|
|
err?.error?.message ?? 'An error occurred. Please try again later.';
|
|
console.error('Login error:', err);
|
|
}
|
|
});
|
|
}
|
|
}
|