1

I have a page template that contains the following sub-component:

<template>
 <ManageVacanciesAndMatchesTabVacancies @loading-data="setIsTabDataLoading" />
</template>

And the script setup syntax is as follows:

<script setup>
import { ref, onMounted, computed } from 'vue'
import { isOrganisationAuthor, isSuperAuthor } from '../../functions/frontendAuthUtils.js'
import { useCheckAuth } from '@/composables/useCheckAuth.js'
import ManageVacanciesAndMatchesTabVacancies from '@/components/vacancy/ManageVacanciesAndMatchesTabVacancies.vue'

const nuxtApp = useNuxtApp()
const router = useRouter()
const route = useRoute()

const orgOrSuperAuthor = (user) => {
  return user && ((nuxtApp.$globalState.toggledOrganisationInfo && isOrganisationAuthor(user, nuxtApp.$globalState.toggledOrganisationId)) || isSuperAuthor(user))
}

useCheckAuth(orgOrSuperAuthor)
</script>

useCheckAuth is defined as a composable as such:

export const useCheckAuth = (checkAuthFunction) => {
  let nuxtApp = useNuxtApp()
  if (!checkAuthFunction) {
    throw new Error('You need to pass a function to check the authorization')
  }
  if (!checkAuthFunction(nuxtApp.$auth.user)) {
    nuxtApp.$userNotAuthorized()
  }
}

And ultimately the userNotAuthorized is defined as a plugin as such:

userNotAuthorized: () => {
  nuxtApp.$setStatusCode(401)
  let route = nuxtApp._route
  let fullPath = route.fullPath
  if (nuxtApp.$auth.user) {
    if (!fullPath.startsWith(nuxtApp.$findPath('/onvoldoende-rechten'))) {
      navigateTo(nuxtApp.$findRoute('/onvoldoende-rechten', {}, null, { redirectUrl: route.fullPath }), { statusCode: 401 })
    }
  } else if (!fullPath.startsWith(nuxtApp.$findPath('/login'))) {
    navigateTo(nuxtApp.$findRoute('/login', {}, null, { redirectUrl: route.fullPath }), { statusCode: 401 })
  }
},

The problem is that the component ManageVacanciesAndMatchesTabVacancies.vue contains a lot of code in the script setup syntax. Which expects the user to be logged in (it has to do backend calls and others).

But when the useCheckAuth method executes the userNotAuthorized function, all the other code in the script setup block is still being executed. Which ends up for me in the component giving an error rather than the user being redirected to the login page.

Is it possible to stop all code execution after the authCheck returns false?

2
  • rather than worrying about stopping, it sounds like it'd be better to not start execution until the auth value is determined, using async/await and Promises. Commented Apr 25, 2024 at 13:45
  • @yoduh how does that work exactly? I've tried making the useCheckAuth function async and making the nuxt3 navigateTo also await. But this did not solve the problem, it was the exact same behavior. Commented Apr 25, 2024 at 14:22

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.