Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Angular2 Firebase Set User Custom Propertise

I've been trying to set user custom properties or attributes using auth.updateProfile method but it only save displayName property because it's mentioned in its docs :

this.af.auth.createUser({
   email: formData.value.email,
   password: formData.value.password,
}).then((user) => {
   let userProfile = formData.value;
   userProfile.role = AuthService.ROLE_PATIENT;
   userProfile.displayName = `${formData.value.firstName} ${formData.value.lastName}`;
   user.auth.updateProfile(userProfile).then(function(){
   this.router.navigate(['/']);  
}); 

Now Problem how can i set custom properties of the user so i can get them in FirebaseAuthState

Question: Why i need to save custom properties with each user?

Answer: Because i'm working with Auth Guard to activate a particular route using role property which is saved in the database example:

canActivate(route: ActivatedRouteSnapshot,
              state: RouterStateSnapshot): Observable<boolean> | Promise<boolean> | boolean {
    //Currently I'm using this which only check that user is logged in or not
    return this.auth
      .take(1)
      .map((authState: FirebaseAuthState) => !!authState)
      .do(authenticated => {
        if (!authenticated) this.router.navigate(['/login']);
      });   
  }

But i want one more check to validate the user has the role of patient which i've saved while creating the user above eg: return user.role == 'patient'

like image 373
Touqeer Shafi Avatar asked Nov 07 '22 23:11

Touqeer Shafi


1 Answers

The existing identity providers in Firebase Authentication have no way to add custom properties. If you want to add custom properties, you will either have to implement a custom identify provider (see minting custom tokens in a trusted process and signing in with a custom token) OR do a client-side look-up of the additional information from an alternate data source (such as the Firebase Database).

like image 71
Frank van Puffelen Avatar answered Nov 15 '22 07:11

Frank van Puffelen