useAfterAuthPlugins.ts 1.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546
  1. // Copyright (C) 2012-2024 Zammad Foundation, https://zammad-foundation.org/
  2. import { keyBy } from 'lodash-es'
  3. import { computed, ref } from 'vue'
  4. import type {
  5. EnumAfterAuthType,
  6. SessionAfterAuth,
  7. } from '#shared/graphql/types.ts'
  8. import type { AfterAuthPlugin } from '../types.ts'
  9. import type { Router } from 'vue-router'
  10. const pluginsModules = import.meta.glob<AfterAuthPlugin>('../plugins/*.ts', {
  11. eager: true,
  12. import: 'default',
  13. })
  14. const pluginsFiles = Object.values(pluginsModules)
  15. const plugins = keyBy(pluginsFiles, 'name')
  16. const currentPlugin = ref<EnumAfterAuthType | null>(null)
  17. const currentPluginData = ref<Record<string, unknown> | null>(null)
  18. export const ensureAfterAuth = async (
  19. router: Router,
  20. afterAuth: SessionAfterAuth,
  21. ) => {
  22. currentPlugin.value = afterAuth.type
  23. currentPluginData.value = afterAuth.data || null
  24. await router.replace('/login/after-auth')
  25. }
  26. export const useAfterAuthPlugins = () => {
  27. const plugin = computed(() => {
  28. if (!currentPlugin.value) return null
  29. return plugins[currentPlugin.value] || null
  30. })
  31. return {
  32. currentPlugin: plugin,
  33. data: currentPluginData,
  34. plugins,
  35. }
  36. }