I have a login view in Angular. I have made:
an UserService to fetch User Data.
an AuthService for checking valid User.
So from login view, I pass the username to AuthService and it contacts with UserService for the data and manipulates data and return a boolean
value whether the user is valid or not.
code goes below from login view is as follows:
AuthenticationService:
this.authenticationService.checkValidLogin(userName,password);
AuthenticationService:
checkValidLogin(username: string, password: string) {
this.userService.getUserByEmail(username).snapshotChanges().pipe(map(data => data.map(c => ({ key: c.payload.doc.id, ...c.payload.doc.data() })))).subscribe(res => {
if (res[0].password === password) {
return true;
} else {
return false;
}
});
}
I have return true
or false
but that wont return to the method..!!
I want to return true
or false
value from subscription or what I am missing here?
Since the code is asynchronous, you have to return the
Observable
and subscribe to it when you want to check its validity.Then when you want to use it: