@@ -90,10 +90,8 @@ import kotlinx.coroutines.launch
9090import kotlinx.coroutines.withContext
9191import org.jetbrains.annotations.VisibleForTesting
9292
93- internal class AuthenticatorViewModel (
94- application : Application ,
95- private val authProvider : AuthProvider
96- ) : AndroidViewModel(application) {
93+ internal class AuthenticatorViewModel (application : Application , private val authProvider : AuthProvider ) :
94+ AndroidViewModel (application) {
9795
9896 // Constructor for compose viewModels provider
9997 constructor (application: Application ) : this (application, RealAuthProvider ())
@@ -185,7 +183,8 @@ internal class AuthenticatorViewModel(
185183
186184 // region SignUp
187185
188- private suspend fun signUp (username : String , password : String , attributes : List <AuthUserAttribute >) {
186+ @VisibleForTesting
187+ suspend fun signUp (username : String , password : String , attributes : List <AuthUserAttribute >) {
189188 viewModelScope.launch {
190189 val options = AuthSignUpOptions .builder().userAttributes(attributes).build()
191190
@@ -229,6 +228,7 @@ internal class AuthenticatorViewModel(
229228 moveTo(newState)
230229 }
231230 AuthSignUpStep .DONE -> handleSignedUp(username, password)
231+ AuthSignUpStep .COMPLETE_AUTO_SIGN_IN -> handleAutoSignIn(username, password)
232232 else -> {
233233 // Generic error for any other next steps that may be added in the future
234234 val exception = AuthException (
@@ -241,6 +241,18 @@ internal class AuthenticatorViewModel(
241241 }
242242 }
243243
244+ private suspend fun handleAutoSignIn (username : String , password : String ) {
245+ when (val result = authProvider.autoSignIn()) {
246+ is AmplifyResult .Error -> {
247+ // If auto sign in fails then proceed with manually trying to sign in the user. If this also fails the
248+ // user will end up back on the sign in screen.
249+ logger.warn(" Unable to complete auto-signIn" )
250+ handleSignedUp(username, password)
251+ }
252+ is AmplifyResult .Success -> handleSignInSuccess(username, password, result.data)
253+ }
254+ }
255+
244256 private suspend fun handleSignedUp (username : String , password : String ) {
245257 when (val result = authProvider.signIn(username, password)) {
246258 is AmplifyResult .Error -> {
@@ -355,22 +367,15 @@ internal class AuthenticatorViewModel(
355367 )
356368 }
357369
358- private suspend fun handleEmailMfaSetupRequired (
359- username : String ,
360- password : String
361- ) {
370+ private suspend fun handleEmailMfaSetupRequired (username : String , password : String ) {
362371 moveTo(
363372 stateFactory.newSignInContinueWithEmailSetupState(
364373 onSubmit = { mfaType -> confirmSignIn(username, password, mfaType) }
365374 )
366375 )
367376 }
368377
369- private suspend fun handleMfaSelectionRequired (
370- username : String ,
371- password : String ,
372- allowedMfaTypes : Set <MFAType >?
373- ) {
378+ private suspend fun handleMfaSelectionRequired (username : String , password : String , allowedMfaTypes : Set <MFAType >? ) {
374379 if (allowedMfaTypes.isNullOrEmpty()) {
375380 handleGeneralFailure(AuthException (" Missing allowedMfaTypes" , " Please open a bug with Amplify" ))
376381 return
@@ -492,10 +497,7 @@ internal class AuthenticatorViewModel(
492497 }.join()
493498 }
494499
495- private suspend fun handleResetPasswordSuccess (
496- username : String ,
497- result : AuthResetPasswordResult
498- ) {
500+ private suspend fun handleResetPasswordSuccess (username : String , result : AuthResetPasswordResult ) {
499501 when (result.nextStep.resetPasswordStep) {
500502 AuthResetPasswordStep .DONE -> handlePasswordResetComplete()
501503 AuthResetPasswordStep .CONFIRM_RESET_PASSWORD_WITH_CODE -> {
0 commit comments