Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. import { currentProfile, authenticator } from '../services'
  2. import WaveUI from '../../node_modules/wave-ui/src/wave-ui/core'
  3. const DEV_MODE = import.meta.env.VITE_DEV == 'true'
  4. async function log(to) {
  5. if (DEV_MODE) {
  6. if (!currentProfile.isLoggedIn || !currentProfile.isComplete) {
  7. console.info(
  8. `[Guard Status debug]: Profile: ${currentProfile.id.value} | Login: ${currentProfile.isLoggedIn} | Complete: ${currentProfile.isComplete}`,
  9. )
  10. }
  11. console.info('[Guard Status debug]: being routed to:', to.fullPath)
  12. }
  13. }
  14. // TODO: move to utils/index.js and import to this file, OnboardingView.vue and VerifyView.vue
  15. const grabStoredCookie = cookieKey => {
  16. const cookies = document.cookie.split('; ').reduce((prev, current) => {
  17. const [name, ...value] = current.split('=')
  18. prev[name] = value.join('=')
  19. return prev
  20. }, {})
  21. const cookieVal = cookieKey in cookies ? cookies[`${cookieKey}`] : undefined
  22. return cookieVal
  23. }
  24. const verifySession = async () => {
  25. const hashedAccessToken = grabStoredCookie('siimee_access')
  26. if (!hashedAccessToken)
  27. return console.warn('WARNING :=> accessToken is not defined')
  28. const validatedToken = await authenticator.validateSession(
  29. hashedAccessToken,
  30. )
  31. if (validatedToken.error) {
  32. console.error('ERROR :=>', validatedToken.error)
  33. } else {
  34. return validatedToken
  35. }
  36. }
  37. const loginIfToken = async () => {
  38. const sessionData = await verifySession()
  39. if (
  40. sessionData?.profileId &&
  41. sessionData?.accessToken &&
  42. !currentProfile.isLoggedIn
  43. ) {
  44. await currentProfile.login(
  45. sessionData.profileId,
  46. WaveUI.notify,
  47. sessionData.accessToken,
  48. )
  49. }
  50. }
  51. const checkLoginStatus = async (destination, nextCb) => {
  52. await loginIfToken()
  53. log(destination)
  54. if (DEV_MODE) {
  55. nextCb()
  56. } else if (
  57. destination.meta.requiresCompleteProfile &&
  58. !currentProfile.isLoggedIn &&
  59. !currentProfile.isComplete
  60. ) {
  61. nextCb('onboarding')
  62. } else if (
  63. destination.meta.requiresCompleteProfile &&
  64. destination.meta.requiresAuth &&
  65. !currentProfile.isLoggedIn
  66. ) {
  67. nextCb('login')
  68. } else {
  69. nextCb()
  70. }
  71. }
  72. export { checkLoginStatus }