Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

241
Visualizações
Firebase - Why is the claim not added to the user attributes?

I'm adding the claim to a user's profile that he or she paid for something, though, after the payment this attribute isn't visible. I'm running the functions on an emulator on a local host.

This is the code I'm using:

If the paypal function has been handled succesfully through paypalHandleOrder, then the function addPaidClaim is invoked.

  onApprove: (data, actions) => {
    paypalHandleOrder({ orderId: data.orderID }).then( 
      addPaidClaim(currentUser).then(
        alert("THANKS FOR ORDERING!"),
        // currentUser.getIdTokenResult().then(idTokenResult => {
        //   console.log(idTokenResult.claims)
        // })
      )
      .catch((err) => {
        return err;
      })
    );}

addPaidClaim is a firebase cloud function, which goes as follows:

  exports.addPaidClaim = functions.https.onCall((data, context) => {
    // get user and add custom claim (paid)
    return admin.auth().setCustomUserClaims(data.uid, {
      paid: true,
    }).then(() => {
      return {
        message: `Success! ${data.email} has paid the course`,
      };
    }).catch((err) => {
      return err;
    });
  });

I've refreshed the page and checked the user attributes afterwards through console.log on the user to see if the attribute had been added, but this is not the case. I can't find attribute paid inside the idTokenResult object. What should I do? I also find it hard to make sense of what's happening inside the function addPaidClaim. It's not returning an error when I look at the logs on my firebase console, and not much information is given, besides that the function has been invoked.

about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

Okay, I know this question is pretty old. But I found a way just yesterday after 3 days searching over the solution. After we set up a new claim for a new user using, we need to refresh the client's getIdTokenResult(true) in the app. These are the ways I did it in Flutter Dart until a new user with updated claim managed to use it:

FirebaseAuth auth = FirebaseAuth.instance;

Future<Map<String, dynamic>> signInWithGoogle() async {
   Map<String, dynamic> output = {};
   final googleUser = await googleSignIn.signIn();
   if (googleUser == null) {
        log("Firebase => Gmail account doesn't exist");
   } else {
     final googleAuth = await googleUser.authentication;
     final credential = GoogleAuthProvider.credential(
        idToken: googleAuth.idToken,
        accessToken: googleAuth.accessToken,
     );
     await auth.signInWithCredential(credential).then((values) async {
        await userAuth(credential).then((value) =>          
          value.addAll(output));
     });
   }
   return output;
}

Future<Map<String, dynamic> userAuth (OAuthCredential credential) async {
   Map<String, dynamic> output = {};
   await auth.currentUser!.reauthenticateWithCredential(credential);
   await auth.currentUser!.reload();
   await auth.currentUser!.getIdTokenResult().then((result) => {
      if(result.claims!.isNotEmpty){
        //check your claim here
      } else {
        //assign log here
      }
   });
   return output;
}

about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda