Skip to content

Commit da66f6e

Browse files
committed
Reorder MSAL plugin functions
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
1 parent c115a83 commit da66f6e

File tree

1 file changed

+22
-22
lines changed

1 file changed

+22
-22
lines changed

frontend/plugins/msal.ts

Lines changed: 22 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,10 @@ export default defineNuxtPlugin(async (_nuxtApp) => {
100100

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

103+
const router = useRouter()
104+
const { userClientWithAuth } = useAPI()
105+
const localePath = useLocalePath()
106+
103107
const accounts = useState<AccountInfo[] | undefined>('useMSAL.accounts')
104108
const interactionStatus = useState<InteractionStatus | undefined>('useMSAL.interactionStatus')
105109

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

141+
const signOut = (): Promise<void> => {
142+
const logoutRequest = {
143+
postLogoutRedirectUri: msalConfig.auth.redirectUri,
144+
mainWindowRedirectUri: msalConfig.auth.logoutUri,
145+
}
146+
const userClient = userClientWithAuth('') // Logging out doesn't require auth.
147+
return Promise.all([
148+
userClient.logout(),
149+
instance.logoutPopup(logoutRequest),
150+
])
151+
.catch((e) => { console.log('failed to log out', e) })
152+
.then(() => { /* cast to void */ })
153+
.finally(() => {
154+
isAuthenticated.value = false
155+
void router.push(localePath('/'))
156+
})
157+
}
158+
137159
const handleResponse = async (response: AuthenticationResult, force = false): Promise<AuthenticationResult> => {
138160
if (!response?.account) {
139161
return await Promise.resolve(response)
@@ -183,10 +205,6 @@ export default defineNuxtPlugin(async (_nuxtApp) => {
183205
console.log('multiple accounts found, user needs to select one')
184206
}
185207

186-
const router = useRouter()
187-
const { userClientWithAuth } = useAPI()
188-
const localePath = useLocalePath()
189-
190208
const account = computed(() => {
191209
if (!accounts.value) {
192210
return undefined
@@ -278,24 +296,6 @@ export default defineNuxtPlugin(async (_nuxtApp) => {
278296
})
279297
}
280298

281-
const signOut = (): Promise<void> => {
282-
const logoutRequest = {
283-
postLogoutRedirectUri: msalConfig.auth.redirectUri,
284-
mainWindowRedirectUri: msalConfig.auth.logoutUri,
285-
}
286-
const userClient = userClientWithAuth('') // Logging out doesn't require auth.
287-
return Promise.all([
288-
userClient.logout(),
289-
instance.logoutPopup(logoutRequest),
290-
])
291-
.catch((e) => { console.log('failed to log out', e) })
292-
.then(() => { /* cast to void */ })
293-
.finally(() => {
294-
isAuthenticated.value = false
295-
void router.push(localePath('/'))
296-
})
297-
}
298-
299299
return {
300300
provide: {
301301
msal: {

0 commit comments

Comments
 (0)