capacitor-biometric-authentication
Version:
Framework-agnostic biometric authentication library. Works with React, Vue, Angular, or vanilla JS. No providers required!
119 lines • 4.58 kB
JavaScript
export class PlatformDetector {
constructor() {
this.platformInfo = null;
}
static getInstance() {
if (!PlatformDetector.instance) {
PlatformDetector.instance = new PlatformDetector();
}
return PlatformDetector.instance;
}
detect() {
var _a, _b;
if (this.platformInfo) {
return this.platformInfo;
}
const info = {
name: 'unknown',
isCapacitor: false,
isReactNative: false,
isCordova: false,
isWeb: false,
isIOS: false,
isAndroid: false,
isElectron: false
};
// Check if we're in a browser environment
if (typeof window !== 'undefined' && typeof document !== 'undefined') {
info.isWeb = true;
// Check for Capacitor
if (window.Capacitor) {
info.isCapacitor = true;
const capacitor = window.Capacitor;
const platform = (_a = capacitor === null || capacitor === void 0 ? void 0 : capacitor.getPlatform) === null || _a === void 0 ? void 0 : _a.call(capacitor);
if (platform) {
info.name = platform;
info.isIOS = platform === 'ios';
info.isAndroid = platform === 'android';
info.isWeb = platform === 'web';
}
}
// Check for Cordova
else if (window.cordova || window.phonegap) {
info.isCordova = true;
info.name = 'cordova';
const userAgent = navigator.userAgent || navigator.vendor || window.opera || '';
if (/android/i.test(userAgent)) {
info.isAndroid = true;
info.name = 'android';
}
else if (/iPad|iPhone|iPod/.test(userAgent) && !window.MSStream) {
info.isIOS = true;
info.name = 'ios';
}
}
// Check for Electron
else if (((_b = window.process) === null || _b === void 0 ? void 0 : _b.type) === 'renderer' || navigator.userAgent.indexOf('Electron') !== -1) {
info.isElectron = true;
info.name = 'electron';
}
// Default to web
else {
info.name = 'web';
}
}
// Check for React Native
else if (typeof global !== 'undefined' && global.nativePerformanceNow) {
info.isReactNative = true;
info.name = 'react-native';
// Try to detect platform in React Native
try {
// eslint-disable-next-line @typescript-eslint/no-require-imports, @typescript-eslint/no-explicit-any
const { Platform } = require('react-native');
if (Platform) {
info.name = Platform.OS;
info.isIOS = Platform.OS === 'ios';
info.isAndroid = Platform.OS === 'android';
}
}
catch (_c) {
// React Native not available
}
}
// Node.js environment
else if (typeof process !== 'undefined' && process.versions && process.versions.node) {
info.name = 'node';
}
// Get version info if available
if (info.isCapacitor && typeof window !== 'undefined') {
const capacitor = window.Capacitor;
if (capacitor === null || capacitor === void 0 ? void 0 : capacitor.version) {
info.version = capacitor.version;
}
}
else if (info.isReactNative) {
try {
// eslint-disable-next-line @typescript-eslint/no-require-imports, @typescript-eslint/no-explicit-any
const { Platform } = require('react-native');
info.version = Platform.Version;
}
catch (_d) {
// Ignore
}
}
this.platformInfo = info;
return info;
}
isSupported() {
const info = this.detect();
return info.isWeb || info.isCapacitor || info.isReactNative || info.isCordova;
}
getPlatformName() {
return this.detect().name;
}
isNativePlatform() {
const info = this.detect();
return (info.isIOS || info.isAndroid) && (info.isCapacitor || info.isReactNative || info.isCordova);
}
}
//# sourceMappingURL=platform-detector.js.map