Skip to content

Instantly share code, notes, and snippets.

@adamtarmstrong
Last active February 6, 2018 15:06
Show Gist options
  • Save adamtarmstrong/425b1522d33e10e91cf9f204d10b4c97 to your computer and use it in GitHub Desktop.
Save adamtarmstrong/425b1522d33e10e91cf9f204d10b4c97 to your computer and use it in GitHub Desktop.
iOS/Android Biometric Auth
/*
The Android UI is provided via my Ti.Widget: ti.androidfingerprintalertdialog
https://github.com/adamtarmstrong/ti.androidfingerprintalertdialog
referenced in the code below as: $.androidFingerprint
*/
function promptForTouchAuth(){ "use strict";
if (OS_IOS) {
$.emailAddress.blur(); //hide keyboard before prompting for biometrics
TiTouchId.authenticate({
reason: 'Use your fingerprint for quick and secure access',
callback: function(e) { "use strict";
if (!e.success) {
switch(e.code) {
case TiTouchId.ERROR_AUTHENTICATION_FAILED:
clearpswd();
Alloy.Globals.Notifier.show({
message: 'Too many attempts. Please re-enter password.',
style: 'warn',
duration: 5000,
onClick: function(){ /* */ }
});
break;
case TiTouchId.ERROR_USER_CANCEL: touchIDIconEnabled(true); break; //user cancelled
case TiTouchId.ERROR_USER_FALLBACK: break; //pressed "enter password"
case TiTouchId.ERROR_SYSTEM_CANCEL: alert('Touch not recognized'); clearpswd(); break; //system cancelled
case TiTouchId.ERROR_PASSCODE_NOT_SET: alert('You must first set a passcode before you can use touch authentication'); clearpswd(); break;
case TiTouchId.ERROR_TOUCH_ID_NOT_AVAILABLE: alert('Your device does not support touch authentication'); clearpswd(); break;
case TiTouchId.ERROR_TOUCH_ID_NOT_ENROLLED: alert('You must enroll at least one finger before you can use touch authentication'); clearpswd(); break;
default: break;
}
} else {
setTimeout(function() { "use strict";
authenticate();
}, 0);
}
}
});
} else if (OS_ANDROID) {
TiTouchId.authenticate({
reason: 'Use your fingerprint for quick and secure access',
callback: function(e) { "use strict";
if (!e.success) {
if (e.error == "Unable to recognize fingerprint"){
$.androidFingerprint.failure();
} else if (e.error == "Fingerprint operation canceled.") {
touchIDIconEnabled(true);
} else { //presumeably too many failures - e.error="Too many attempts. Try again later."
$.androidFingerprint.hide();
clearpswd();
Alloy.Globals.Notifier.show({
message: 'Too many attempts. Please re-enter password.',
style: 'warn',
duration: 5000,
onClick: function(){ /* */ }
});
}
} else {
$.androidFingerprint.success();
setTimeout(function() {
authenticate();
}, 0);
}
}
});
//present user with Android Specific AlertDialog UI Widget
$.androidFingerprint.show(usepasswordAndroidTouchID,cancelAndroidTouchID);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment