public Observable < BackendUser > loginASync ( final LoginCredentials loginCreds ) { logger . debug ( "loginASync(" + loginCreds + ")" ) ; try { setLoginState ( LOGGING_IN ) ; return getWebService ( ) . getPublicKeyA ( ) . flatMap ( new Func1 < byte [ ] , Observable < LoginCredentials > > ( ) { @ Override public Observable < LoginCredentials > call ( byte [ ] bytes ) { try { if ( ! loginCreds . isEncrypted ( ) ) { PublicKey key = Crypto . pubKeyFromBytes ( bytes ) ; loginCreds . encryptPassword ( key ) ; } return Observable . from ( loginCreds ) ; } catch ( Exception e ) { return Observable . error ( e ) ; } } } ) . flatMap ( new Func1 < LoginCredentials , Observable < ValidCredentials > > ( ) { @ Override public Observable < ValidCredentials > call ( LoginCredentials credentials ) { return getWebService ( ) . loginA ( credentials ) ; } } ) . map ( new Func1 < ValidCredentials , BackendUser > ( ) { @ Override public BackendUser call ( ValidCredentials validCredentials ) { return setUser ( validCredentials ) ; } } ) . subscribeOn ( Schedulers . io ( ) ) . observeOn ( config . observeOn ( ) ) ; } catch ( Exception e ) { logger . error ( "Failed to SignIn(" + loginCreds . getEmailAddress ( ) + ")" , e ) ; setLoginState ( LOGGED_OUT ) ; return Observable . error ( e ) ; } }
Asyncronously attempt to log into user account