Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Docs, example and ease of use suggestion for acquireTokenSilent #2

Open
Josh-Ring-SD opened this issue May 30, 2023 · 5 comments
Open

Comments

@Josh-Ring-SD
Copy link

Josh-Ring-SD commented May 30, 2023

import {
    createEffect,
    createResource,
    createMemo,
} from 'solid-js'

import { useMsal } from "msal-community-solid";
import { SilentRequest } from '@azure/msal-browser';

const [readContext, writeContext] = useMsal()

createEffect(() => {
    writeContext.instance.setActiveAccount(readContext.accounts[0])
})

const tokenRequestConfig = createMemo(() => ({
    scopes: msalConfig.scopes,
    redirectUri: msalConfig.auth.redirectUri,
    authority: msalConfig.auth.authority,
    account: readContext.activeAccount,
    forceRefresh: false,
    cacheLookupPolicy: 1,
    prompt: "none"
} as SilentRequest))


// creating this async wrapper and using with createResource 
//  could be a bit simpler for the end user? Perhaps we could add something for convenience?
// Or do you think we could achieve this better with an example like this code here?
async function asyncWrapper(silentRequest: SilentRequest) {
    return await writeContext.instance.acquireTokenSilent(silentRequest)
}

const [response] = createResource(
    tokenRequestConfig,
    asyncWrapper
)

What are your thoughts?

@Josh-Ring-SD
Copy link
Author

I suppose this could be cleaner:

const [response] = createResource(
    tokenRequestConfig,
    async (tokenRequestConfig) => await writeContext.instance.acquireTokenSilent(tokenRequestConfig)
)

@saraelsa
Copy link
Owner

Yeah, I think writing a small arrow function would make more sense than an a very simple wrapper for a very specific scenario. It also makes it clearer what's happening with the inline arrow function (a token is being acquired based on the config).

@Josh-Ring-SD
Copy link
Author

Based on how I have used the library would be happy to put a few docs together to show how to use this. Are you interested in a PR?
Would it sit outside of the main readme and be linked to it, or be in a usage examples section?

@saraelsa
Copy link
Owner

For sure, you'd be very welcome to do that! I think the best course of action would be a readme entry, or possibility within doc comments in the codebase.

@Josh-Ring-SD
Copy link
Author

Raised PR here: #3

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants