import {Component, Input, OnInit} from '@angular/core'; import {FormControl, FormGroup, Validators} from "@angular/forms"; import {HttpClient, HttpErrorResponse} from "@angular/common/http"; import {ApiService} from "../../../Services/api.service"; import {authResponse} from "../../../Interfaces/authResponse.interface"; @Component({ selector: 'app-login', templateUrl: './login.component.html', styleUrls: ['./login.component.scss'] }) export class LoginComponent implements OnInit{ @Input() setHasLoggedIn: any; loginFormGroup!: FormGroup; error: boolean = false; constructor(private apiService: ApiService) { } ngOnInit(): void { this.loginFormGroup = new FormGroup({ 'email': new FormControl('', Validators.required), 'password': new FormControl('', Validators.required) }) } onSubmit(): void{ this.apiService.adminLogin(this.loginFormGroup.value).subscribe({ next: (data: authResponse): void =>{ this.error = false if(data.status === "error"){ this.error = true return } localStorage.setItem("token", data.data.token) localStorage.setItem("exptime", JSON.stringify(Date.now()+ (24*60*60*1000))) this.apiService.setLoggedInSubj(true) }, error: (error: HttpErrorResponse): void =>{ this.error = true } }) } }