|
1 | | -import { Component } from '@angular/core'; |
2 | | -import {AccountsService} from "../accounts.service"; |
3 | | -import {FormBuilder, FormControl, ReactiveFormsModule, Validators} from "@angular/forms"; |
4 | | -import {MatDialogActions, MatDialogContent, MatDialogRef, MatDialogTitle} from "@angular/material/dialog"; |
5 | | -import {MatFormField, MatLabel} from "@angular/material/form-field"; |
6 | | -import {MatInput} from "@angular/material/input"; |
7 | | -import {MatButton} from "@angular/material/button"; |
8 | | -import {WebService} from "../../web.service"; |
9 | | - |
10 | | -@Component({ |
11 | | - selector: 'app-login-dialog', |
12 | | - imports: [ |
13 | | - MatDialogTitle, |
14 | | - MatDialogContent, |
15 | | - ReactiveFormsModule, |
16 | | - MatFormField, |
17 | | - MatInput, |
18 | | - MatDialogActions, |
19 | | - MatButton, |
20 | | - MatLabel, |
21 | | - ], |
22 | | - templateUrl: './login-dialog.component.html', |
23 | | - styleUrl: './login-dialog.component.scss' |
24 | | -}) |
25 | | -export class LoginDialogComponent { |
26 | | - |
27 | | - form = this.fb.group({ |
28 | | - username: new FormControl('', Validators.required), |
29 | | - password: new FormControl('', Validators.required), |
30 | | - }) |
31 | | - |
32 | | - constructor(private accounts: AccountsService, private fb: FormBuilder, private dialogRef: MatDialogRef<LoginDialogComponent>, private web: WebService) { |
33 | | - |
34 | | - } |
35 | | - |
36 | | - close() { |
37 | | - this.dialogRef.close() |
38 | | - } |
39 | | - |
40 | | - login() { |
41 | | - if (this.form.invalid) { |
42 | | - return |
43 | | - } |
44 | | - if (this.form.value.username && this.form.value.password) { |
45 | | - this.accounts.login(this.form.value.username, this.form.value.password).subscribe( |
46 | | - (data) => { |
47 | | - this.accounts.loggedIn = true |
48 | | - this.accounts.userAccount.token = data.token |
49 | | - // @ts-ignore |
50 | | - this.accounts.userAccount.username = this.form.value.username |
51 | | - this.accounts.saveToStorage() |
52 | | - this.dialogRef.close() |
53 | | - this.web.getCurrentUser().subscribe((data) => { |
54 | | - if (data) { |
55 | | - this.accounts.currentUser = data |
56 | | - } |
57 | | - }) |
58 | | - }, |
59 | | - (error) => { |
60 | | - console.log(error) |
61 | | - } |
62 | | - ) |
63 | | - } |
64 | | - } |
65 | | - |
66 | | - connectKeycloak() { |
67 | | - this.web.getLoginProviderRedirect() |
68 | | - } |
69 | | - |
70 | | -} |
| 1 | +import { Component } from '@angular/core'; |
| 2 | +import { AccountsService } from "../accounts.service"; |
| 3 | +import { FormBuilder, FormControl, ReactiveFormsModule, Validators } from "@angular/forms"; |
| 4 | +import { MatDialogActions, MatDialogContent, MatDialogRef, MatDialogTitle } from "@angular/material/dialog"; |
| 5 | +import { MatFormField, MatLabel, MatError } from "@angular/material/form-field"; |
| 6 | +import { MatInput } from "@angular/material/input"; |
| 7 | +import { MatButton } from "@angular/material/button"; |
| 8 | +import { MatProgressSpinner } from "@angular/material/progress-spinner"; |
| 9 | +import { WebService } from "../../web.service"; |
| 10 | +import { finalize, catchError } from "rxjs/operators"; |
| 11 | +import { of } from "rxjs"; |
| 12 | + |
| 13 | +@Component({ |
| 14 | + selector: 'app-login-dialog', |
| 15 | + imports: [ |
| 16 | + MatDialogTitle, |
| 17 | + MatDialogContent, |
| 18 | + ReactiveFormsModule, |
| 19 | + MatFormField, |
| 20 | + MatInput, |
| 21 | + MatDialogActions, |
| 22 | + MatButton, |
| 23 | + MatLabel, |
| 24 | + MatError, |
| 25 | + MatProgressSpinner |
| 26 | + ], |
| 27 | + templateUrl: './login-dialog.component.html', |
| 28 | + styleUrl: './login-dialog.component.scss' |
| 29 | +}) |
| 30 | +export class LoginDialogComponent { |
| 31 | + form = this.fb.group({ |
| 32 | + username: new FormControl('', [Validators.required]), |
| 33 | + password: new FormControl('', [Validators.required]) |
| 34 | + }); |
| 35 | + |
| 36 | + isLoading = false; |
| 37 | + loginError: string | null = null; |
| 38 | + |
| 39 | + constructor( |
| 40 | + private accounts: AccountsService, |
| 41 | + private fb: FormBuilder, |
| 42 | + private dialogRef: MatDialogRef<LoginDialogComponent>, |
| 43 | + private web: WebService |
| 44 | + ) {} |
| 45 | + |
| 46 | + close() { |
| 47 | + this.dialogRef.close(); |
| 48 | + } |
| 49 | + |
| 50 | + login() { |
| 51 | + if (this.form.invalid) { |
| 52 | + this.form.markAllAsTouched(); |
| 53 | + return; |
| 54 | + } |
| 55 | + |
| 56 | + const username = this.form.value.username; |
| 57 | + const password = this.form.value.password; |
| 58 | + |
| 59 | + if (username && password) { |
| 60 | + this.isLoading = true; |
| 61 | + this.loginError = null; |
| 62 | + |
| 63 | + this.accounts.login(username, password).pipe( |
| 64 | + catchError(error => { |
| 65 | + this.loginError = error.status === 401 ? |
| 66 | + 'Invalid username or password' : |
| 67 | + 'Login failed. Please try again.'; |
| 68 | + return of(null); |
| 69 | + }), |
| 70 | + finalize(() => this.isLoading = false) |
| 71 | + ).subscribe(data => { |
| 72 | + if (data) { |
| 73 | + this.accounts.loggedIn = true; |
| 74 | + this.accounts.userAccount.token = data.token; |
| 75 | + this.accounts.userAccount.username = username; |
| 76 | + this.accounts.saveToStorage(); |
| 77 | + this.dialogRef.close(true); |
| 78 | + |
| 79 | + this.web.getCurrentUser().subscribe(userData => { |
| 80 | + if (userData) { |
| 81 | + this.accounts.currentUser = userData; |
| 82 | + } |
| 83 | + }); |
| 84 | + } |
| 85 | + }); |
| 86 | + } |
| 87 | + } |
| 88 | + |
| 89 | + connectKeycloak() { |
| 90 | + this.isLoading = true; |
| 91 | + this.loginError = null; |
| 92 | + this.web.getLoginProviderRedirect(); |
| 93 | + } |
| 94 | +} |
0 commit comments