diff --git a/frontend/plugins/msal.ts b/frontend/plugins/msal.ts index 4df38cb..b5ed040 100644 --- a/frontend/plugins/msal.ts +++ b/frontend/plugins/msal.ts @@ -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('useMSAL.accounts') const interactionStatus = useState('useMSAL.interactionStatus') @@ -134,6 +138,24 @@ export default defineNuxtPlugin(async (_nuxtApp) => { throw new Error('failed to init MSAL instance', { cause: error }) } + const signOut = (): Promise => { + 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 => { if (!response?.account) { return await Promise.resolve(response) @@ -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 @@ -278,24 +296,6 @@ export default defineNuxtPlugin(async (_nuxtApp) => { }) } - const signOut = (): Promise => { - 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: {