RXSwift user login on error and continue

I'm new to RX, and I'm trying to figure out how I can continue the task after an error requiring user input.

A concrete example is two-factor authentication. We have an auth-service and a secure resource. Upon entering the system, we receive a LOA-2 token (username and password) from an authorized user. trying to extract data from a protected resource, we get an error message requiring LOA-3 (two-factor). Thus, we need to get data from the user, send it to auth-service, get a new token (LOA-3) and repeat our fetch call with a new token.

There are many examples for logins, but I can’t wrap my head around the continuation of a chain that requires user input.

Any ideas? Thanks:)

+1
source share
1 answer

You will need a catchError function to recover from an error and launch a new observable that triggers an alternative behavior.

So, for example, you need a manufacturer that gets a username and password ...

 let credentialInput = Observable.combineLatest(usernameLabel.rx_text, passwordLabel.rx_text) 

You will probably want to wait until the user closes the Login button ...

 let credentials = credentialInput.sample(loginButton.rx_tap) 

Then enter the token.

 let loaToken = credentials.flatMap { serverLogin($0, $1) }.catchError { error in if error == loa3Error { return getLOA3Data().flatMap { loa3ServerLogin($0) } } else { throw error } } 

getLOA3Data is a function that returns an Observable that contains the data needed for loa3 authentication.

The above is of course pseudo code, but I expect it to give you a good idea on how to wrap your head around a problem.

0
source

Source: https://habr.com/ru/post/1237738/


All Articles