123 lines
3.1 KiB
TypeScript
123 lines
3.1 KiB
TypeScript
import { tokenName } from '@/themeConfig'
|
|||
|
|
|
||
|
|
import { ref } from 'vue'
|
||
|
|
|
||
|
|
export const loading = ref<boolean>(false)
|
||
|
|
export const loadingMore = ref<boolean>(false)
|
||
|
|
const userInfo = ref()
|
||
|
|
|
||
|
|
function setToken(token: string | undefined) {
|
||
|
|
return localStorage.setItem(tokenName, token)
|
||
|
|
}
|
||
|
|
|
||
|
|
function setFingerPrint(val: string | undefined) {
|
||
|
|
return localStorage.setItem('finger', val)
|
||
|
|
}
|
||
|
|
|
||
|
|
function getToken() {
|
||
|
|
return localStorage.getItem(tokenName)
|
||
|
|
}
|
||
|
|
|
||
|
|
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: 'GOLD.IR' },
|
||
|
|
user: {
|
||
|
|
id: Uint8Array.from('user-id', c => c.charCodeAt(0)),
|
||
|
|
name: 'GOLD.IR',
|
||
|
|
displayName: 'GOLD.IR'
|
||
|
|
},
|
||
|
|
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 = localStorage.getItem('GOLD.IR_app_token')
|
||
|
|
|
||
|
|
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() {
|
||
|
|
return localStorage.removeItem(tokenName)
|
||
|
|
}
|
||
|
|
|
||
|
|
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)
|
||
|
|
}
|
||
|
|
|
||
|
|
export {
|
||
|
|
getToken,
|
||
|
|
setToken,
|
||
|
|
removeToken,
|
||
|
|
URLUserType,
|
||
|
|
toFixedDownThreeChar,
|
||
|
|
isIphone,
|
||
|
|
removeFingerPrint,
|
||
|
|
getIsActiveFingerprint,
|
||
|
|
setFingerPrint
|
||
|
|
}
|