authentication.ts 2.8 KB

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