how to verify azure access token using Nodejs ?

MarwanSamrout-7915 40 Reputation points
2024-01-22T08:18:57.19+00:00

i have downloaded angular sample microsoft-authentication-library-for-js\samples\msal-angular-v3-samples\angular16-sample-app from github and im trying to make a backend to protect my routes using azure ad
in angular im getting access token using this function

      this.authService.acquireTokenSilent({
        scopes: ['user.read']
    }).subscribe({
        next: (result) => {
            console.log('Access Token:', result.accessToken);
        },
        error: (error) => {
            console.error('Error acquiring token:', error);
        }
    });

now when my angular app try to call nodejs server i want to verify that token, how can i do that with best practices? and thank you.

Microsoft Entra ID
Microsoft Entra ID
A Microsoft Entra identity service that provides identity management and access control capabilities. Replaces Azure Active Directory.
20,631 questions
0 comments No comments
{count} votes

Accepted answer
  1. Shweta Mathur 29,756 Reputation points Microsoft Employee
    2024-01-22T10:58:32.6933333+00:00

    Hi @Marwan Samrout , Thaks for reaching out. To verify the access token received from the client-side in your Node.js server, you can use the passport-azure-ad library. This library provides a middleware that can be used to authenticate the incoming requests based on the access token.

    https://github.com/Azure-Samples/ms-identity-javascript-angular-tutorial/tree/main/3-Authorization-II/1-call-api

    Hope this will help.
    Thanks,

    Shweta


    Please remember to "Accept Answer" if answer helped you.


0 additional answers

Sort by: Most helpful