Skip to content

Commit

Permalink
fix: use new magic link format
Browse files Browse the repository at this point in the history
  • Loading branch information
JazzarKarim committed Dec 4, 2024
1 parent 7da1a93 commit 354c1df
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 14 deletions.
5 changes: 5 additions & 0 deletions business-registry-dashboard/app/middleware/01.auth.global.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,9 @@
export default defineNuxtRouteMiddleware((to) => {
if (to.path.includes('/affiliationInvitation/acceptToken')) {
window.location.href = to.fullPath
return
}

if (process.client) {
const { $keycloak } = useNuxtApp()
const localePath = useLocalePath()
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,10 @@
<script setup lang="ts">
import { useRoute } from 'vue-router'
import { StatusCodes } from 'http-status-codes'
const { t } = useI18n()
const affStore = useAffiliationsStore()
const brdModal = useBrdModals()
const route = useRoute()
const toast = useToast()
// Token parsing
const parseToken = (encodedToken: string): AffiliationToken => {
try {
Expand All @@ -18,54 +16,49 @@ const parseToken = (encodedToken: string): AffiliationToken => {
throw new Error('Invalid token format')
}
}
definePageMeta({
checkMagicLink: true,
order: 0
})
onMounted(async () => {
try {
const token = parseToken(route.params.token as string)
const token = parseToken(route.query.token as string)
// Parse the URL and try to add the affiliation
parseUrlAndAddAffiliation(token, route.params.token as string)
parseUrlAndAddAffiliation(token, route.query.token as string)
// Load affiliations to update the table
await affStore.loadAffiliations()
} catch (e) {
console.error('Error accepting affiliation invitation:', e)
}
})
// Function to parse the URL and extract the parameters, used for magic link email
const parseUrlAndAddAffiliation = async (token: any, base64Token: string) => {
if (!route.meta.checkMagicLink) {
return
}
const { businessIdentifier: identifier, id: invitationId } = token
try {
// 1. Accept invitation
const response = await affiliationInvitationService.acceptInvitation(invitationId, base64Token)
// 2. Adding magic link success
if (response.status === AffiliationInvitationStatus.Accepted) {
toast.add({ title: t('modal.manageBusiness.success.toast', { identifier }) }) // add success toast
}
} catch (error: any) {
console.error(error)
// 3. Unauthorized
if (error.response?.status === 401) {
if (error.response?.status === StatusCodes.UNAUTHORIZED) {
brdModal.openMagicLinkModal(t('error.magicLinkUnauthorized.title'), t('error.magicLinkUnauthorized.description'))
return
}
// 4. Expired
if (error.response?.status === 400 &&
if (error.response?.status === StatusCodes.BAD_REQUEST &&
error.response?._data.code === MagicLinkInvitationStatus.EXPIRED_AFFILIATION_INVITATION) {
brdModal.openMagicLinkModal(t('error.magicLinkExpired.title'), t('error.magicLinkExpired.description', { identifier }))
return
}
// 5. Already Added
if (error.response?.status === 400 &&
if (error.response?.status === StatusCodes.BAD_REQUEST &&
error.response?._data.code === MagicLinkInvitationStatus.ACTIONED_AFFILIATION_INVITATION) {
brdModal.openMagicLinkModal(t('error.magicLinkAlreadyAdded.title'), t('error.magicLinkAlreadyAdded.description', { identifier }))
return
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { describe, it, expect, vi, beforeEach } from 'vitest'
import { mount } from '@vue/test-utils'
import { mockNuxtImport } from '@nuxt/test-utils/runtime'
import AcceptToken from '~/pages/[encodedOrgId]/affiliationInvitation/acceptToken/[token].vue'
import AcceptToken from '~/pages/affiliationInvitation/acceptToken.vue'
import { enI18n } from '~~/tests/mocks/i18n'

// Mock useRoute to provide test route parameters and metadata
Expand Down

0 comments on commit 354c1df

Please sign in to comment.