source: bus-n-go-pavel-216049/bus-n-go-frontend/src/app/components/login/login.component.ts

Last change on this file was baf4cc4, checked in by ppaunovski <paunovskipavel@…>, 3 months ago

split group project and individual project into two separate folders

  • Property mode set to 100644
File size: 1.6 KB
Line 
1import { Component } from '@angular/core';
2import {
3 FormControl,
4 FormGroupDirective,
5 NgForm,
6 Validators,
7 FormsModule,
8 ReactiveFormsModule, FormGroup,
9} from '@angular/forms';
10import {MatInputModule} from '@angular/material/input';
11import {MatFormFieldModule} from '@angular/material/form-field';
12import {ErrorStateMatcher} from "@angular/material/core";
13import {Router, RouterLink} from "@angular/router";
14import {InputFieldComponent} from "../input-field/input-field.component";
15import {AuthService} from "../../services/auth/auth.service";
16import {AuthRequest} from "../../model/requests/AuthRequest";
17import {tap} from "rxjs";
18@Component({
19 standalone: true,
20 imports: [FormsModule, MatFormFieldModule, MatInputModule, ReactiveFormsModule, RouterLink, InputFieldComponent],
21 templateUrl: './login.component.html',
22})
23export 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
Note: See TracBrowser for help on using the repository browser.