import React , { Component } from 'react' ; import { Modal , Platform , ScrollView , StyleSheet , Text , TouchableOpacity , View } from 'react-native' ; import AsyncStorage from '@react-native-community/async-storage' ; const styles = StyleSheet . create ( { closeBtn : { justifyContent : 'center' , alignItems : 'center' , backgroundColor : '#eee' , height : 50 , marginRight : 5 } } ) ; import * as CookieManager from 'react-native-cookies' ; import SInfo , { RNSensitiveInfoOptions , SensitiveInfoEntry } from 'react-native-sensitive-info' ; import TouchableRow from './TouchableRow' ; export interface CookieMangerState { data : string | null ; } export interface CookieMangerProps { sInfoKeys ? : RNSensitiveInfoOptions ; } export default class CookieManger extends Component < CookieMangerProps , CookieMangerState > { constructor ( props ) { super ( props ) ; this . state = { data : null } ; } showData = ( data ) => { this . setState ( { data } ) ; } ; cleanData = ( ) => { this . setState ( { data : null } ) ; } ; render ( ) { return ( < View style = { { flex : 1 } } > < TouchableRow text = 'Cookies - Clear' onPress = { this . clearCookies } / > < TouchableRow text = 'Cookies - View (iOS only)' onPress = { this . viewCookies } / > < TouchableRow text = 'AsyncStorage - Clear' onPress = { this . clearAsyncStorage } / > < TouchableRow text = 'AsyncStorage - View' onPress = { this . viewAsyncStorage } / > < TouchableRow text = 'Sensitive Info - Clear' onPress = { this . clearSensitiveInfo } / > < TouchableRow text = 'Sensitive Info - View' onPress = { this . viewSensitiveInfo } / > < Modal visible = { ! ! this . state . data } > < ScrollView > < View style = { { paddingVertical : 20 , paddingHorizontal : 10 } } > < Text > { this . state . data } < /Text> < /View> < /ScrollView> < TouchableOpacity style = { styles . closeBtn } onPress = { this . cleanData } > < Text > OK < /Text> < /TouchableOpacity> < /Modal> < /View>); } clearCookies = ( ) => { CookieManager . clearAll ( ) . then ( ( ) => { alert ( 'Cookie cleared.' ) ; } ) . catch ( ( err ) => { return alert ( `Cookie clear failed. Error: ${ err }` ) ; } ) ; } ; viewCookies = ( ) => { if ( Platform . OS !== 'ios' ) { return alert ( 'View Cookie is iOS only.' ) ; } CookieManager . getAll ( ) . then ( ( res ) => { this . showData ( JSON . stringify ( res , null , '  ' ) ) ; } ) . catch ( ( err ) => { return alert ( `Cookie View failed. Error: ${ err }` ) ; } ) ; } ; clearAsyncStorage = ( ) => { AsyncStorage . clear ( ) . then ( ( ) => { alert ( 'AsyncStorage cleared.' ) ; } ) . catch ( err => { alert ( `AsyncStorage clear failed. Error: ${ err }` ) ; } ) ; } ; viewAsyncStorage = ( ) => { AsyncStorage . getAllKeys ( ) . then ( keys => { AsyncStorage . multiGet ( keys ) . then ( stores => { this . showData ( JSON . stringify ( stores , null , '  ' ) ) ; } ) . catch ( e => console . log ( 'cannot get multi values from AsyncStorage' , e ) ) ; } ) . catch ( e => console . log ( 'cannot get all keys from AsyncStorage' , e ) ) ; } ; clearSensitiveInfo = ( ) => { SInfo . getAllItems ( this . props . sInfoKeys || { } ) . then ( ( values ) => { if ( ! values || ! values [ 0 ] ) { return alert ( 'Nothing to be cleared.' ) ; } const keys = values [ 0 ] . map ( ( item ) => item . key ) ; Promise . all ( keys . map ( async ( k ) => SInfo . deleteItem ( k , this . props . sInfoKeys || { } ) ) ) . then ( ( ) => { alert ( `Cleared: ${ keys }` ) ; } ) . catch ( e => console . log ( 'cannot delete item from react-native-sensitive-info' , e ) ) ; } ) . catch ( e => console . log ( 'cannot get all items from react-native-sensitive-info' , e ) ) ; } ; viewSensitiveInfo = ( ) => { SInfo . getAllItems ( { } ) . then ( ( values ) => { this . showData ( JSON . stringify ( values , null , '  ' ) ) ; } ) . catch ( e => console . warn ( 'cannot view sensitive info' , e ) ) ; } ; }