win-hello
Version:
Windows Hello authentication for Node.js
92 lines (82 loc) • 3.01 kB
JavaScript
// ESM wrapper for win-hello
import { createRequire } from 'module';
import os from 'os';
const require = createRequire(import.meta.url);
/**
* Factory function to create Windows Hello API
* @returns {Object} Windows Hello API object with isHelloAvailable and requestHello methods
*/
function createWinHello() {
// Check if running on Windows
const isWindows = os.platform() === 'win32';
let winHello = null;
// Only try to load the native module on Windows
if (isWindows) {
try {
winHello = require('./win_hello.node');
} catch (err) {
console.error('Failed to load Windows Hello native module:', err.message);
}
}
return {
/**
* Check if Windows Hello is available on the system
* @returns {Promise<boolean>} Promise that resolves to true if Windows Hello is available, rejects otherwise
*/
isHelloAvailable() {
return new Promise((resolve, reject) => {
// If not on Windows, reject immediately
if (!isWindows) {
reject(new Error('Windows Hello is only available on Windows platforms'));
return;
}
// If module failed to load, reject
if (!winHello) {
reject(new Error('Windows Hello native module could not be loaded'));
return;
}
try {
// The native module will now throw errors directly with specific messages
// if Windows Hello is not available, so we just need to resolve if no error
const result = winHello.isHelloAvailable();
resolve(true);
} catch (error) {
// Pass through any errors from the native module
reject(error);
}
});
},
/**
* Request Windows Hello authentication
* @param {string} [message='Verify your identity'] - Message to display to the user
* @param {Buffer} [windowHandle] - Optional native window handle (from Electron's getNativeWindowHandle)
* @returns {Promise<string>} Promise that resolves on successful authentication, rejects otherwise
*/
requestHello(message = 'Verify your identity', windowHandle = null) {
return new Promise((resolve, reject) => {
// If not on Windows, reject immediately
if (!isWindows) {
reject(new Error('Windows Hello is only available on Windows platforms'));
return;
}
// If module failed to load, reject
if (!winHello) {
reject(new Error('Windows Hello native module could not be loaded'));
return;
}
try {
// Pass both message and window handle to the native module
const result = winHello.requestHello(message, windowHandle);
if (result === 'Success') {
resolve(result);
} else {
reject(new Error(result));
}
} catch (error) {
reject(error);
}
});
}
};
}
export default createWinHello;