Files
boursecu_user/src/app/setupRouter.ts
T

158 lines
4.0 KiB
TypeScript
Raw Normal View History

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 { useWalletStore } from '@/store/walletStore'
import { useFundUserStore } from '@/store/fundStore'
import type { NavigationGuardNext, RouteLocationNormalized } from 'vue-router'
import { useAuthStore } from '@/store/useAuthStore'
import { getConfigApp } from '@/utils/config'
import { getFingerPosition, setFingerPosition } from '@/app/authBoot'
const PUBLIC_ROUTE_NAMES = ['login', '404', 'educate', 'aboutUs'] as const
export function setupRouterGuards() {
const userStore = useUserStore()
const walletStore = useWalletStore()
const fundStore = useFundUserStore()
const authStore = useAuthStore()
const isApple: boolean = isIphone()
let isFirst = true
let timeoutId: ReturnType<typeof setTimeout> | null = null
let hasFingerPrint = false
const currentRoute = computed(() => router.currentRoute.value.fullPath)
async function start() {
if (getToken()) {
await walletStore.getLimitedPrice()
await walletStore.getLimitedBuyGold()
await fundStore.getMyFund()
}
}
function goToLogin(to: RouteLocationNormalized, next: NavigationGuardNext) {
if (to.name !== 'login') {
return next({ name: 'login' })
}
return next()
}
function goToHome(to: RouteLocationNormalized, next: NavigationGuardNext) {
if (to.name !== 'home') {
return next({ name: 'home' })
}
return next()
}
function isPublicRoute(name: string | symbol | null | undefined) {
return PUBLIC_ROUTE_NAMES.includes(name as (typeof PUBLIC_ROUTE_NAMES)[number])
}
router.beforeEach(async (to, from, next) => {
authStore.setLoading(true)
try {
if (getToken() && !userStore.userInfo) {
await userStore.getMyInfo()
}
if (timeoutId) {
clearTimeout(timeoutId)
}
if (to.name == 'giftCard' && getConfigApp('name') != 'garoosi') {
return next({ name: from.name })
}
if (from.name !== 'login' || from.name !== undefined) {
timeoutId = setTimeout(() => {
if (hasFingerPrint) {
setFingerPosition(false)
console.log('Fingerprint authentication reset after 5 seconds of inactivity')
}
}, 180000)
}
if (isFirst) {
isFirst = false
if (getToken()) {
await userStore.getMyInfo()
if (!userStore.userInfo) {
return goToLogin(to, next)
}
const finger_print = userStore.userInfo.finger_print
hasFingerPrint = !!finger_print
if (isApple && finger_print) {
removeToken()
return goToLogin(to, next)
}
if (!isApple && finger_print) {
setFingerPosition(await authenticateWithFingerprint())
if (!getFingerPosition()) {
return goToLogin(to, next)
}
if (to.name === 'login') {
return goToHome(to, next)
}
return next()
}
if (to.name == 'login') {
return goToHome(to, next)
}
return next()
}
if (isPublicRoute(to.name)) {
return next()
}
return goToLogin(to, next)
}
if (getFingerPosition()) {
if (getToken()) {
if (to.name === 'login') {
return next({ name: from.name || 'home' })
}
return next()
}
if (isPublicRoute(to.name)) {
return next()
}
return next({ name: 'login' })
}
if (getToken() && !hasFingerPrint) {
return next()
}
if (isPublicRoute(to.name)) {
if (to.name === 'login') {
return goToLogin(to, next)
}
return next()
}
return goToLogin(to, next)
} finally {
authStore.setLoading(false)
}
})
watch(currentRoute, async () => (userStore.userInfo ? '' : await start()))
}