Files
boursecu_user/src/app/setupRouter.ts
T
2026-07-01 22:28:25 +03:30

154 lines
3.7 KiB
TypeScript

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'
import type { NavigationGuardNext } from 'vue-router'
import { useAuthStore } from '@/store/useAuthStore'
import { getConfigApp } from '@/utils/config'
import { recoverMobileViewportAfterAuth } from '@/layouts/composables/useMobileScrollReset'
const PUBLIC_ROUTE_NAMES = ['login', '404', 'educate', 'aboutUs'] as const
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' })
}
export function setupRouterGuards() {
const userStore = useUserStore()
const fundStore = useFundUserStore()
const authStore = useAuthStore()
const isApple = isIphone()
let fingerprintChecked = false
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()
}
}
async function resolveSession() {
const token = getToken()
if (!token) {
userStore.userInfo = undefined
return false
}
if (!userStore.userInfo) {
await userStore.getMyInfo()
}
if (!userStore.userInfo) {
removeToken()
return false
}
return true
}
router.beforeEach(async (to, from, next) => {
authStore.setLoading(true)
try {
if (timeoutId) {
clearTimeout(timeoutId)
}
if (to.name === 'giftCard' && getConfigApp('name') !== 'garoosi') {
return next({ name: (from.name as string) || 'home' })
}
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)
}
timeoutId = setTimeout(() => {
if (hasFingerPrint) {
setFingerPosition(false)
}
}, 180000)
}
if (to.name === 'login') {
return goToHome(next)
}
return next()
} finally {
authStore.setLoading(false)
}
})
router.afterEach((to, from) => {
if (document.documentElement.classList.contains('app-layout--desktop')) return
if (from.name === 'login') {
recoverMobileViewportAfterAuth()
}
})
watch(currentRoute, async () => (userStore.userInfo ? '' : await start()))
}
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
}