2026-06-16 19:47:28 +03:30
|
|
|
import { computed, watch } from 'vue'
|
|
|
|
|
import router from '@/router'
|
|
|
|
|
import { authenticateWithFingerprint, getToken, isIphone, removeToken } from '@/utils'
|
|
|
|
|
import { useUserStore } from '@/store/userStore'
|
|
|
|
|
import { useFundUserStore } from '@/store/fundStore'
|
2026-06-17 11:26:26 +03:30
|
|
|
import type { NavigationGuardNext } from 'vue-router'
|
2026-06-16 19:47:28 +03:30
|
|
|
import { useAuthStore } from '@/store/useAuthStore'
|
|
|
|
|
import { getConfigApp } from '@/utils/config'
|
2026-07-01 12:49:43 +03:30
|
|
|
import { recoverMobileViewportAfterAuth } from '@/layouts/composables/useMobileScrollReset'
|
2026-06-16 19:47:28 +03:30
|
|
|
|
|
|
|
|
const PUBLIC_ROUTE_NAMES = ['login', '404', 'educate', 'aboutUs'] as const
|
|
|
|
|
|
2026-06-17 11:26:26 +03:30
|
|
|
function isPublicRoute(name: string | symbol | null | undefined) {
|
|
|
|
|
return PUBLIC_ROUTE_NAMES.includes(name as (typeof PUBLIC_ROUTE_NAMES)[number])
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function goToLogin(next: NavigationGuardNext) {
|
|
|
|
|
return next({ name: 'login' })
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function goToHome(next: NavigationGuardNext) {
|
|
|
|
|
return next({ name: 'home' })
|
|
|
|
|
}
|
|
|
|
|
|
2026-06-16 19:47:28 +03:30
|
|
|
export function setupRouterGuards() {
|
|
|
|
|
const userStore = useUserStore()
|
|
|
|
|
const fundStore = useFundUserStore()
|
|
|
|
|
const authStore = useAuthStore()
|
|
|
|
|
|
2026-06-17 11:26:26 +03:30
|
|
|
const isApple = isIphone()
|
|
|
|
|
let fingerprintChecked = false
|
2026-06-16 19:47:28 +03:30
|
|
|
let timeoutId: ReturnType<typeof setTimeout> | null = null
|
|
|
|
|
let hasFingerPrint = false
|
|
|
|
|
|
|
|
|
|
const currentRoute = computed(() => router.currentRoute.value.fullPath)
|
|
|
|
|
|
|
|
|
|
async function start() {
|
|
|
|
|
if (getToken()) {
|
|
|
|
|
await fundStore.getMyFund()
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2026-06-17 11:26:26 +03:30
|
|
|
async function resolveSession() {
|
|
|
|
|
const token = getToken()
|
2026-06-16 19:47:28 +03:30
|
|
|
|
2026-06-17 11:26:26 +03:30
|
|
|
if (!token) {
|
|
|
|
|
userStore.userInfo = undefined
|
|
|
|
|
return false
|
2026-06-16 19:47:28 +03:30
|
|
|
}
|
|
|
|
|
|
2026-06-17 11:26:26 +03:30
|
|
|
if (!userStore.userInfo) {
|
|
|
|
|
await userStore.getMyInfo()
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (!userStore.userInfo) {
|
|
|
|
|
removeToken()
|
|
|
|
|
return false
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return true
|
2026-06-16 19:47:28 +03:30
|
|
|
}
|
|
|
|
|
|
|
|
|
|
router.beforeEach(async (to, from, next) => {
|
|
|
|
|
authStore.setLoading(true)
|
|
|
|
|
|
|
|
|
|
try {
|
|
|
|
|
if (timeoutId) {
|
|
|
|
|
clearTimeout(timeoutId)
|
|
|
|
|
}
|
|
|
|
|
|
2026-06-17 11:26:26 +03:30
|
|
|
if (to.name === 'giftCard' && getConfigApp('name') !== 'garoosi') {
|
|
|
|
|
return next({ name: (from.name as string) || 'home' })
|
2026-06-16 19:47:28 +03:30
|
|
|
}
|
|
|
|
|
|
2026-06-17 11:26:26 +03:30
|
|
|
const authenticated = await resolveSession()
|
|
|
|
|
|
|
|
|
|
if (!authenticated) {
|
|
|
|
|
if (isPublicRoute(to.name)) {
|
|
|
|
|
return next()
|
|
|
|
|
}
|
|
|
|
|
return goToLogin(next)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
hasFingerPrint = !!userStore.userInfo?.finger_print
|
|
|
|
|
|
|
|
|
|
if (isApple && userStore.userInfo?.finger_print) {
|
|
|
|
|
removeToken()
|
|
|
|
|
return goToLogin(next)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (!isApple && userStore.userInfo?.finger_print) {
|
|
|
|
|
if (!fingerprintChecked) {
|
|
|
|
|
fingerprintChecked = true
|
|
|
|
|
setFingerPosition(await authenticateWithFingerprint())
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (!getFingerPosition()) {
|
|
|
|
|
return goToLogin(next)
|
|
|
|
|
}
|
|
|
|
|
|
2026-06-16 19:47:28 +03:30
|
|
|
timeoutId = setTimeout(() => {
|
|
|
|
|
if (hasFingerPrint) {
|
|
|
|
|
setFingerPosition(false)
|
|
|
|
|
}
|
|
|
|
|
}, 180000)
|
|
|
|
|
}
|
|
|
|
|
|
2026-06-17 11:26:26 +03:30
|
|
|
if (to.name === 'login') {
|
|
|
|
|
return goToHome(next)
|
2026-06-16 19:47:28 +03:30
|
|
|
}
|
|
|
|
|
|
2026-06-17 11:26:26 +03:30
|
|
|
return next()
|
2026-06-16 19:47:28 +03:30
|
|
|
} finally {
|
|
|
|
|
authStore.setLoading(false)
|
|
|
|
|
}
|
|
|
|
|
})
|
|
|
|
|
|
2026-07-01 12:49:43 +03:30
|
|
|
router.afterEach((to, from) => {
|
|
|
|
|
if (document.documentElement.classList.contains('app-layout--desktop')) return
|
|
|
|
|
if (from.name === 'login') {
|
|
|
|
|
recoverMobileViewportAfterAuth()
|
|
|
|
|
}
|
|
|
|
|
})
|
|
|
|
|
|
2026-06-16 19:47:28 +03:30
|
|
|
watch(currentRoute, async () => (userStore.userInfo ? '' : await start()))
|
|
|
|
|
}
|
2026-06-17 11:26:26 +03:30
|
|
|
|
|
|
|
|
export async function ensureAuthenticatedRoute() {
|
|
|
|
|
const userStore = useUserStore()
|
|
|
|
|
const token = getToken()
|
|
|
|
|
|
|
|
|
|
if (!token) {
|
|
|
|
|
userStore.userInfo = undefined
|
|
|
|
|
if (router.currentRoute.value.name !== 'login') {
|
|
|
|
|
await router.replace({ name: 'login' })
|
|
|
|
|
}
|
|
|
|
|
return false
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (!userStore.userInfo) {
|
|
|
|
|
await userStore.getMyInfo()
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (!userStore.userInfo) {
|
|
|
|
|
removeToken()
|
|
|
|
|
if (router.currentRoute.value.name !== 'login') {
|
|
|
|
|
await router.replace({ name: 'login' })
|
|
|
|
|
}
|
|
|
|
|
return false
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return true
|
|
|
|
|
}
|