UNPKG

capacitor-biometric-authentication

Version:

Framework-agnostic biometric authentication library. Works with React, Vue, Angular, or vanilla JS. No providers required!

117 lines 4.25 kB
import { BiometricErrorCode, BiometryType } from '../core/types'; export class ElectronAdapter { constructor() { this.platform = 'electron'; // Electron-specific initialization } async isAvailable() { try { // Check if we're in Electron main or renderer process if (typeof process !== 'undefined' && process.versions && process.versions.electron) { // In Electron, we can use TouchID on macOS if (process.platform === 'darwin') { // eslint-disable-next-line @typescript-eslint/no-require-imports const electronModule = require('electron'); const { systemPreferences } = electronModule.remote || electronModule; return systemPreferences.canPromptTouchID(); } // Windows Hello support could be added here return false; } return false; } catch (_a) { return false; } } async getSupportedBiometrics() { if (!(await this.isAvailable())) { return []; } // On macOS, we support Touch ID if (process.platform === 'darwin') { return [BiometryType.TOUCH_ID]; } return []; } async authenticate(options) { try { if (!(await this.isAvailable())) { return { success: false, error: { code: BiometricErrorCode.BIOMETRIC_UNAVAILABLE, message: 'Biometric authentication is not available' } }; } if (process.platform === 'darwin') { // eslint-disable-next-line @typescript-eslint/no-require-imports const electronModule = require('electron'); const { systemPreferences } = electronModule.remote || electronModule; try { await systemPreferences.promptTouchID((options === null || options === void 0 ? void 0 : options.reason) || 'authenticate with Touch ID'); return { success: true, biometryType: BiometryType.TOUCH_ID, sessionId: this.generateSessionId(), platform: 'electron' }; } catch (_a) { return { success: false, error: { code: BiometricErrorCode.AUTHENTICATION_FAILED, message: 'Touch ID authentication failed' } }; } } return { success: false, error: { code: BiometricErrorCode.PLATFORM_NOT_SUPPORTED, message: 'Platform not supported' } }; } catch (error) { return { success: false, error: this.mapError(error) }; } } async deleteCredentials() { // Electron doesn't store biometric credentials // This is a no-op } async hasCredentials() { // In Electron, we don't store credentials // Return true if biometrics are available return await this.isAvailable(); } mapError(error) { let code = BiometricErrorCode.UNKNOWN_ERROR; let message = 'An unknown error occurred'; if (error instanceof Error) { message = error.message; if (message.includes('cancelled') || message.includes('canceled')) { code = BiometricErrorCode.USER_CANCELLED; } else if (message.includes('failed')) { code = BiometricErrorCode.AUTHENTICATION_FAILED; } } return { code, message, details: error }; } generateSessionId() { return Date.now().toString(36) + Math.random().toString(36).substr(2); } } //# sourceMappingURL=ElectronAdapter.js.map