authentication.ts 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. // Copyright (C) 2012-2023 Zammad Foundation, https://zammad-foundation.org/
  2. import { ref } from 'vue'
  3. import { defineStore } from 'pinia'
  4. import { useLocalStorage } from '@vueuse/core'
  5. import { MutationHandler } from '@shared/server/apollo/handler'
  6. import { useLoginMutation } from '@shared/graphql/mutations/login.api'
  7. import { useLogoutMutation } from '@shared/graphql/mutations/logout.api'
  8. import { clearApolloClientStore } from '@shared/server/apollo/client'
  9. import useFingerprint from '@shared/composables/useFingerprint'
  10. import testFlags from '@shared/utils/testFlags'
  11. import { useSessionStore } from './session'
  12. import { useApplicationStore } from './application'
  13. import { resetAndDisposeStores } from '.'
  14. export const useAuthenticationStore = defineStore(
  15. 'authentication',
  16. () => {
  17. const authenticated = useLocalStorage<boolean>('authenticated', false)
  18. const externalLogout = ref(false)
  19. const { fingerprint } = useFingerprint()
  20. const clearAuthentication = async (): Promise<void> => {
  21. await clearApolloClientStore()
  22. const session = useSessionStore()
  23. session.resetCurrentSession()
  24. authenticated.value = false
  25. resetAndDisposeStores(true)
  26. // Refresh the config after logout, to have only the non authenticated version.
  27. await useApplicationStore().resetAndGetConfig()
  28. // TODO... check for other things which must be removed/cleared during a logout.
  29. }
  30. const refreshAfterAuthentication = async (): Promise<void> => {
  31. await Promise.all([
  32. useApplicationStore().getConfig(),
  33. useSessionStore().getCurrentUser(),
  34. ])
  35. }
  36. const logout = async (): Promise<void> => {
  37. const logoutMutation = new MutationHandler(useLogoutMutation())
  38. const result = await logoutMutation.send()
  39. if (result?.logout?.success) {
  40. if (result.logout.externalLogoutUrl) {
  41. externalLogout.value = true
  42. authenticated.value = false
  43. window.location.href = result.logout.externalLogoutUrl
  44. return
  45. }
  46. await clearAuthentication()
  47. testFlags.set('logout.success')
  48. }
  49. }
  50. const login = async (
  51. login: string,
  52. password: string,
  53. rememberMe: boolean,
  54. ): Promise<void> => {
  55. const loginMutation = new MutationHandler(
  56. useLoginMutation({
  57. variables: {
  58. input: {
  59. login,
  60. password,
  61. rememberMe,
  62. },
  63. },
  64. context: {
  65. headers: {
  66. 'X-Browser-Fingerprint': fingerprint.value,
  67. },
  68. },
  69. }),
  70. )
  71. const result = await loginMutation.send()
  72. if (result?.login?.errors || !result) {
  73. return Promise.reject(result?.login?.errors)
  74. }
  75. const newSessionId = result.login?.sessionId || null
  76. if (newSessionId) {
  77. const session = useSessionStore()
  78. session.id = newSessionId
  79. authenticated.value = true
  80. }
  81. await refreshAfterAuthentication()
  82. return Promise.resolve()
  83. }
  84. return {
  85. authenticated,
  86. externalLogout,
  87. clearAuthentication,
  88. logout,
  89. login,
  90. refreshAfterAuthentication,
  91. }
  92. },
  93. {
  94. requiresAuth: false,
  95. },
  96. )