77 lines
2.4 KiB
TypeScript
77 lines
2.4 KiB
TypeScript
import { Component, inject, signal } from '@angular/core';
|
|
import { CommonModule } from '@angular/common';
|
|
import { Router, ActivatedRoute } from '@angular/router';
|
|
import { FormsModule } from '@angular/forms';
|
|
import { NzInputModule } from 'ng-zorro-antd/input';
|
|
import { NzButtonModule } from 'ng-zorro-antd/button';
|
|
import { NzCardModule } from 'ng-zorro-antd/card';
|
|
import { NzAlertModule } from 'ng-zorro-antd/alert';
|
|
import { AuthService } from '../../../core/services/auth.service';
|
|
import { ApiService } from '../../../core/services/api.service';
|
|
import { isAdminKey } from '../../../core/models';
|
|
|
|
@Component({
|
|
selector: 'app-login',
|
|
standalone: true,
|
|
imports: [
|
|
CommonModule,
|
|
FormsModule,
|
|
NzInputModule,
|
|
NzButtonModule,
|
|
NzCardModule,
|
|
NzAlertModule,
|
|
],
|
|
templateUrl: './login.component.html',
|
|
styleUrl: './login.component.scss'
|
|
})
|
|
export class LoginComponent {
|
|
private authService = inject(AuthService);
|
|
private apiService = inject(ApiService);
|
|
private router = inject(Router);
|
|
private route = inject(ActivatedRoute);
|
|
|
|
userId = '';
|
|
adminKey = '';
|
|
loading = signal(false);
|
|
error = signal<string | null>(null);
|
|
|
|
async login(): Promise<void> {
|
|
if (!this.userId.trim() || !this.adminKey.trim()) {
|
|
this.error.set('Please enter both User ID and Admin Key');
|
|
return;
|
|
}
|
|
|
|
this.loading.set(true);
|
|
this.error.set(null);
|
|
|
|
// Temporarily set credentials to make the API call
|
|
this.authService.login(this.userId.trim(), this.adminKey.trim());
|
|
|
|
this.apiService.getCurrentKey(this.userId.trim()).subscribe({
|
|
next: (key) => {
|
|
if (!isAdminKey(key)) {
|
|
this.authService.logout();
|
|
this.error.set('This key does not have admin permissions. Please use an admin key.');
|
|
this.loading.set(false);
|
|
return;
|
|
}
|
|
|
|
// Login successful
|
|
const returnUrl = this.route.snapshot.queryParams['returnUrl'] || '/messages';
|
|
this.router.navigateByUrl(returnUrl);
|
|
},
|
|
error: (err) => {
|
|
this.authService.logout();
|
|
if (err.status === 401 || err.status === 403) {
|
|
this.error.set('Invalid User ID or Admin Key');
|
|
} else if (err.status === 404) {
|
|
this.error.set('User not found');
|
|
} else {
|
|
this.error.set('Failed to authenticate. Please try again.');
|
|
}
|
|
this.loading.set(false);
|
|
}
|
|
});
|
|
}
|
|
}
|