2026-06-29 15:27:35 +03:30
|
|
|
import { computed, watch } from 'vue'
|
|
|
|
|
import router from '@/router'
|
|
|
|
|
import { authenticateWithFingerprint, getToken, isIphone, removeToken } from '@/utils'
|
|
|
|
|
import { useUserStore } from '@/store/userStore'
|
|
|
|
|
import type { NavigationGuardNext, RouteLocationNormalized } from 'vue-router'
|
|
|
|
|
import { useAuthStore } from '@/store/useAuthStore'
|
|
|
|
|
import {
|
|
|
|
|
clearFingerTimeout,
|
|
|
|
|
getFingerPosition,
|
|
|
|
|
getHasFingerPrint,
|
|
|
|
|
setFingerPosition,
|
|
|
|
|
setFingerTimeOut,
|
|
|
|
|
setHasFingerPrint
|
|
|
|
|
} from '@/app/authBoot'
|
2026-07-01 18:16:12 +03:30
|
|
|
import { recoverMobileViewportAfterAuth } from '@/layouts/composables/useMobileScrollReset'
|
2026-06-29 15:27:35 +03:30
|
|
|
|
|
|
|
|
export function setupRouterGuards() {
|
|
|
|
|
const userStore = useUserStore()
|
|
|
|
|
const authStore = useAuthStore()
|
|
|
|
|
|
|
|
|
|
const isApple: boolean = isIphone()
|
|
|
|
|
let isFirst = true
|
|
|
|
|
|
|
|
|
|
const currentRoute = computed(() => router.currentRoute.value.fullPath)
|
|
|
|
|
|
|
|
|
|
async function start() {
|
2026-06-29 22:43:28 +03:30
|
|
|
if (!getToken()) {
|
|
|
|
|
return
|
2026-06-29 15:27:35 +03:30
|
|
|
}
|
2026-06-29 22:43:28 +03:30
|
|
|
|
|
|
|
|
if (!userStore.userInfo) {
|
2026-07-01 22:29:18 +03:30
|
|
|
await userStore.getMyInfo()
|
2026-06-29 22:43:28 +03:30
|
|
|
}
|
2026-06-29 15:27:35 +03:30
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function goToLogin(to: RouteLocationNormalized, next: NavigationGuardNext) {
|
|
|
|
|
if (to.name === 'buyProduct' || to.name === 'productDetail' || to.name === 'about' || to.name === 'cart') {
|
|
|
|
|
return next()
|
|
|
|
|
}
|
|
|
|
|
if (to.name !== 'login') {
|
|
|
|
|
return next({ name: 'login' })
|
|
|
|
|
}
|
|
|
|
|
return next()
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function goToHome(to: RouteLocationNormalized, next: NavigationGuardNext) {
|
|
|
|
|
if (to.name !== 'buyProduct') {
|
|
|
|
|
return next({ name: 'buyProduct' })
|
|
|
|
|
}
|
|
|
|
|
return next()
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
router.beforeEach(async (to, from, next) => {
|
|
|
|
|
authStore.setLoading(true)
|
|
|
|
|
clearFingerTimeout()
|
|
|
|
|
setFingerTimeOut(to)
|
|
|
|
|
|
2026-07-25 12:13:30 +03:30
|
|
|
if (to.name === 'notifyTrigger' && getToken()) {
|
|
|
|
|
const { useSettingsStore } = await import('@/store/settingsStore')
|
|
|
|
|
const settingsStore = useSettingsStore()
|
|
|
|
|
if (!settingsStore.loaded) {
|
|
|
|
|
await settingsStore.fetchSettings()
|
|
|
|
|
}
|
|
|
|
|
if (!settingsStore.isSettingTruthy('has_page_price_trigger')) {
|
|
|
|
|
return next({ name: (from.name as string) || 'user' })
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (to.name === 'appNotifications' && getToken()) {
|
|
|
|
|
const { useSettingsStore } = await import('@/store/settingsStore')
|
|
|
|
|
const settingsStore = useSettingsStore()
|
|
|
|
|
if (!settingsStore.loaded) {
|
|
|
|
|
await settingsStore.fetchSettings()
|
|
|
|
|
}
|
|
|
|
|
if (!settingsStore.isSettingTruthy('has_page_notification')) {
|
|
|
|
|
return next({ name: (from.name as string) || 'home' })
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2026-06-29 15:27:35 +03:30
|
|
|
if (isFirst) {
|
|
|
|
|
isFirst = false
|
|
|
|
|
|
|
|
|
|
if (getToken()) {
|
|
|
|
|
await userStore.getMyInfo()
|
|
|
|
|
const finger_print = userStore.userInfo.finger_print
|
|
|
|
|
setHasFingerPrint(finger_print)
|
|
|
|
|
|
|
|
|
|
if (isApple && finger_print) {
|
|
|
|
|
removeToken()
|
|
|
|
|
goToLogin(to, next)
|
|
|
|
|
} else if (!isApple && finger_print) {
|
|
|
|
|
setFingerPosition(await authenticateWithFingerprint())
|
|
|
|
|
|
|
|
|
|
if (!getFingerPosition()) {
|
|
|
|
|
goToLogin(to, next)
|
|
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
if (to.name == 'login') {
|
|
|
|
|
goToHome(to, next)
|
|
|
|
|
}
|
|
|
|
|
return next()
|
|
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
if (['login', '404', 'buyProduct', 'productDetail', 'about', 'cart'].includes(to.name as string)) {
|
|
|
|
|
return next()
|
|
|
|
|
}
|
|
|
|
|
goToLogin(to, next)
|
|
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
if (getFingerPosition()) {
|
|
|
|
|
if (getToken()) {
|
|
|
|
|
if (to.name === 'login') {
|
|
|
|
|
return next({ name: from.name || 'buyProduct' })
|
|
|
|
|
}
|
|
|
|
|
return next()
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (['login', '404', 'buyProduct', 'productDetail', 'about', 'cart'].includes(to.name as string)) {
|
|
|
|
|
return next()
|
|
|
|
|
}
|
|
|
|
|
return next({ name: 'login' })
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (getToken() && !getHasFingerPrint()) {
|
|
|
|
|
next()
|
|
|
|
|
} else {
|
|
|
|
|
goToLogin(to, next)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
})
|
|
|
|
|
|
2026-07-01 18:16:12 +03:30
|
|
|
router.afterEach((to, from) => {
|
|
|
|
|
if (document.documentElement.classList.contains('app-layout--desktop')) return
|
|
|
|
|
if (from.name === 'login') {
|
|
|
|
|
recoverMobileViewportAfterAuth()
|
|
|
|
|
}
|
|
|
|
|
})
|
|
|
|
|
|
2026-06-29 15:27:35 +03:30
|
|
|
watch(currentRoute, async () => (userStore.userInfo ? '' : await start()))
|
|
|
|
|
}
|