import { Component, OnInit } from '@angular/core'; import { FormControl, FormGroup, Validators } from '@angular/forms'; import { Router } from '@angular/router'; import { User } from 'src/app/model/User'; import { ProductsService } from 'src/app/service/products.service'; import { UserService } from '../service/user.service'; @Component({ selector: 'app-login', templateUrl: './login.component.html', styleUrls: ['./login.component.css'] }) export class LoginComponent implements OnInit { loginForm! : FormGroup; user! : User msg: string; uid!:any; constructor(private route: Router,private userService:UserService, private productService:ProductsService) { this.msg=''; } ngOnInit(): void { this.loginForm = new FormGroup({ username : new FormControl('', Validators.required), password : new FormControl('', Validators.required) }); } OnLoginFormSubmit() { this.user = { username : this.loginForm.value.username, role : this.loginForm.value.role, password : this.loginForm.value.password } this.userService.login(this.user).subscribe((data: any)=>{ //save the username and password in local storage window.sessionStorage.setItem("username",this.user.username); window.sessionStorage.setItem("isLoggedIn","true"); this.route.navigateByUrl('/banner'); }), (err: any)=>{ this.msg='Invalid credentials...Check your Login and Password!!!'; window.sessionStorage.removeItem("username"); window.sessionStorage.removeItem("isLoggedIn"); }; } }