【问题标题】:How do I refresh the custom claims when its set in a google cloud function?如何在谷歌云功能中设置自定义声明?
【发布时间】:2018-06-11 06:46:13
【问题描述】:

我正在尝试在 firebase 的 google 函数中设置自定义声明。这是我的代码。

admin.auth().setCustomUserClaims(user.uid,{
                activeNetworkProfile: networkProfile,
                activeCompanyProfile: companyProfile,
                activeUserProfile: profiles[0]
            });

在客户端我有以下内容:

public updateProfiles(){
    return this.afAuth.idToken.pipe(
      switchMap(idToken => this.httpClient.post<any>('https://us-central1-company-database.cloudfunctions.net/updateProfiles',{
        "token": idToken,
        "np":"hzlNpVEdwqOKRTcsdfPG",
        "cp":"D7nKYrS235A9vnvfB9oD"
      }))
    );
  } 

然后对已经进行的更改进行测试:

 public validateToken(){
    return this.afAuth.idToken.pipe(
      switchMap(idToken => this.httpClient.post<any>('https://us-central1-company-database.cloudfunctions.net/validateToken',{
        "token": idToken
      }))
    );
  }

在服务器上我有以下内容:

 export const validateToken = functions.https.onRequest((req, res) => {
    return cors(req, res, () => {
        const db = admin.firestore();
        let token = req.body.token;
        admin.auth().verifyIdToken(token).then(decodedToken=>{
        res.status(200).send(decodedToken);
        })
        .catch(error=>{
            res.status(401).send(error.message);
        });
    });
 });

我没有收到回复中的声明。我尝试通过将参数设置为 true 来强制 angularfire2 中的更新

public getToken(){
    return this.afAuth.auth.currentUser.getIdToken(true);
  }

但这并没有改变任何东西,如果它导致 firebase 令牌更新进入无限循环。

【问题讨论】:

    标签: angular firebase google-cloud-firestore angularfire2


    【解决方案1】:

    我可以通过在设置声明后添加以下代码来修复。

     const metadataRef = admin.database().ref("metadata/" + user.uid);
     return metadataRef.set({refreshTime: new Date().getTime()});
    

    【讨论】:

      猜你喜欢
      • 2020-08-19
      • 2021-04-22
      • 1970-01-01
      • 1970-01-01
      • 2020-11-07
      • 2022-01-23
      • 2019-08-05
      • 2022-11-10
      • 2017-11-14
      相关资源
      最近更新 更多