Ви не можете вибрати більше 25 тем Теми мають розпочинатися з літери або цифри, можуть містити дефіси (-) і не повинні перевищувати 35 символів.

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  1. import WaveUI from '../../node_modules/wave-ui/src/wave-ui/core'
  2. import { authenticator, currentProfile } from '../services'
  3. const DEV_MODE = import.meta.env.VITE_DEV == 'true'
  4. async function log(to) {
  5. if (!currentProfile.isLoggedIn || !currentProfile.isComplete) {
  6. console.info(
  7. `[Guard Status debug]: Profile: ${currentProfile.id.value} | Login: ${currentProfile.isLoggedIn} | Complete: ${currentProfile.isComplete}`,
  8. )
  9. }
  10. console.info('[Guard Status debug]: being routed to:', to.fullPath)
  11. }
  12. const loginIfToken = async () => {
  13. const sessionData = await authenticator.isValidSession()
  14. if (
  15. sessionData?.profileId &&
  16. sessionData?.sessionToken &&
  17. !currentProfile.isLoggedIn
  18. ) {
  19. await currentProfile.login(
  20. sessionData.profileId,
  21. WaveUI.instance.notify,
  22. sessionData.sessionToken,
  23. )
  24. }
  25. }
  26. const checkLoginStatus = async (destination, nextCb) => {
  27. await loginIfToken()
  28. log(destination)
  29. if (DEV_MODE) {
  30. nextCb()
  31. } else if (
  32. destination.meta.requiresAuth &&
  33. !currentProfile.isLoggedIn &&
  34. !currentProfile.isComplete
  35. ) {
  36. nextCb('/onboarding')
  37. } else if (destination.meta.requiresAuth && !currentProfile.isLoggedIn) {
  38. nextCb('/login')
  39. } else {
  40. nextCb()
  41. }
  42. }
  43. export { checkLoginStatus }