useAfterAuthPlugins.ts 1.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344
  1. // Copyright (C) 2012-2024 Zammad Foundation, https://zammad-foundation.org/
  2. import { keyBy } from 'lodash-es'
  3. import type {
  4. EnumAfterAuthType,
  5. SessionAfterAuth,
  6. } from '#shared/graphql/types.ts'
  7. import { computed, ref } from 'vue'
  8. import type { Router } from 'vue-router'
  9. import type { AfterAuthPlugin } from '../types.ts'
  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. }