icon changed

This commit is contained in:
2026-06-17 11:26:26 +03:30
parent 32dde73dff
commit b42b123957
4 changed files with 108 additions and 149 deletions
+88 -95
View File
@@ -4,21 +4,33 @@ import { authenticateWithFingerprint, getToken, isIphone, removeToken } from '@/
import { useUserStore } from '@/store/userStore'
import { useWalletStore } from '@/store/walletStore'
import { useFundUserStore } from '@/store/fundStore'
import type { NavigationGuardNext, RouteLocationNormalized } from 'vue-router'
import type { NavigationGuardNext } 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
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 walletStore = useWalletStore()
const fundStore = useFundUserStore()
const authStore = useAuthStore()
const isApple: boolean = isIphone()
let isFirst = true
const isApple = isIphone()
let fingerprintChecked = false
let timeoutId: ReturnType<typeof setTimeout> | null = null
let hasFingerPrint = false
@@ -32,122 +44,76 @@ export function setupRouterGuards() {
}
}
function goToLogin(to: RouteLocationNormalized, next: NavigationGuardNext) {
if (to.name !== 'login') {
return next({ name: 'login' })
}
return next()
}
async function resolveSession() {
const token = getToken()
function goToHome(to: RouteLocationNormalized, next: NavigationGuardNext) {
if (to.name !== 'home') {
return next({ name: 'home' })
if (!token) {
userStore.userInfo = undefined
return false
}
return next()
}
function isPublicRoute(name: string | symbol | null | undefined) {
return PUBLIC_ROUTE_NAMES.includes(name as (typeof PUBLIC_ROUTE_NAMES)[number])
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 (getToken() && !userStore.userInfo) {
await userStore.getMyInfo()
}
if (timeoutId) {
clearTimeout(timeoutId)
}
if (to.name == 'giftCard' && getConfigApp('name') != 'garoosi') {
return next({ name: from.name })
if (to.name === 'giftCard' && getConfigApp('name') !== 'garoosi') {
return next({ name: (from.name as string) || 'home' })
}
if (from.name !== 'login' || from.name !== undefined) {
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)
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 (to.name === 'login') {
return goToHome(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)
return next()
} finally {
authStore.setLoading(false)
}
@@ -155,3 +121,30 @@ export function setupRouterGuards() {
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
}