-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add phone lock (pin, biometrics, etc) (#120)
* feat: add biometrics * chore: add alert in security page * chore: remove switch colors from constants * fix: copy & component usage * chore: fix inactivity threshold and use key constant * chore: use boolean to string method * chore: do not store is biometric supported --------- Co-authored-by: René Aaron <rene@twentyuno.net>
- Loading branch information
1 parent
77f42dc
commit 0d03586
Showing
15 changed files
with
406 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
import { Security } from "../../pages/settings/Security"; | ||
|
||
export default function Page() { | ||
return <Security />; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
import { Unlock } from "../pages/Unlock"; | ||
|
||
export default function Page() { | ||
return <Unlock />; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,96 @@ | ||
import * as SwitchPrimitives from '@rn-primitives/switch'; | ||
import * as React from 'react'; | ||
import { Platform } from 'react-native'; | ||
import Animated, { | ||
interpolateColor, | ||
useAnimatedStyle, | ||
useDerivedValue, | ||
withTiming, | ||
} from 'react-native-reanimated'; | ||
import { useColorScheme } from '~/lib/useColorScheme'; | ||
import { cn } from '~/lib/utils'; | ||
|
||
const SwitchWeb = React.forwardRef< | ||
React.ElementRef<typeof SwitchPrimitives.Root>, | ||
React.ComponentPropsWithoutRef<typeof SwitchPrimitives.Root> | ||
>(({ className, ...props }, ref) => ( | ||
<SwitchPrimitives.Root | ||
className={cn( | ||
'peer flex-row h-6 w-11 shrink-0 cursor-pointer items-center rounded-full border-2 border-transparent transition-colors focus-visible:outline-none focus-visible:ring-2 focus-visible:ring-ring focus-visible:ring-offset-2 focus-visible:ring-offset-background disabled:cursor-not-allowed', | ||
props.checked ? 'bg-amber-300' : 'bg-input', | ||
props.disabled && 'opacity-50', | ||
className | ||
)} | ||
{...props} | ||
ref={ref} | ||
> | ||
<SwitchPrimitives.Thumb | ||
className={cn( | ||
'pointer-events-none block h-5 w-5 rounded-full bg-background shadow-md shadow-foreground/5 ring-0 transition-transform', | ||
props.checked ? 'translate-x-5' : 'translate-x-0' | ||
)} | ||
/> | ||
</SwitchPrimitives.Root> | ||
)); | ||
|
||
SwitchWeb.displayName = 'SwitchWeb'; | ||
|
||
const RGB_COLORS = { | ||
light: { | ||
primary: 'rgb(255, 224, 112)', | ||
input: 'rgb(228, 228, 231)', | ||
}, | ||
dark: { | ||
primary: 'rgb(255, 224, 112)', | ||
input: 'rgb(228, 228, 231)', | ||
}, | ||
} as const; | ||
|
||
const SwitchNative = React.forwardRef< | ||
React.ElementRef<typeof SwitchPrimitives.Root>, | ||
React.ComponentPropsWithoutRef<typeof SwitchPrimitives.Root> | ||
>(({ className, ...props }, ref) => { | ||
const { colorScheme } = useColorScheme(); | ||
const translateX = useDerivedValue(() => (props.checked ? 18 : 0)); | ||
const animatedRootStyle = useAnimatedStyle(() => { | ||
return { | ||
backgroundColor: interpolateColor( | ||
Number(props.checked), | ||
[0, 1], | ||
[RGB_COLORS[colorScheme].input, RGB_COLORS[colorScheme].primary] | ||
), | ||
}; | ||
}); | ||
const animatedThumbStyle = useAnimatedStyle(() => ({ | ||
transform: [{ translateX: withTiming(translateX.value, { duration: 200 }) }], | ||
})); | ||
return ( | ||
<Animated.View | ||
style={animatedRootStyle} | ||
className={cn('h-8 w-[46px] rounded-full', props.disabled && 'opacity-50')} | ||
> | ||
<SwitchPrimitives.Root | ||
className={cn( | ||
'flex-row h-8 w-[46px] shrink-0 items-center rounded-full border-2 border-transparent', | ||
className | ||
)} | ||
{...props} | ||
ref={ref} | ||
> | ||
<Animated.View style={animatedThumbStyle}> | ||
<SwitchPrimitives.Thumb | ||
className={'h-7 w-7 rounded-full bg-background shadow-md shadow-foreground/25 ring-0'} | ||
/> | ||
</Animated.View> | ||
</SwitchPrimitives.Root> | ||
</Animated.View> | ||
); | ||
}); | ||
SwitchNative.displayName = 'SwitchNative'; | ||
|
||
const Switch = Platform.select({ | ||
web: SwitchWeb, | ||
default: SwitchNative, | ||
}); | ||
|
||
export { Switch }; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
import * as React from "react"; | ||
import { AppState, AppStateStatus, NativeEventSubscription } from 'react-native'; | ||
import { secureStorage } from "~/lib/secureStorage"; | ||
import { INACTIVITY_THRESHOLD } from "~/lib/constants"; | ||
import { lastActiveTimeKey, useAppStore } from "~/lib/state/appStore"; | ||
|
||
export const UserInactivityProvider = ({ children }: any) => { | ||
const [appState, setAppState] = React.useState<AppStateStatus>(AppState.currentState); | ||
const isSecurityEnabled = useAppStore((store) => store.isSecurityEnabled); | ||
|
||
const handleAppStateChange = async (nextState: AppStateStatus) => { | ||
if (appState === "active" && nextState.match(/inactive|background/)) { | ||
const now = Date.now(); | ||
secureStorage.setItem(lastActiveTimeKey, now.toString()); | ||
} else if (appState.match(/inactive|background/) && nextState === "active") { | ||
const lastActiveTime = secureStorage.getItem(lastActiveTimeKey); | ||
if (lastActiveTime) { | ||
const timeElapsed = Date.now() - parseInt(lastActiveTime, 10); | ||
if (timeElapsed >= INACTIVITY_THRESHOLD) { | ||
useAppStore.getState().setUnlocked(false) | ||
} | ||
} | ||
await secureStorage.removeItem(lastActiveTimeKey); | ||
} | ||
setAppState(nextState); | ||
}; | ||
|
||
React.useEffect(() => { | ||
let subscription: NativeEventSubscription | ||
if (isSecurityEnabled) { | ||
subscription = AppState.addEventListener("change", handleAppStateChange); | ||
} | ||
|
||
return () => { | ||
if (subscription) { | ||
subscription.remove(); | ||
} | ||
}; | ||
}, [appState, isSecurityEnabled]); | ||
|
||
return children; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
import * as LocalAuthentication from "expo-local-authentication"; | ||
|
||
export async function isBiometricSupported() { | ||
const compatible = await LocalAuthentication.hasHardwareAsync(); | ||
const securityLevel = await LocalAuthentication.getEnrolledLevelAsync(); | ||
return compatible && securityLevel > 0 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.