Skip to content

Commit

Permalink
Reorder MSAL plugin functions
Browse files Browse the repository at this point in the history
I saw an error in dev (which I haven't been able to replicate) about some minified function/variable begin referenced before its definition, and I think it might be this, which can now be eagerly invoked in `handleResponse` as we do initialization synchronously

In any case, it probably doesn't hurt
  • Loading branch information
bcspragu committed Nov 17, 2023
1 parent 1233a47 commit 247dfc1
Showing 1 changed file with 22 additions and 22 deletions.
44 changes: 22 additions & 22 deletions frontend/plugins/msal.ts
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,10 @@ export default defineNuxtPlugin(async (_nuxtApp) => {

const scopes: string[] = ['openid', 'profile', 'offline_access', msalConfig.auth.clientId]

const router = useRouter()
const { userClientWithAuth } = useAPI()
const localePath = useLocalePath()

const accounts = useState<AccountInfo[] | undefined>('useMSAL.accounts')
const interactionStatus = useState<InteractionStatus | undefined>('useMSAL.interactionStatus')

Expand Down Expand Up @@ -134,6 +138,24 @@ export default defineNuxtPlugin(async (_nuxtApp) => {
throw new Error('failed to init MSAL instance', { cause: error })
}

const signOut = (): Promise<void> => {
const logoutRequest = {
postLogoutRedirectUri: msalConfig.auth.redirectUri,
mainWindowRedirectUri: msalConfig.auth.logoutUri,
}
const userClient = userClientWithAuth('') // Logging out doesn't require auth.
return Promise.all([
userClient.logout(),
instance.logoutPopup(logoutRequest),
])
.catch((e) => { console.log('failed to log out', e) })
.then(() => { /* cast to void */ })
.finally(() => {
isAuthenticated.value = false
void router.push(localePath('/'))
})
}

const handleResponse = async (response: AuthenticationResult, force = false): Promise<AuthenticationResult> => {
if (!response?.account) {
return await Promise.resolve(response)
Expand Down Expand Up @@ -183,10 +205,6 @@ export default defineNuxtPlugin(async (_nuxtApp) => {
console.log('multiple accounts found, user needs to select one')
}

const router = useRouter()
const { userClientWithAuth } = useAPI()
const localePath = useLocalePath()

const account = computed(() => {
if (!accounts.value) {
return undefined
Expand Down Expand Up @@ -278,24 +296,6 @@ export default defineNuxtPlugin(async (_nuxtApp) => {
})
}

const signOut = (): Promise<void> => {
const logoutRequest = {
postLogoutRedirectUri: msalConfig.auth.redirectUri,
mainWindowRedirectUri: msalConfig.auth.logoutUri,
}
const userClient = userClientWithAuth('') // Logging out doesn't require auth.
return Promise.all([
userClient.logout(),
instance.logoutPopup(logoutRequest),
])
.catch((e) => { console.log('failed to log out', e) })
.then(() => { /* cast to void */ })
.finally(() => {
isAuthenticated.value = false
void router.push(localePath('/'))
})
}

return {
provide: {
msal: {
Expand Down

0 comments on commit 247dfc1

Please sign in to comment.