Skip to content

Commit

Permalink
feat(DevScreen): 添加本地身份验证功能,确保用户认证后才能访问开发选项
Browse files Browse the repository at this point in the history
  • Loading branch information
thoulee21 committed Nov 15, 2024
1 parent afa3f23 commit bb2426d
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 2 deletions.
2 changes: 1 addition & 1 deletion src/components/DevItem.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ export const DevItem = () => {
return (
<List.Item
title="Developer Options"
description="Access developer settings."
description="Access developer settings"
left={CodeTags}
right={ChevronRight}
onPress={() => {
Expand Down
45 changes: 44 additions & 1 deletion src/pages/dev.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,9 @@ import {
type ScrollLargeHeaderProps,
} from '@codeherence/react-native-header';
import { useNavigation } from '@react-navigation/native';
import React, { useCallback } from 'react';
import type { LocalAuthenticationResult } from 'expo-local-authentication';
import * as LocalAuthentication from 'expo-local-authentication';
import React, { useCallback, useEffect, useState } from 'react';
import { useWindowDimensions, View } from 'react-native';
import { Divider, List, useTheme } from 'react-native-paper';
import { AniGalleryItem } from '../components/AniGalleryItem';
Expand All @@ -14,15 +16,50 @@ import {
} from '../components/AnimatedHeader';
import { ClearAllDataItem } from '../components/ClearAllDataItem';
import { DevSwitchItem } from '../components/DevSwitchItem';
import { LottieAnimation } from '../components/LottieAnimation';
import { RestartItem } from '../components/RestartItem';
import { ViewAppDataItem } from '../components/ViewAppDataItem';
import type { ListLRProps } from '../types/paperListItem';
import { rootLog } from '../utils/logger';

export function DevScreen() {
const navigation = useNavigation();
const { height } = useWindowDimensions();
const appTheme = useTheme();

const [isLoaded, setIsLoaded] = useState(false);
const [authResult, setAuthResult] = useState<LocalAuthenticationResult>();

useEffect(() => {
const init = async () => {
const hasHardware = await LocalAuthentication.hasHardwareAsync();
rootLog.debug('hasHardware', hasHardware);

const supported = await LocalAuthentication.supportedAuthenticationTypesAsync();
rootLog.debug('supported', supported);

const enrolled = await LocalAuthentication.isEnrolledAsync();
rootLog.info('enrolled', enrolled);

const result = await LocalAuthentication.authenticateAsync();
rootLog.info('result', result);
setAuthResult(result);

if (result.success) {
rootLog.info('Authenticated!');
} else {
rootLog.warn('Not authenticated!');
navigation.goBack();
}
};

if (!isLoaded) {
init().then(() => {
setIsLoaded(true);
});
}
}, [isLoaded, navigation]);

const renderTestIcon = useCallback((props: ListLRProps) => (
<List.Icon icon="test-tube" {...props} />
), []);
Expand All @@ -45,6 +82,12 @@ export function DevScreen() {
<List.Icon icon="folder-eye-outline" {...props} />
), []);

if (!authResult || !authResult.success) {
return (
<LottieAnimation animation="rocket" />
);
}

return (
<ScrollViewWithHeaders
LargeHeaderComponent={renderLargeHeader}
Expand Down

0 comments on commit bb2426d

Please sign in to comment.