import { injectable } from 'tsyringe' ; import { Observable , Observer } from 'rxjs' ; import * as Keychain from 'react-native-keychain' ; import { LocalException } from '@core' ; export interface LocalAuthenticationDataSource { saveToken ( username , token ) : Observable < boolean > ; getToken ( ) : Observable < string > ; } @ injectable ( ) export class KeyChainAuthenticationDataSource implements LocalAuthenticationDataSource { saveToken ( username , token ) { return Observable . create ( async ( observer ) => { try { await Keychain . setGenericPassword ( username , token ) ; observer . next ( true ) ; observer . complete ( ) ; } catch ( error ) { observer . error ( new LocalException ( error ) ) ; } } ) ; } getToken ( ) { return Observable . create ( async ( observer ) => { try { const result = await Keychain . getGenericPassword ( ) ; if ( result ) { observer . next ( result . password ) ; observer . complete ( ) ; return ; } observer . error ( new LocalException ( { } ) ) ; } catch ( error ) { observer . error ( new LocalException ( error ) ) ; } } ) ; } }