Files
itzmineydev/src/app/login/login.component.ts
T
2026-03-16 00:20:57 +01:00

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 {DeviceDetectorService} from '../shared/services/device-detector.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'
]);
isMobile = false;
redirectUrl = '/admin';
constructor(
private authService: AuthService,
private router: Router,
private route: ActivatedRoute,
private deviceService: DeviceDetectorService,
private titleService: Title,
@Inject(PLATFORM_ID) private platformId: Object
) {}
ngOnInit() {
this.titleService.setTitle('Login | itzMiney')
this.isMobile = this.deviceService.isMobile;
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);
}
});
}
}