Refer to the sample code snippet of the graph JS SDK, which uses the auth code flow for interactive authentication of your guest users:
Using @azure/msal-browser for browser applications
const {
PublicClientApplication,
InteractionType,
AccountInfo
} = require("@azure/msal-browser");
const {
AuthCodeMSALBrowserAuthenticationProvider,
AuthCodeMSALBrowserAuthenticationProviderOptions
} = require("@microsoft/microsoft-graph-client/authProviders/authCodeMsalBrowser");
const options: AuthCodeMSALBrowserAuthenticationProviderOptions = {
account: account, // the AccountInfo instance to acquire the token for.
interactionType: InteractionType.PopUp, // msal-browser InteractionType
scopes: ["user.read", "mail.send"] // example of the scopes to be passed
}
// Pass the PublicClientApplication instance from step 2 to create AuthCodeMSALBrowserAuthenticationProvider instance
const authProvider = new AuthCodeMSALBrowserAuthenticationProvider(publicClientApplication, options),
Using @azure/identity for server-side applications
const {
Client
} = require("@microsoft/microsoft-graph-client");
const {
TokenCredentialAuthenticationProvider
} = require("@microsoft/microsoft-graph-client/authProviders/azureTokenCredentials");
const {
AuthorizationCodeCredential
} = require("@azure/identity");
const credential = new AuthorizationCodeCredential(
"<YOUR_TENANT_ID>",
"<YOUR_CLIENT_ID>",
"<AUTH_CODE_FROM_QUERY_PARAMETERS>",
"<REDIRECT_URL>"
);
const authProvider = new TokenCredentialAuthenticationProvider(credential, {
scopes: [scopes]
});
Hope this helps. If the reply is helpful, please click Accept Answer and kindly upvote it. If you have additional questions about this answer, please click Comment.