During the account registration process, I initially set the default value to false
for the field IsApproved
. I need to create security rules that allow login only for users with IsApproved:true
, and redirect those with IsApproved:false
to the accessdenied
page. Here is the code used for user registration:
async register(){
if(this.firstname && this.lastname && this.email && this.password){
const loading =await this.loadingCtrl.create({
message:'Processing...',
spinner:'crescent',
showBackdrop:true
});
loading.present();
this.afauth.createUserWithEmailAndPassword(this.email,this.password)
.then((data)=>{
this.afs.collection('user').doc(data.user.uid).set({
'userId':data.user.uid,
'IsApproved':false,
'userEmail':this.email,
'userFirstname':this.firstname,
'userLastname':this.lastname
})
.then(()=>{
loading.dismiss();
this.toast('Registration Success','success');
this.router.navigate(['/login']);
})
.catch(error=>{
loading.dismiss();
this.toast(error.message,'danger')
})
})
}
}
How can I check whether the IsApproved
field is true
or false
when a user attempts to sign in
? Here is the code used for signing in:
async SignIn(email,password)
{
const loading =await this.LoadingCtrl.create({
message:'Authenticating..',
spinner:"crescent",
showBackdrop:true
});
loading.present();
this.afauth.setPersistence(firebase.default.auth.Auth.Persistence.LOCAL)
.then(()=>{
this.afauth.signInWithEmailAndPassword(email,password)
.then((data)=>{
if(!data.user){
loading.dismiss();
this.toast('Please check your credentials','warning');
this.afauth.signOut();
}else{
loading.dismiss();
this.router.navigate(['/menu']);
}
})
.catch(error=>{
loading.dismiss();
this.toast(error.message,'danger');
})
})
.catch(error=>{
loading.dismiss();
this.toast(error.message,'danger');
});
}
In my attempt to check using If-Else statement If(!data.user.IsApproved)
:
if(!data.user){
loading.dismiss();
this.toast('Please check your credentials','warning');
this.afauth.signOut();
}else{
loading.dismiss();
if(data.user.IsApproved===true){
this.router.navigate(['/menu']);
}else{
this.router.navigate(['/accessdenied']);
}
}
})
However, I encountered the error message:
Property 'IsApproved' does not exist on type 'User'.
My user model looks like this:
export interface User {
userId:string;
IsApproved:boolean;
userEmail:string;
userPhoto:string;
userFirstname:string;
userLastname:string;
}
I attempted to modify the security rules as follows:
allow read,write:if request.auth.uid.IsApproved!=false;
But it resulted in an unknown error occurred
.
I am aiming to restrict access to only users with IsApproved:true
, while routing others to the access denied page.