-1

My add in has been set up on Azure for Application authentication , but i am having problems writing this in javascript to get my authentication token to then call the Graph API without the need for the user to sign in via the pop up modal that appears (i had previously written it so the pop up modal appears, the user signs in and all Graph API calls work ok, but a scenario where a user doesnt have to sign in is much smoother and a better user experience. Previous way included below)

    const msalConfig = {
        auth: {
            clientId: "cID", // Client Id of the registered application
            redirectUri: "https://localhost:111", // debug

        }
    };

    const graphScopes = ["user.read", "user.readWrite", "mail.send", "mail.send.shared", "mail.readWrite"/*, "files.readWrite.All"*/]; // An array of graph scopes
    const msalApplication = new Msal.UserAgentApplication(msalConfig);
    const options = new MicrosoftGraph.MSALAuthenticationProviderOptions(graphScopes);
    const authProvider = new MicrosoftGraph.ImplicitMSALAuthenticationProvider(msalApplication, options);
    const options2 = {
        authProvider // An instance created from previous step
    };
    const Client = MicrosoftGraph.Client;
    var client = Client.initWithMiddleware(options2);

I am looking through some microsoft docs, and they say in order to get this working without the need for user sign in, i am to include the client secret in my POST to get the authentication token? Am i looking into the wrong docs because that is something i cannot include in the JS for security reasons surely?

See below the doc im following

https://learn.microsoft.com/en-us/graph/auth-v2-user

Any insight is much appreciated, thanks!

944jmc
  • 39
  • 7

1 Answers1

0

I would suggest you to look at this sample, build a JavaScript single-page application (SPA) that signs in users using MSAL and calls Microsoft Graph by using the authorization code flow with PKCE. The SPA you build uses the Microsoft Authentication Library (MSAL) for JavaScript v2.0.

Note: MSAL.js 2.0 improves on older MSAL.js 1.0 by supporting the authorization code flow in the browser instead of the implicit grant flow.

Dev
  • 2,428
  • 2
  • 14
  • 15