1- import KVStore from ' expo-sqlite/kv-store' ;
2- import * as SecureStore from ' expo-secure-store' ;
3- import { AUTH_DEFAULT_OPTIONS } from ' @evolu/common' ;
1+ import KVStore from " expo-sqlite/kv-store" ;
2+ import * as SecureStore from " expo-secure-store" ;
3+ import { AUTH_DEFAULT_OPTIONS } from " @evolu/common" ;
44
5- import type { LocalAuthOptions , SecureStorage , SensitiveInfoItem , StorageMetadata , AccessControl } from "@evolu/common" ;
5+ import type {
6+ LocalAuthOptions ,
7+ SecureStorage ,
8+ SensitiveInfoItem ,
9+ StorageMetadata ,
10+ AccessControl ,
11+ } from "@evolu/common" ;
612
713export const createSecureStore = ( ) : SecureStorage => {
814 const store : SecureStorage = {
915 setItem : async ( key , value , options ) => {
1016 const rnsiOpts = convertOptions ( options ) ;
11- const service = options ?. service ?? 'default' ;
12- const metadata = createMetadata ( options ?. accessControl === 'none' ) ;
13- await KVStore . setItem ( `${ service } -${ key } ` , '1' ) ;
14- await SecureStore . setItemAsync ( key , JSON . stringify ( { value, metadata} ) , rnsiOpts ) ;
17+ const service = options ?. service ?? "default" ;
18+ const metadata = createMetadata ( options ?. accessControl === "none" ) ;
19+ await KVStore . setItem ( `${ service } -${ key } ` , "1" ) ;
20+ await SecureStore . setItemAsync (
21+ key ,
22+ JSON . stringify ( { value, metadata } ) ,
23+ rnsiOpts ,
24+ ) ;
1525 return { metadata } ;
1626 } ,
1727
1828 getItem : async ( key , options ) => {
1929 const rnsiOpts = convertOptions ( options ) ;
20- const service = options ?. service ?? ' default' ;
21- let data : { value : string ; metadata : StorageMetadata } ;
30+ const service = options ?. service ?? " default" ;
31+ let data : { value : string ; metadata : StorageMetadata } ;
2232 try {
2333 const result = await SecureStore . getItemAsync ( key , rnsiOpts ) ;
2434 if ( ! result ) return null ;
25- data = JSON . parse ( result ) as { value : string ; metadata : StorageMetadata } ;
35+ data = JSON . parse ( result ) as {
36+ value : string ;
37+ metadata : StorageMetadata ;
38+ } ;
2639 } catch ( _error ) {
2740 return null ;
2841 }
@@ -31,7 +44,7 @@ export const createSecureStore = (): SecureStorage => {
3144
3245 deleteItem : async ( key , options ) => {
3346 const rnsiOpts = convertOptions ( options ) ;
34- const service = options ?. service ?? ' default' ;
47+ const service = options ?. service ?? " default" ;
3548 await Promise . all ( [
3649 KVStore . removeItemAsync ( `${ service } -${ key } ` ) ,
3750 SecureStore . deleteItemAsync ( key , rnsiOpts ) ,
@@ -41,21 +54,27 @@ export const createSecureStore = (): SecureStorage => {
4154
4255 getAllItems : async ( options ) => {
4356 const keys = await KVStore . getAllKeysAsync ( ) ;
44- const service = options ?. service ?? ' default' ;
45- const metadata = createMetadata ( options ?. accessControl === ' none' ) ;
57+ const service = options ?. service ?? " default" ;
58+ const metadata = createMetadata ( options ?. accessControl === " none" ) ;
4659 return keys
47- . filter ( key => key . startsWith ( `${ service } -` ) )
48- . map ( key => ( { key : key . slice ( service . length + 1 ) , service, metadata } ) ) ;
60+ . filter ( ( key ) => key . startsWith ( `${ service } -` ) )
61+ . map ( ( key ) => ( {
62+ key : key . slice ( service . length + 1 ) ,
63+ service,
64+ metadata,
65+ } ) ) ;
4966 } ,
5067
5168 clearService : async ( options ) => {
5269 const rnsiOpts = convertOptions ( options ) ;
53- const service = options ?. service ?? ' default' ;
70+ const service = options ?. service ?? " default" ;
5471 const items = await store . getAllItems ( options ) ;
55- await KVStore . multiRemove ( items . map ( item => `${ service } -${ item . key } ` ) ) ;
56- await Promise . all ( items . map ( async ( item ) => {
57- await SecureStore . deleteItemAsync ( item . key , rnsiOpts ) ;
58- } ) ) ;
72+ await KVStore . multiRemove ( items . map ( ( item ) => `${ service } -${ item . key } ` ) ) ;
73+ await Promise . all (
74+ items . map ( async ( item ) => {
75+ await SecureStore . deleteItemAsync ( item . key , rnsiOpts ) ;
76+ } ) ,
77+ ) ;
5978 } ,
6079 } ;
6180
@@ -75,29 +94,34 @@ const createMetadata = (isSecure = true): SensitiveInfoItem["metadata"] => {
7594 } ;
7695} ;
7796
78- function convertOptions ( options ?: LocalAuthOptions ) : SecureStore . SecureStoreOptions {
79- const accessGroup = options ?. keychainGroup
80- ?? AUTH_DEFAULT_OPTIONS . keychainGroup
81- ?? '' ;
82- const keychainService = options ?. service
83- ?? AUTH_DEFAULT_OPTIONS . service
84- ?? '' ;
85- const keychainAccessible = convertKeychainAccessible ( options ?. accessControl
86- ?? AUTH_DEFAULT_OPTIONS . accessControl
87- ?? 'biometryCurrentSet' ) ;
88- const authenticationPrompt = options ?. authenticationPrompt ?. title
89- ?? AUTH_DEFAULT_OPTIONS . authenticationPrompt ?. title
90- ?? '' ;
97+ function convertOptions (
98+ options ?: LocalAuthOptions ,
99+ ) : SecureStore . SecureStoreOptions {
100+ const accessGroup =
101+ options ?. keychainGroup ?? AUTH_DEFAULT_OPTIONS . keychainGroup ?? "" ;
102+ const keychainService =
103+ options ?. service ?? AUTH_DEFAULT_OPTIONS . service ?? "" ;
104+ const keychainAccessible = convertKeychainAccessible (
105+ options ?. accessControl ??
106+ AUTH_DEFAULT_OPTIONS . accessControl ??
107+ "biometryCurrentSet" ,
108+ ) ;
109+ const authenticationPrompt =
110+ options ?. authenticationPrompt ?. title ??
111+ AUTH_DEFAULT_OPTIONS . authenticationPrompt ?. title ??
112+ "" ;
91113 return {
92114 accessGroup,
93115 keychainService,
94116 keychainAccessible,
95117 authenticationPrompt,
96- requireAuthentication : options ?. accessControl !== ' none' ,
118+ requireAuthentication : options ?. accessControl !== " none" ,
97119 } ;
98120}
99121
100- function convertKeychainAccessible ( accessControl : AccessControl ) : SecureStore . KeychainAccessibilityConstant {
122+ function convertKeychainAccessible (
123+ accessControl : AccessControl ,
124+ ) : SecureStore . KeychainAccessibilityConstant {
101125 switch ( accessControl ) {
102126 case "none" :
103127 // eslint-disable-next-line @typescript-eslint/no-deprecated
@@ -111,8 +135,9 @@ function convertKeychainAccessible(accessControl: AccessControl): SecureStore.Ke
111135 case "secureEnclaveBiometry" :
112136 return SecureStore . AFTER_FIRST_UNLOCK ;
113137 // Exhaustive check
114- default : accessControl satisfies never ;
115- // Default (for typescript, should never hit)
116- return SecureStore . AFTER_FIRST_UNLOCK ;
138+ default :
139+ accessControl satisfies never ;
140+ // Default (for typescript, should never hit)
141+ return SecureStore . AFTER_FIRST_UNLOCK ;
117142 }
118143}
0 commit comments