Firebase Custom Claims doesn't propagate

前端 未结 2 443
渐次进展
渐次进展 2020-12-07 05:29

I\'m working in an Angular6 app with angularfire2. I\'m setting the roles as custom claims in user creation, but it doesn\'t seem to propagate.

When I\'m creating th

相关标签:
2条回答
  • 2020-12-07 05:47

    For me it simply worked taking the advice from one of the comments:

    // --------
    // Frontend
    // --------
    
    // Triggering the cloud function
    const url: string = 'url-to-your-cloud-function'
    await this.http.post<unknown>(url, {}).toPromise();
    
    
    // After cloud function was run and custom claim was set -> refresh the id token
    // The 'currentUser' is a reference to the firebase user
    await this.authService.currentUser.getIdToken(true);
    
    // --------
    // Cloud Function - createSubscription
    // --------
    
    const createSubscription = () => {  
      await admin.auth().setCustomUserClaims(userId, {
        subscriber: true
      })
    }
    
    
    0 讨论(0)
  • 2020-12-07 05:58

    When the user is signed in, they get an ID token that is valid for about an hour. If you set a custom claim, their profile is updated immediately, but their ID token is not auto-updated. So you'll need to refresh their ID token to get the new custom claims.

    As far as I know this ID token is only refreshed by calling getIdTokenResult if it has expired. If that's the cause, calling user.reload() and then getting the ID token should give you the updated claims.

    0 讨论(0)
提交回复
热议问题