1 | import { Component } from '@angular/core';
|
---|
2 | import {
|
---|
3 | FormControl,
|
---|
4 | FormGroupDirective,
|
---|
5 | NgForm,
|
---|
6 | Validators,
|
---|
7 | FormsModule,
|
---|
8 | ReactiveFormsModule, FormGroup,
|
---|
9 | } from '@angular/forms';
|
---|
10 | import {MatInputModule} from '@angular/material/input';
|
---|
11 | import {MatFormFieldModule} from '@angular/material/form-field';
|
---|
12 | import {ErrorStateMatcher} from "@angular/material/core";
|
---|
13 | import {Router, RouterLink} from "@angular/router";
|
---|
14 | import {InputFieldComponent} from "../input-field/input-field.component";
|
---|
15 | import {AuthService} from "../../services/auth/auth.service";
|
---|
16 | import {AuthRequest} from "../../model/requests/AuthRequest";
|
---|
17 | import {tap} from "rxjs";
|
---|
18 | @Component({
|
---|
19 | standalone: true,
|
---|
20 | imports: [FormsModule, MatFormFieldModule, MatInputModule, ReactiveFormsModule, RouterLink, InputFieldComponent],
|
---|
21 | templateUrl: './login.component.html',
|
---|
22 | })
|
---|
23 | export class LoginComponent {
|
---|
24 |
|
---|
25 | form: FormGroup = new FormGroup({
|
---|
26 | email: new FormControl('', [Validators.required, Validators.email]),
|
---|
27 | password: new FormControl('', [Validators.required]),
|
---|
28 | })
|
---|
29 |
|
---|
30 | constructor(
|
---|
31 | private router: Router,
|
---|
32 | private service: AuthService
|
---|
33 | ) {
|
---|
34 | }
|
---|
35 |
|
---|
36 | onSubmit($event: Event) {
|
---|
37 | console.log(this.form.value as AuthRequest)
|
---|
38 | this.service.login(this.form.value as AuthRequest)
|
---|
39 | .pipe(
|
---|
40 | tap(result => {
|
---|
41 | console.log(result)
|
---|
42 | sessionStorage.setItem('jwt', result.token);
|
---|
43 | this.service.refreshAuth$.next(true);
|
---|
44 | })
|
---|
45 | )
|
---|
46 | .subscribe({
|
---|
47 | next: result => {
|
---|
48 | this.router.navigate(['/'])
|
---|
49 | },
|
---|
50 | error: error => {
|
---|
51 | sessionStorage.removeItem('jwt');
|
---|
52 | }
|
---|
53 | })
|
---|
54 | }
|
---|
55 | }
|
---|
56 |
|
---|