Skip to content

Move Authenticator to Signed-In State after web UI login #235

@concavegit

Description

@concavegit

Before opening, please confirm:

Language and Async Model

Kotlin

Amplify Categories

Authentication

Gradle script dependencies

amplifyVersion = "2.27.3"
amplifyAuthenticatorVersion = "1.4.3"

Environment information

------------------------------------------------------------
Gradle 8.11.1
------------------------------------------------------------

Build time:    2024-11-20 16:56:46 UTC
Revision:      481cb05a490e0ef9f8620f7873b83bd8a72e7c39

Kotlin:        2.0.20
Groovy:        3.0.22
Ant:           Apache Ant(TM) version 1.10.14 compiled on August 16 2023
Launcher JVM:  17.0.4 (Amazon.com Inc. 17.0.4+8-LTS)
Daemon JVM:    /Library/Java/JavaVirtualMachines/amazon-corretto-17.jdk/Contents/Home (no JDK specified, using current Java home)
OS:            Mac OS X 15.4.1 aarch64

Please include any relevant guides or documentation you're referencing

https://docs.amplify.aws/android/build-a-backend/auth/sign-in-with-web-ui/

Describe the bug

While the context below is more relevant to the amplify-ui-android repository, the solution requires understanding of the authentication state management in this amplify-android repository. Feel free to move this issue as you see fit.

I created a button in the signInContent of the Authenticator that calls Amplify.Auth.signInWithSocialWebUI to sign in with google. The sign in succeeds, but the Authenticator's state is still SignInStateImpl, not SignedInStateImpl. This causes my app's contents to still be behind the login screen. If I force quit the app and restart, the re-initialization of viewModel allows the state to check if the user is logged in again, finally showing the app's contents.

Swift had a similar issue. In Swift's case, I used a state variable that toggles on successful social sign in, allowing the authorizer to refresh. This does not work for Android since Authenticator remembers the old viewModel throughout the app's session.

There are two possible fixes to this bug:

  1. Provide a way for me to signal to the AuthStateMachine that it should enter the SignedInState upon successful social sign in.
  2. Or provide a way for me to completely re-initialize the viewModel so that the viewModel can pick up the fact that the user is now signed in. This is what I used in Swift

Reproduction steps (if applicable)

  1. Create an Authorizer with a button that signs in with google in the Authorizer's signInContent
Amplify.Auth.signInWithSocialWebUI(
    AuthProvider.google(),
    this,
    {
        Log.i(
            "KanaiAuth",
            "Sign in with Google succeeded: $it"
        )
    },
    { Log.e("KanaiAuth", "Sign in with Google failed", it) }
)
  1. Notice that the authorizer logs success, but the log in screen still shows
  2. Notice that if you force quit the app and enter it again, the app's contents now show.

Code Snippet

Authenticator(
    state = newState,
    signInContent = { state ->
        CustomSignIn( // My sign in button that runs onGoogleSignIn
            state = state,
            onGoogleSignIn = {
                // Sign in with Google using Amplify
                Amplify.Auth.signInWithSocialWebUI(
                    AuthProvider.google(),
                    this,
                    {
                        Log.i(
                            "KanaiAuth",
                            "Sign in with Google succeeded: $it"
                        )
                    },
                    { Log.e("KanaiAuth", "Sign in with Google failed", it) }
                )
            }
        )
    }
) { state ->
    HomeScreen(state)
}

Log output

 Sign in with Google succeeded: AuthSignInResult{isSignedIn=true, nextStep=AuthNextSignInStep{signInStep=DONE, additionalInfo={}, codeDeliveryDetails=null, totpSetupDetails=null, allowedMFATypes=null, availableFactors=null}}

Configuration File

No response

GraphQL Schema

No response

Additional information and screenshots

No response

Metadata

Metadata

Assignees

No one assigned

    Labels

    authenticatorThis issue relates to the Authenticator componentfeature-requestNew feature or requestpending-maintainer-responseIssue is pending response from an Amplify team member

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions