import { clearStoredAuthTokens, findStoredAuthToken, persistAuthToken, } from '@/utils/authToken' import { stopPriceRealtimeAuth } from '@/utils/priceSocket' import { ref } from 'vue' import {useConfigStore} from "@/store/configStore"; import {getConfigApp} from "@/utils/config"; export const loading = ref(false) export const loadingMore = ref(false) const userInfo = ref() const nameProject= getConfigApp('name') function setToken(token: string | undefined) { if (!token) { return } return persistAuthToken(token) } function setFingerPrint(val: string | undefined) { return localStorage.setItem('finger', val) } function getToken() { return findStoredAuthToken() } function getIsActiveFingerprint() { return localStorage.getItem('finger') === 'true' } export async function authenticateWithFingerprint() { const challenge = new Uint8Array(32) window.crypto.getRandomValues(challenge) const createOptions = { challenge, rp: { name: nameProject }, user: { id: Uint8Array.from('user-id', c => c.charCodeAt(0)), name: nameProject, displayName: nameProject }, pubKeyCredParams: [{ type: 'public-key', alg: -7 }], authenticatorSelection: { authenticatorAttachment: 'platform', userVerification: 'required' }, timeout: 60000, attestation: 'direct' } try { const credential = await navigator.credentials.create({ publicKey: createOptions }) console.log('Credential با موفقیت ایجاد شد!') const credentialId = btoa(String.fromCharCode(...new Uint8Array(credential.rawId))) // بازیابی توکن از localStorage const token = findStoredAuthToken() if (!token) { console.warn('توکن پیدا نشد، عملیات متوقف شد.') return false } // Success, proceed with login using token // Perform your login action here return true } catch (error) { const errorMessage = error.name === 'NotAllowedError' ? 'احراز هویت لغو شد یا دسترسی مجاز نبود.' : 'عملیات ناموفق بود. لطفاً دوباره امتحان کنید.' console.error(errorMessage, error) return false } } function removeToken() { stopPriceRealtimeAuth() return clearStoredAuthTokens() } function removeFingerPrint() { return localStorage.removeItem('finger') } /*const userType = computed(()=>{ console.log('salam',router.currentRoute.value.fullPath) if (router.currentRoute.value.fullPath.split('/')[1] === 'customer') return 'customer' else return 'user' })*/ function URLUserType() { /* console.log('salam', router.currentRoute.value.fullPath.split('/')) console.log('salam', router.currentRoute.value.fullPath)*/ /* console.log('salam', location.host.split('.')[0] === 'app') console.log('salam', location.host) console.log('salam', location.host.split('.'))*/ if (location.host.split('.')[0] === 'app') return 'single' else return 'customer' } function toFixedDownThreeChar(num: number) { return Math.floor(num * 1000) / 1000 } function isIphone() { const userAgent = navigator.userAgent.toLowerCase() // const isAndroid = /Android/.test(userAgent); return /iPhone/.test(userAgent) } function createUuid() { if (typeof crypto !== 'undefined' && typeof crypto.randomUUID === 'function') { return crypto.randomUUID() } if (typeof crypto !== 'undefined' && typeof crypto.getRandomValues === 'function') { const bytes = new Uint8Array(16) crypto.getRandomValues(bytes) bytes[6] = (bytes[6] & 0x0f) | 0x40 bytes[8] = (bytes[8] & 0x3f) | 0x80 const hex = Array.from(bytes, (b) => b.toString(16).padStart(2, '0')).join('') return [ hex.slice(0, 8), hex.slice(8, 12), hex.slice(12, 16), hex.slice(16, 20), hex.slice(20), ].join('-') } return `${Date.now()}-${Math.floor(Math.random() * 1e9)}` } export { getToken, setToken, removeToken, URLUserType, toFixedDownThreeChar, isIphone, removeFingerPrint, getIsActiveFingerprint, setFingerPrint, createUuid }