70 lines
1.9 KiB
TypeScript
70 lines
1.9 KiB
TypeScript
import {Component, Inject, OnInit, PLATFORM_ID} from '@angular/core';
|
|
import {AuthService} from '../shared/services/auth.service';
|
|
import {ActivatedRoute, Router} from '@angular/router';
|
|
import {FormsModule} from '@angular/forms';
|
|
import {ViewportService} from '../shared/services/viewport.service';
|
|
import { Title } from '@angular/platform-browser';
|
|
import { take } from 'rxjs/operators';
|
|
import {ParallaxComponent} from '../shared/parallax/parallax.component';
|
|
|
|
@Component({
|
|
selector: 'app-login',
|
|
templateUrl: './login.component.html',
|
|
styleUrls: ['./login.component.css'],
|
|
imports: [
|
|
FormsModule,
|
|
ParallaxComponent
|
|
]
|
|
})
|
|
|
|
export class LoginComponent implements OnInit {
|
|
username = '';
|
|
password = '';
|
|
errorMessage = '';
|
|
private readonly allowedAdminRoutes = new Set([
|
|
'/admin',
|
|
'/shortener'
|
|
]);
|
|
isNarrow = false;
|
|
redirectUrl = '/admin';
|
|
|
|
constructor(
|
|
private authService: AuthService,
|
|
private router: Router,
|
|
private route: ActivatedRoute,
|
|
private deviceService: ViewportService,
|
|
private titleService: Title,
|
|
@Inject(PLATFORM_ID) private platformId: Object
|
|
) {}
|
|
|
|
ngOnInit() {
|
|
this.titleService.setTitle('Login | itzMiney')
|
|
this.isNarrow = this.deviceService.isNarrow;
|
|
|
|
this.route.queryParams.pipe(take(1)).subscribe(params => {
|
|
const raw = params['redirectUrl'];
|
|
|
|
if (typeof raw === 'string' && this.allowedAdminRoutes.has(raw)) {
|
|
this.redirectUrl = raw;
|
|
} else {
|
|
this.redirectUrl = '/admin';
|
|
}
|
|
});
|
|
}
|
|
|
|
onLogin() {
|
|
this.errorMessage = '';
|
|
|
|
this.authService.login(this.username.trim(), this.password).subscribe({
|
|
next: () => {
|
|
void this.router.navigateByUrl(this.redirectUrl);
|
|
},
|
|
error: (err) => {
|
|
this.errorMessage =
|
|
err?.error?.message ?? 'An error occurred. Please try again later.';
|
|
console.error('Login error:', err);
|
|
}
|
|
});
|
|
}
|
|
}
|