From eee26d06b769be1de9883eadd72d128f6cabd68a Mon Sep 17 00:00:00 2001 From: IluaAir Date: Mon, 6 Oct 2025 23:09:38 +0300 Subject: [PATCH] add fingerprint --- taskncoffee-app/src/apiv1/fingerprint.js | 42 ++++++++++++++++++++++++ 1 file changed, 42 insertions(+) create mode 100644 taskncoffee-app/src/apiv1/fingerprint.js diff --git a/taskncoffee-app/src/apiv1/fingerprint.js b/taskncoffee-app/src/apiv1/fingerprint.js new file mode 100644 index 0000000..050fa73 --- /dev/null +++ b/taskncoffee-app/src/apiv1/fingerprint.js @@ -0,0 +1,42 @@ +import FingerprintJS from '@fingerprintjs/fingerprintjs'; + +let fpPromise = null; + +/** + * @returns {Promise} Promise с экземпляром FingerprintJS + */ +const initFingerprint = () => { + if (!fpPromise) { + fpPromise = FingerprintJS.load(); + } + return fpPromise; +}; + +/** + * @returns {Promise} Fingerprint ID + */ +export const getFingerprint = async () => { + try { + const savedFingerprint = localStorage.getItem('fingerprint'); + if (savedFingerprint) { + return savedFingerprint; + } + + const fp = await initFingerprint(); + const result = await fp.get(); + const fingerprint = result.visitorId; + + localStorage.setItem('fingerprint', fingerprint); + return fingerprint; + } catch (error) { + console.error('Error getting fingerprint:', error); + const fallbackFingerprint = `fallback_${Date.now()}_${Math.random().toString(36).substr(2, 9)}`; + localStorage.setItem('fingerprint', fallbackFingerprint); + return fallbackFingerprint; + } +}; + +export const clearFingerprint = () => { + localStorage.removeItem('fingerprint'); +}; +