import { Component, Inject, ChangeDetectionStrategy } from '@angular/core'; import { CommonModule } from '@angular/common'; import { FormsModule } from '@angular/forms'; import { MatFormFieldModule } from '@angular/material/form-field'; import { MatInputModule } from '@angular/material/input'; import { MatButtonModule } from '@angular/material/button'; import { MatIconModule } from '@angular/material/icon'; import { MatToolbarModule } from '@angular/material/toolbar'; import { MatMenuModule } from '@angular/material/menu'; import { I18nService } from '../services/i18n.service'; import { AuthService } from '../services/auth.service'; import { switchGui } from '../../core/gui-switch'; @Component({ selector: 'p3xr-login', standalone: true, imports: [ CommonModule, FormsModule, MatFormFieldModule, MatInputModule, MatButtonModule, MatIconModule, MatToolbarModule, MatMenuModule, ], changeDetection: ChangeDetectionStrategy.OnPush, template: `
{{ i18n.strings().form?.connection?.label?.username }} person {{ i18n.strings().form?.connection?.label?.password }} lock @if (auth.loginError()) { }
`, styles: [` .p3xr-login-dialog-wrapper { display: flex; align-items: center; justify-content: center; min-height: calc(100vh - 96px); } .p3xr-login-dialog { width: 400px; max-width: calc(100vw - 32px); border-radius: 4px; overflow: hidden; box-shadow: 0 11px 15px -7px rgba(0,0,0,.2), 0 24px 38px 3px rgba(0,0,0,.14), 0 9px 46px 8px rgba(0,0,0,.12); } .full-width { width: 100%; } .p3xr-login-error { color: #f44336; font-size: 13px; margin: 4px 0 8px; } .p3xr-layout-spacer { flex: 1 1 auto; } `], }) export class LoginComponent { username = ''; password = ''; loading = false; hidePassword = true; currentGui = 'ng'; constructor( @Inject(I18nService) readonly i18n: I18nService, @Inject(AuthService) readonly auth: AuthService, ) { try { this.currentGui = localStorage.getItem('p3xr-frontend') || 'ng'; } catch {} } async onLogin(): Promise { if (this.loading || !this.username || !this.password) return; this.loading = true; const success = await this.auth.login(this.username, this.password); if (success) { location.reload(); } this.loading = false; } switchGui(gui: string): void { this.currentGui = gui; switchGui(gui); } getErrorMessage(error: string): string { const strings = this.i18n.strings(); return strings?.confirm?.invalidCredentials; } }