Files
boursecu_user/src/main.ts
T

188 lines
5.7 KiB
TypeScript
Raw Normal View History

2026-01-18 13:01:46 +03:30
import 'ant-design-vue/dist/antd.less'
import './assets/css/main.less'
import 'virtual:uno.css'
import { computed, createApp, ref, watch } from 'vue'
import { createPinia } from 'pinia'
import '@unocss/reset/tailwind-compat.css'
import VueApexCharts from 'vue3-apexcharts'
import App from './App.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 VueLazyLoad from 'vue3-lazyload'
import { getMe } from '@/api/me'
import type { NavigationGuardNext, RouteLocationNormalized } from 'vue-router'
import { useAuthStore } from '@/store/useAuthStore'
const app = createApp(App)
app.use(createPinia())
app.use(VueLazyLoad, {
// options...
})
app.use(router)
app.use(VueApexCharts)
const userStore = useUserStore()
const walletStore = useWalletStore()
const fundStore = useFundUserStore()
const authStore = useAuthStore()
const isApple: boolean = isIphone()
let isFirst: boolean = true
let fingerPosition: boolean = false
let timeoutId: ReturnType<typeof setTimeout> | null = null
let hasFingerPrint: boolean = false
const userInfo = ref()
// userInfo.value = await getMe()
// const token = getToken()
const currentRoute = computed(() => {
return router.currentRoute.value.fullPath
})
async function start() {
// console.log('ffffffffffffffffffffffffffffffffffffffffff')
if (getToken()) {
// await userStore.getMyInfo()
await walletStore.getLimitedPrice()
await walletStore.getLimitedBuyGold()
await fundStore.getMyFund()
}
}
export function setTruth() {
fingerPosition = true
}
export function setEnableAuthenticate() {
fingerPosition = false
}
function goToLogin(to: RouteLocationNormalized, next: NavigationGuardNext) {
if (to.name !== 'login') {
return next({ name: 'login' }) // Redirect to login
} else {
return next() // If already on the login page, don't redirect
}
}
function goToHome(to: RouteLocationNormalized, next: NavigationGuardNext) {
if (to.name !== 'home') {
return next({ name: 'home' }) // Redirect to login
} else {
return next() // If already on the login page, don't redirect
}
}
router.beforeEach(async (to, from, next) => {
authStore.setLoading(true)
console.log(authStore)
if (timeoutId) {
clearTimeout(timeoutId)
}
// Start a new timeout to reset fingerprint after 5 seconds of inactivity
if (from.name !== 'login' || from.name !== undefined) {
console.log(from.name)
timeoutId = setTimeout(async () => {
if (hasFingerPrint) {
fingerPosition = false
/* fingerPosition = await authenticateWithFingerprint()
if (!fingerPosition) {
goToLogin(to, next)
}*/
// isFirst = true;
console.log('Fingerprint authentication reset after 5 seconds of inactivity')
}
}, 180000) // 5 seconds
}
// If it's the first time the navigation guard runs
if (isFirst) {
isFirst = false // Set isFirst to false after the first check
// If the user has a token
if (getToken()) {
await userStore.getMyInfo()
const finger_print =userStore.userInfo.finger_print
console.log(finger_print)
// const { finger_print } = await getMe()
hasFingerPrint = finger_print
// If on an Apple device and fingerprint exists
if (isApple && finger_print) {
removeToken() // Remove token if fingerprint is found
goToLogin(to, next) // Redirect to login
}
// If not an Apple device and fingerprint exists
else if (!isApple && finger_print) {
// goToLogin(to, next)
fingerPosition = await authenticateWithFingerprint()
if (fingerPosition) {
// goToHome(to, next) // Redirect to home if fingerprint is authenticated
} else {
goToLogin(to, next) // Redirect to login if authentication fails
}
} else {
console.log('salam')
if (from.path === '/') {
// goToHome(to, next)
}
if(to.name=="login"){
goToHome(to, next)
}
return next()
}
} else {
// If no token, handle redirect to login or allow access to login or 404
if (['login', '404'].includes(to.name as string)) {
return next() // Allow access to login or 404
}
goToLogin(to, next) // Redirect to login if no token
}
}
// For subsequent navigations after the first check
else {
// If the user has passed fingerprint authentication
if (fingerPosition) {
if (getToken()) {
if (to.name === 'login') {
return next({ name: from.name || 'home' }) // Redirect to home if trying to access login after authentication
}
return next() // Proceed to the intended route
} else {
// If no token, ensure proper redirection to login or 404
if (['login', '404'].includes(to.name as string)) {
return next() // Allow access to login or 404
}
return next({ name: 'login' })
}
}
// If fingerprint authentication failed or was not done
else {
if (getToken() && !hasFingerPrint) {
next()
} else {
goToLogin(to, next)
}
/* if (to.name !== 'login') {
return next({ name: 'login' }) // Redirect to login
} else {
return next() // If already on the login page, don't redirect
}*/
/* console.log('sssssssssssssssssssssssss')
return next({ name: 'login' })*/
}
}
})
app.mount('#app')
// if (currentRoute.value !== '/') {
// await start()
// }
watch(currentRoute, async () => userStore.userInfo ? '' : await start())