Skip to content

Commit

Permalink
fix: use new magic link format (#34)
Browse files Browse the repository at this point in the history
* fix: use new magic link format

* misc changes

* added small middleware

* testing something out

* added new middleware

* full page reload

* changed middleware name + provided more comments
  • Loading branch information
JazzarKarim authored Dec 4, 2024
1 parent 7da1a93 commit dc1f16e
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 8 deletions.
16 changes: 16 additions & 0 deletions business-registry-dashboard/app/middleware/00.magic-link.global.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
export default defineNuxtRouteMiddleware((to) => {
// Check if the route is an affiliation invitation link without locale prefix
if (to.path.startsWith('/affiliationInvitation/acceptToken') && !to.path.startsWith('/en-CA/')) {
// Convert query params to URLSearchParams to maintain all existing query parameters
const searchParams = new URLSearchParams(to.query as Record<string, string>)
const queryString = searchParams.toString()

// Construct the full URL with the en-CA locale prefix and existing query parameters
const newUrl = `/en-CA${to.path}${queryString ? '?' + queryString : ''}`
// Force a full page reload with the new URL to ensure proper locale handling
window.location.href = newUrl

// Prevent Nuxt from handling the navigation since we're doing a full page reload
return abortNavigation()
}
})
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<script setup lang="ts">
import { useRoute } from 'vue-router'
import { StatusCodes } from 'http-status-codes'
const { t } = useI18n()
const affStore = useAffiliationsStore()
Expand All @@ -26,9 +26,9 @@ definePageMeta({
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) {
Expand All @@ -54,18 +54,18 @@ const parseUrlAndAddAffiliation = async (token: any, base64Token: string) => {
} 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
2 changes: 1 addition & 1 deletion business-registry-dashboard/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"name": "business-registry-dashboard",
"private": true,
"type": "module",
"version": "0.0.4",
"version": "0.0.5",
"scripts": {
"build-check": "nuxt build",
"build": "nuxt generate",
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 dc1f16e

Please sign in to comment.