Skip to content

Commit

Permalink
feat: wip store biometric in preferences
Browse files Browse the repository at this point in the history
  • Loading branch information
jimcase committed May 9, 2024
1 parent 08123e8 commit 5df94f7
Show file tree
Hide file tree
Showing 6 changed files with 50 additions and 36 deletions.
1 change: 1 addition & 0 deletions src/core/storage/preferences/preferencesStorage.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ enum PreferencesKeys {
APP_IDENTIFIERS_FAVOURITES = "app-identifiers-favourites",
APP_CREDS_FAVOURITES = "app-creds-favourites",
APP_USER_NAME = "app-user-name",
APP_BIOMETRY = "app-biometry",
APP_KERIA_NOTIFICATION_MARKER = "app-keria-notification-marker",
}

Expand Down
11 changes: 6 additions & 5 deletions src/locales/en/en.json
Original file line number Diff line number Diff line change
Expand Up @@ -1180,13 +1180,14 @@
"reason": "Please authenticate",
"canceltitle": "Cancel",
"iosfallbacktitle": "Use passcode",
"androidtitle": "Biometric login",
"androidsubtitle": "Log in using biometric authentication",
"iosnotenabled": "Please enable Face ID in your device settings or update the app for enhanced security",
"androidtitle": "Authentication",
"androidsubtitle": "Authentication using biometrics",
"iosnotenabled": "Biometry not added in iOS info.plist configuration file",
"setupandroidbiometryheader": "Do you want to allow “IDW” to use biometrics?",
"setupandroidbiometryconfirm": "Ok",
"setupandroidbiometrycancel": "Dont allow",
"cancelbiometryheader": "You canceled Biometrics. You can set this up later via the settings page."

"cancelbiometryheader": "You canceled Biometrics. You can set this up later via the settings page.",
"notavailable": "Biometry not available",
"weakbiometry": "Biometry too weak"
}
}
1 change: 0 additions & 1 deletion src/ui/globals/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,6 @@ enum ToastMsgType {
WALLET_CONNECTION_DELETED = "walletconnectiondeleted",
CONNECT_WALLET_SUCCESS = "connectwalletsuccess",
DISCONNECT_WALLET_SUCCESS = "disconnectwallet",
STRONG_BIOMETRY_NOT_AVAILABLE = "strongbiometrynotavailable",
}

const IDENTIFIER_BG_MAPPING: Record<number, unknown> = {
Expand Down
34 changes: 21 additions & 13 deletions src/ui/hooks/useBiometrics.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,37 +29,45 @@ const useBiometricAuth = () => {
}
};

const handleBiometricAuth = async (): Promise<
boolean | BiometryError | undefined
> => {
const handleBiometricAuth = async (): Promise<boolean | BiometryError> => {
const biometricResult = await checkBiometry();
if (!biometricResult?.isAvailable) {
if (!biometricInfo?.strongBiometryIsAvailable) {
return new BiometryError(
i18n.t("biometry.weakbiometry"),
BiometryErrorType.biometryNotAvailable
);
}
if (biometricResult?.strongReason?.includes("NSFaceIDUsageDescription")) {
// TODO: handle error i18n.t("biometry.iosnotenabled")
return new BiometryError(
i18n.t("biometry.iosnotenabled"),
BiometryErrorType.biometryNotAvailable
);
}
return;
return new BiometryError(
i18n.t("biometry.notavailable"),
BiometryErrorType.biometryNotAvailable
);
}

try {
await BiometricAuth.authenticate({
reason: i18n.t("biometry.reason") || "Please authenticate",
cancelTitle: i18n.t("biometry.canceltitle") || "Cancel",
reason: i18n.t("biometry.reason") as string,
cancelTitle: i18n.t("biometry.canceltitle") as string,
allowDeviceCredential: true,
iosFallbackTitle: i18n.t("biometry.iosfallbacktitle") || "Use passcode",
androidTitle: i18n.t("biometry.androidtitle") || "Biometric login",
androidSubtitle:
i18n.t("biometry.androidsubtitle") ||
"Log in using biometric authentication",
iosFallbackTitle: i18n.t("biometry.iosfallbacktitle") as string,
androidTitle: i18n.t("biometry.androidtitle") as string,
androidSubtitle: i18n.t("biometry.androidsubtitle") as string,
androidConfirmationRequired: false,
androidBiometryStrength: AndroidBiometryStrength.strong,
});
setPauseTimestamp(new Date().getTime());
return true;
} catch (error) {
if (error instanceof BiometryError) {
// TODO: Handle other biometry errors here
return error;
}
return new BiometryError(`${error}`, BiometryErrorType.none);
}
};

Expand Down
15 changes: 7 additions & 8 deletions src/ui/pages/LockPage/LockPage.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import i18n from "i18next";
import { useEffect, useState } from "react";
import { BiometryError } from "@aparajita/capacitor-biometric-auth/dist/esm/definitions";
import { ResponsivePageLayout } from "../../components/layout/ResponsivePageLayout";
import { useAppIonRouter } from "../../hooks";
import { useAppDispatch, useAppSelector } from "../../../store/hooks";
Expand All @@ -22,7 +23,6 @@ import { Alert } from "../../components/Alert";
import { useBiometricAuth } from "../../hooks/useBiometrics";

import "./LockPage.scss";
import { useActivityTimer } from "../../components/AppWrapper/hooks/useActivityTimer";
import { ToastMsgType } from "../../globals/types";

const LockPage = () => {
Expand All @@ -34,7 +34,7 @@ const LockPage = () => {
const seedPhrase = authentication.seedPhraseIsSet;
const [alertIsOpen, setAlertIsOpen] = useState(false);
const [passcodeIncorrect, setPasscodeIncorrect] = useState(false);
const { handleBiometricAuth, biometricInfo } = useBiometricAuth();
const { handleBiometricAuth } = useBiometricAuth();

const headerText = seedPhrase
? i18n.t("lockpage.alert.text.verify")
Expand Down Expand Up @@ -94,15 +94,14 @@ const LockPage = () => {
};

const handleBiometrics = async () => {
if (biometricInfo?.strongBiometryIsAvailable) {
const isAuthenticated = await handleBiometricAuth();
if (isAuthenticated === true) {
dispatch(login());
}
const isAuthenticated = await handleBiometricAuth();
if (isAuthenticated instanceof BiometryError) {
dispatch(setToastMsg(isAuthenticated.message as ToastMsgType));
} else {
dispatch(setToastMsg(ToastMsgType.STRONG_BIOMETRY_NOT_AVAILABLE));
dispatch(login());
}
};

const resetPasscode = () => {
SecureStorage.delete(KeyStoreKeys.APP_PASSCODE).then(() => {
dispatch(
Expand Down
24 changes: 15 additions & 9 deletions src/ui/pages/SetPasscode/SetPasscode.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -68,25 +68,29 @@ const SetPasscode = () => {
await processBiometrics();
}
}
await handlePassAuth();
}
}
}
};

const processBiometrics = async () => {
const isBiometricAuthenticated = await handleBiometricAuth();
if (isBiometricAuthenticated instanceof BiometryError) {
if (isBiometricAuthenticated.code === BiometryErrorType.userCancel) {
await handlePassAuth();
}
if (
isBiometricAuthenticated.code === BiometryErrorType.authenticationFailed
) {
return;
if (isBiometricAuthenticated === true) {
await PreferencesStorage.set(PreferencesKeys.APP_BIOMETRY, {
enabled: true,
});
await handlePassAuth();
} else {
if (isBiometricAuthenticated instanceof BiometryError) {
if (isBiometricAuthenticated.code === BiometryErrorType.userCancel) {
setShowCancelAndroidBiometryAlert(true);
} else {
return;
}
}
}
};

const handlePassAuth = async () => {
const data: DataProps = {
store: { stateCache },
Expand Down Expand Up @@ -122,6 +126,8 @@ const SetPasscode = () => {
const handleClearState = () => {
setPasscode("");
setOriginalPassCode("");
setShowCancelAndroidBiometryAlert(false);
setShowSetupAndroidBiometryAlert(false);
};

const handleBeforeBack = () => {
Expand Down

0 comments on commit 5df94f7

Please sign in to comment.