-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlogin.component.ts
More file actions
80 lines (70 loc) · 2.1 KB
/
Copy pathlogin.component.ts
File metadata and controls
80 lines (70 loc) · 2.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
import { CommonModule } from '@angular/common';
import { Component, inject, OnInit, signal } from '@angular/core';
import { ReactiveFormsModule } from '@angular/forms';
import { email, form, FormField, required, submit } from '@angular/forms/signals';
import { AppRouter } from 'core/fw-extensions/app-router';
import { firstValueFrom } from 'rxjs';
import { AuthService } from '../../services/auth.service';
type LoginData = {
email: string;
password: string;
};
@Component({
selector: 'gh-login',
imports: [CommonModule, ReactiveFormsModule, FormField],
templateUrl: './login.component.html',
styleUrl: './login.component.scss',
host: {
class: 'container-fluid p-3',
},
})
export class LoginComponent implements OnInit {
readonly #authService = inject(AuthService);
readonly #router = inject(AppRouter);
readonly loginModel = signal<LoginData>({
email: '',
password: '',
});
protected loginForm = form(this.loginModel, (form) => {
required(form.email);
required(form.password);
email(form.email);
});
protected serverError = this.#authService.serverError;
protected connectionError = signal(false);
protected validCredentials = signal(true);
ngOnInit(): void {
this.#authService.logout();
}
#isConnected() {
return firstValueFrom(this.#authService.isConnected());
}
async submitForm() {
await submit(this.loginForm, async (form) => {
console.log(this.loginForm().value());
this.connectionError.set(false);
this.validCredentials.set(true);
if (await this.#isConnected()) {
const login$ = this.#authService.login(form.email().value(), form.password().value());
this.validCredentials.set(true);
await firstValueFrom(login$);
if (this.#authService.authenticated) {
await this.#router.navigateToUsers();
} else if (!this.serverError()) {
this.validCredentials.set(false);
}
} else {
this.connectionError.set(true);
}
});
}
async goToGoogleLogin() {
this.connectionError.set(false);
this.validCredentials.set(true);
if (await this.#isConnected()) {
this.#authService.loginGoogle();
} else {
this.connectionError.set(true);
}
}
}