I am facing an issue with login authentication. Upon debugging, I noticed that my Database is returning null, which was not the case before.
I have tried various approaches to identify the bug but have been unsuccessful in locating it.
Interestingly, when I remove the authentication, the console.log works fine before validation. However, within the login method and prior to validation in the if statement, it returns NULL.
DATABASE.SERVICE
import { Injectable } from '@angular/core';
import {User} from '../model/user';
import {serialize} from
'@angular/compiler/src/i18n/serializers/xml_helper.d';
@Injectable()
export class DatabaseService {
User: User;
constructor() {
this.User = JSON.parse(localStorage.getItem('user'));
}
login(email:string, password:string):boolean {
//Here I get UNDEFIEND
if(email == 'admin' && password == 'admin'){
this.User = {
email:'admin',
password:'admin'
}
}
localStorage.setItem('user',JSON.stringify(this.User));
return this.checkLogin();
}
logOut(): void{
localStorage.clear();
}
checkLogin():boolean{
return (this.User != null);
}
}
LOGIN.COMPONENT
import { Component, OnInit } from '@angular/core';
import { FormsModule } from '@angular/forms';
import {NgForm} from '@angular/forms';
import {DatabaseService} from '../../service/database.service';
import {Router} from '@angular/router';
@Component({
selector: 'app-login',
templateUrl: './login.component.html',
styleUrls: ['./login.component.scss']
})
export class LoginComponent implements OnInit {
constructor(private database:DatabaseService,private router:Router) { }
onFormSubmit(myForm:NgForm){
if(myForm.valid){
//Validation Here Works Fine
//Button Action Not Working
if(this.database.login(myForm.value['email'],myForm.value['password'])){
this.router.navigate(['dashboard']);
}
}
}
ngOnInit() {
}
}
HTML LOGIN
<form #myForm="ngForm" (submit)="onFormSubmit(myForm)" method="GET">
<input type="text" name="email" placeholder="Enter email">
<input type="password" name="password" placeholder="Enter Password">
<button type="submit">Log In</button>
</form>
<router-outlet></router-outlet>
USER MODULE
export class User{
email:string;
password:string;
};