@onairos/react-native 3.0.37 → 3.0.39

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,12 +1,83 @@
1
1
  import { useCallback } from 'react';
2
- import * as Keychain from 'react-native-keychain';
3
2
  import { STORAGE_KEYS } from '../constants';
4
3
  import type { CredentialsResult } from '../types';
5
4
 
5
+ // Create a mock storage for environments without Keychain access
6
+ const mockCredentialStorage: Record<string, any> = {};
7
+
8
+ // Try to import Keychain, but provide fallbacks if not available
9
+ let Keychain: any = null;
10
+ try {
11
+ Keychain = require('react-native-keychain');
12
+ } catch (error) {
13
+ console.warn('react-native-keychain module not available in useCredentials, using mock storage');
14
+ }
15
+
16
+ // Check if Keychain is properly initialized and available
17
+ const isKeychainAvailable = () => {
18
+ try {
19
+ return Keychain && typeof Keychain.getGenericPassword === 'function';
20
+ } catch (e) {
21
+ return false;
22
+ }
23
+ };
24
+
25
+ // Safe wrapper for getGenericPassword
26
+ const safeGetGenericPassword = async (options: any) => {
27
+ try {
28
+ if (isKeychainAvailable()) {
29
+ return await Keychain.getGenericPassword(options);
30
+ } else {
31
+ const key = options?.service || 'default';
32
+ return mockCredentialStorage[key] || null;
33
+ }
34
+ } catch (error) {
35
+ console.warn('Keychain access failed, using mock storage', error);
36
+ const key = options?.service || 'default';
37
+ return mockCredentialStorage[key] || null;
38
+ }
39
+ };
40
+
41
+ // Safe wrapper for setGenericPassword
42
+ const safeSetGenericPassword = async (username: string, password: string, options?: any) => {
43
+ try {
44
+ if (isKeychainAvailable()) {
45
+ return await Keychain.setGenericPassword(username, password, options);
46
+ } else {
47
+ const key = options?.service || 'default';
48
+ mockCredentialStorage[key] = { username, password };
49
+ return true;
50
+ }
51
+ } catch (error) {
52
+ console.warn('Keychain access failed, using mock storage', error);
53
+ const key = options?.service || 'default';
54
+ mockCredentialStorage[key] = { username, password };
55
+ return true;
56
+ }
57
+ };
58
+
59
+ // Safe wrapper for resetGenericPassword
60
+ const safeResetGenericPassword = async (options?: any) => {
61
+ try {
62
+ if (isKeychainAvailable()) {
63
+ return await Keychain.resetGenericPassword(options);
64
+ } else {
65
+ const key = options?.service || 'default';
66
+ delete mockCredentialStorage[key];
67
+ return true;
68
+ }
69
+ } catch (error) {
70
+ console.warn('Keychain access failed, using mock storage', error);
71
+ const key = options?.service || 'default';
72
+ delete mockCredentialStorage[key];
73
+ return true;
74
+ }
75
+ };
76
+
6
77
  export const useCredentials = () => {
7
78
  const hasCredentials = useCallback(async (): Promise<boolean> => {
8
79
  try {
9
- const credentials = await Keychain.getGenericPassword({
80
+ const credentials = await safeGetGenericPassword({
10
81
  service: STORAGE_KEYS.credentials
11
82
  });
12
83
  return !!credentials;
@@ -18,7 +89,7 @@ export const useCredentials = () => {
18
89
 
19
90
  const getCredentials = useCallback(async () => {
20
91
  try {
21
- const credentials = await Keychain.getGenericPassword({
92
+ const credentials = await safeGetGenericPassword({
22
93
  service: STORAGE_KEYS.credentials
23
94
  });
24
95
  if (credentials) {
@@ -37,13 +108,17 @@ export const useCredentials = () => {
37
108
  accessToken: string
38
109
  ): Promise<boolean> => {
39
110
  try {
40
- const options: Keychain.Options = {
41
- accessControl: Keychain.ACCESS_CONTROL.BIOMETRY_ANY,
42
- accessible: Keychain.ACCESSIBLE.WHEN_UNLOCKED,
111
+ const options: any = {
43
112
  service: STORAGE_KEYS.credentials
44
113
  };
45
114
 
46
- await Keychain.setGenericPassword(
115
+ // Only use secure storage options on real devices
116
+ if (isKeychainAvailable()) {
117
+ options.accessControl = Keychain.ACCESS_CONTROL?.BIOMETRY_ANY;
118
+ options.accessible = Keychain.ACCESSIBLE?.WHEN_UNLOCKED;
119
+ }
120
+
121
+ await safeSetGenericPassword(
47
122
  username,
48
123
  JSON.stringify({ userPin, accessToken }),
49
124
  options
@@ -57,7 +132,7 @@ export const useCredentials = () => {
57
132
 
58
133
  const clearCredentials = useCallback(async (): Promise<void> => {
59
134
  try {
60
- await Keychain.resetGenericPassword({
135
+ await safeResetGenericPassword({
61
136
  service: STORAGE_KEYS.credentials
62
137
  });
63
138
  } catch (error) {