Simple Managment webapp [LLM]
This commit is contained in:
@@ -0,0 +1,87 @@
|
||||
import { Component, inject, signal } from '@angular/core';
|
||||
import { CommonModule } from '@angular/common';
|
||||
import { Router, ActivatedRoute } from '@angular/router';
|
||||
import { FormsModule } from '@angular/forms';
|
||||
import { NzFormModule } from 'ng-zorro-antd/form';
|
||||
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 { NzIconModule } from 'ng-zorro-antd/icon';
|
||||
import { NzSpinModule } from 'ng-zorro-antd/spin';
|
||||
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,
|
||||
NzFormModule,
|
||||
NzInputModule,
|
||||
NzButtonModule,
|
||||
NzCardModule,
|
||||
NzAlertModule,
|
||||
NzIconModule,
|
||||
NzSpinModule,
|
||||
],
|
||||
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);
|
||||
showKey = signal(false);
|
||||
|
||||
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);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
toggleShowKey(): void {
|
||||
this.showKey.update(v => !v);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user