Skip to content

Commit

Permalink
biometrics support (keep-starknet-strange#106)
Browse files Browse the repository at this point in the history
  • Loading branch information
ugur-eren authored May 26, 2024
1 parent 8566118 commit 3ee80d9
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 104 deletions.
11 changes: 7 additions & 4 deletions JoyboyCommunity/app.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,7 @@
"image": "./assets/splash.png",
"backgroundColor": "#ffffff"
},
"assetBundlePatterns": [
"**/*"
],
"assetBundlePatterns": ["**/*"],
"ios": {
"supportsTablet": true
},
Expand All @@ -27,7 +25,12 @@
"favicon": "./assets/favicon.png"
},
"plugins": [
"expo-secure-store"
[
"expo-secure-store",
{
"faceIDPermission": "Allow $(PRODUCT_NAME) to use Face ID."
}
]
]
}
}
45 changes: 23 additions & 22 deletions JoyboyCommunity/src/modules/login/index.tsx
Original file line number Diff line number Diff line change
@@ -1,18 +1,16 @@
import * as SecureStore from 'expo-secure-store';
import React, {useEffect, useState} from 'react';
import {Platform} from 'react-native';
import {Alert} from 'react-native';

import {Typography} from '../../components';
import {useAuth} from '../../store/auth';
import {useNavigationStore} from '../../store/navigation';
import {
getCredentialsWithBiometry,
isBiometrySupported,
saveCredentialsWithBiometry,
} from '../../utils/keychain';
import {generateRandomKeypair, getPublicKeyFromSecret} from '../../utils/keypair';
import {
retrieveAndDecryptPrivateKey,
retrievePassword,
retrievePublicKey,
storePassword,
storePrivateKey,
storePublicKey,
} from '../../utils/storage';
Expand All @@ -34,8 +32,6 @@ enum LoginStep {
}

export default function Login() {
const bypassBiometric = Platform.OS == 'web' ? true : false;

const setNavigationStack = useNavigationStore((state) => state.setStack);
const setAuth = useAuth((state) => state.setAuth);

Expand All @@ -52,12 +48,11 @@ export default function Login() {

useEffect(() => {
(async () => {
const biometrySupported = await isBiometrySupported();

if (biometrySupported && !bypassBiometric) {
const credentials = await getCredentialsWithBiometry();
const biometrySupported = SecureStore.canUseBiometricAuthentication();
if (biometrySupported) {
const storedPassword = await retrievePassword();

if (credentials) setPassword(credentials.password);
if (storedPassword) setPassword(storedPassword);
}
})();
}, []);
Expand All @@ -78,9 +73,21 @@ export default function Login() {
await storePrivateKey(secretKeyHex, password);
await storePublicKey(publicKey);

const biometrySupported = await isBiometrySupported();
if (biometrySupported && !bypassBiometric) {
await saveCredentialsWithBiometry(publicKey, password);
const biometySupported = SecureStore.canUseBiometricAuthentication();
if (biometySupported) {
Alert.alert('Easy login', 'Would you like to use biometrics to login?', [
{
text: 'No',
style: 'cancel',
},
{
text: 'Yes',
style: 'default',
onPress: async () => {
storePassword(password);
},
},
]);
}

setAuth(publicKey, secretKey);
Expand Down Expand Up @@ -109,12 +116,6 @@ export default function Login() {
await storePrivateKey(secretKeyHex, password);
await storePublicKey(publicKey);

const biometrySupported = await isBiometrySupported();

if (biometrySupported && !bypassBiometric) {
await saveCredentialsWithBiometry(publicKey, password);
}

setAuth(publicKey, secretKey);
setNavigationStack('app');
};
Expand Down
78 changes: 0 additions & 78 deletions JoyboyCommunity/src/utils/keychain.ts

This file was deleted.

14 changes: 14 additions & 0 deletions JoyboyCommunity/src/utils/storage.ts
Original file line number Diff line number Diff line change
Expand Up @@ -69,3 +69,17 @@ export const retrieveAndDecryptPrivateKey = async (
throw new Error('Error retrieving and decrypting private key');
}
};

export const storePassword = async (password: string) => {
if (isSecureStoreAvailable) {
return SecureStore.setItemAsync('password', password, {requireAuthentication: true});
}
};

export const retrievePassword = async () => {
if (isSecureStoreAvailable) {
return SecureStore.getItemAsync('password', {requireAuthentication: true});
}

return null;
};

0 comments on commit 3ee80d9

Please sign in to comment.