Verify JWT From Azure b2C in a Azure Node Function

Tim Anderson 60 Reputation points
2025-03-03T20:32:05.99+00:00

Hi there,

We have standard paid support. Trying to validate (and get decoded info) a JWT in an Azure Node Function api called from my react app. I get invalid JWT. Here's my code below:

 if(!token) {
        return {'error': 'permission denied - no authorization'};
    }

    //then, validate user is id'd and return the user ID
    const client = jwksClient({
        jwksUri: "https://xxxxxxxx.b2clogin.com/xxxx.onmicrosoft.com/b2c_1_signin1/discovery/v2.0/keys"
    });

    function getKey(header, callback) {

        client.getSigningKey(header.kid, (err, key) => {

            if (err) {

                return callback(err);

            }

            const signingKey = key.getPublicKey();

            callback(null, signingKey);

        });

    }

    return new Promise((resolve) => {
        jwt.verify(token, getKey, { algorithms: ['RS256'] }, (err, decoded) => {
          if (err) {
            // eslint-disable-next-line no-console
            ctx.log('error: ', JSON.stringify(err));
            resolve({'error': err});
          } else {
            ctx.log('decoded: ', decoded);
            resolve(decoded);
          }
        });
    });

}
Azure App Service
Azure App Service
Azure App Service is a service used to create and deploy scalable, mission-critical web apps.
8,460 questions
{count} votes

Accepted answer
  1. Akhilesh Vallamkonda 12,975 Reputation points Microsoft External Staff
    2025-03-03T21:37:33.24+00:00

    Hi @Tim Anderson
    I'm glad that you were able to resolve your issue and thank you for posting your solution so that others experiencing the same thing can easily reference this! Since the Microsoft Q&A community has a policy that "The question author cannot accept their own answer. They can only accept answers by others ", I'll repost your solution in case you'd like to "Accept " the answer.

    Issue:

    Verify JWT From Azure b2C in a Azure Node Function
    When you trying to validate (and get decoded info) a JWT in an Azure Node Function api called from my react app. you got get invalid JWT

    Solution:

    You have fixed your issue, by split the auth bearer token to get just the token without "Bearer "

    If you have any other questions or are still running into more issues, please let me know. Thank you again for your time and patience throughout this issue.

    Please remember to "Accept Answer" if any answer/reply helped, so that others in the community facing similar issues can easily find the solution.

    0 comments No comments

0 additional answers

Sort by: Most helpful

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.