82 lines
2.6 KiB
TypeScript
82 lines
2.6 KiB
TypeScript
export function getDeviceFingerprint(): string {
|
|
if (typeof window === 'undefined') return 'server';
|
|
const key = 'lendry_device_fingerprint';
|
|
const existing = window.localStorage.getItem(key);
|
|
if (existing) return existing;
|
|
const value = crypto.randomUUID();
|
|
window.localStorage.setItem(key, value);
|
|
return value;
|
|
}
|
|
|
|
export function resolveDeviceType(): string {
|
|
if (typeof navigator === 'undefined') return 'WEB';
|
|
const ua = navigator.userAgent;
|
|
if (/Android/i.test(ua)) return 'ANDROID';
|
|
if (/iPhone|iPad|iPod/i.test(ua)) return 'IOS';
|
|
if (/Windows/i.test(ua)) return 'DESKTOP';
|
|
if (/Mac OS X/i.test(ua)) return 'DESKTOP';
|
|
return 'WEB';
|
|
}
|
|
|
|
export function resolveDeviceLabel(): string {
|
|
if (typeof navigator === 'undefined') return 'Browser';
|
|
const ua = navigator.userAgent;
|
|
|
|
const browser = (() => {
|
|
if (/Edg\//i.test(ua)) return 'Microsoft Edge';
|
|
if (/YaBrowser/i.test(ua)) return 'Яндекс Браузер';
|
|
if (/Firefox\//i.test(ua)) return 'Firefox';
|
|
if (/CriOS/i.test(ua)) return 'Chrome';
|
|
if (/Chrome\//i.test(ua) && !/Edg\//i.test(ua)) return 'Chrome';
|
|
if (/Safari/i.test(ua) && !/Chrome/i.test(ua)) return 'Safari';
|
|
return 'Браузер';
|
|
})();
|
|
|
|
const os = (() => {
|
|
if (/Windows NT/i.test(ua)) return 'Windows';
|
|
if (/Android/i.test(ua)) return 'Android';
|
|
if (/iPhone/i.test(ua)) return 'iPhone';
|
|
if (/iPad/i.test(ua)) return 'iPad';
|
|
if (/Mac OS X/i.test(ua)) return 'macOS';
|
|
if (/Linux/i.test(ua)) return 'Linux';
|
|
return 'Устройство';
|
|
})();
|
|
|
|
return `${browser} · ${os}`;
|
|
}
|
|
|
|
export function buildAuthDevicePayload() {
|
|
return {
|
|
fingerprint: getDeviceFingerprint(),
|
|
deviceName: resolveDeviceLabel(),
|
|
deviceType: resolveDeviceType()
|
|
};
|
|
}
|
|
|
|
export function formatUserAgentLabel(userAgent?: string | null): string | null {
|
|
if (!userAgent?.trim()) return null;
|
|
const ua = userAgent.trim();
|
|
|
|
const browser = (() => {
|
|
if (/Edg\//i.test(ua)) return 'Microsoft Edge';
|
|
if (/YaBrowser/i.test(ua)) return 'Яндекс Браузер';
|
|
if (/Firefox\//i.test(ua)) return 'Firefox';
|
|
if (/Chrome\//i.test(ua) && !/Edg\//i.test(ua)) return 'Chrome';
|
|
if (/Safari/i.test(ua) && !/Chrome/i.test(ua)) return 'Safari';
|
|
return null;
|
|
})();
|
|
|
|
const os = (() => {
|
|
if (/Windows NT/i.test(ua)) return 'Windows';
|
|
if (/Android/i.test(ua)) return 'Android';
|
|
if (/iPhone/i.test(ua)) return 'iPhone';
|
|
if (/iPad/i.test(ua)) return 'iPad';
|
|
if (/Mac OS X/i.test(ua)) return 'macOS';
|
|
if (/Linux/i.test(ua)) return 'Linux';
|
|
return null;
|
|
})();
|
|
|
|
if (browser && os) return `${browser} · ${os}`;
|
|
return browser ?? os;
|
|
}
|