Files
boursecu_user/src/main.ts
T

213 lines
6.5 KiB
TypeScript

import 'ant-design-vue/dist/antd.less'
import './assets/css/main.less'
import 'virtual:uno.css'
import {computed, createApp, nextTick, 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'
import {applyProjectFont, getConfigApp} from "@/utils/config";
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() {
if (getToken()) {
// await userStore.getMyInfo()
await walletStore.getLimitedPrice()
await walletStore.getLimitedBuyGold()
await fundStore.getMyFund()
}
}
const win = window as Window & { isWhatsappBtnAdded?: boolean }
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)
}
if (to.name=='giftCard' && getConfigApp('name')!='garoosi')
return next({ name: from.name })
// Start a new timeout to reset fingerprint after 5 seconds of inactivity
if (from.name !== 'login' || from.name !== undefined) {
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 {
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','educate', 'aboutUs'].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','educate', 'aboutUs'].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 {
if (['login', '404','educate', 'aboutUs'].includes(to.name as string)) {
if (to.name === 'login'){
goToLogin(to, next)
return
} else {
return next()
}
// Allow access to login or 404
}
}
/* if (to.name !== 'login') {
return next({ name: 'login' }) // Redirect to login
} else {
return next() // If already on the login page, don't redirect
}*/
/* return next({ name: 'login' })*/
}
}
})
app.mount('#app')
// if (currentRoute.value !== '/') {
// await start()
// }
nextTick(() => {
applyProjectFont()
const color = window.__APP_CONFIG__?.user?.color
const secondary_color = window.__APP_CONFIG__?.user?.secondary_color
if (!color) {
console.warn('Primary color not found')
return
}
console.log('APP_CONFIG:', window.__APP_CONFIG__.user.color)
document.documentElement.style.setProperty('--App-primary-color', color)
document.documentElement.style.setProperty('--App-secondary-color', secondary_color)
})
watch(currentRoute, async () => userStore.userInfo ? '' : await start())