Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

WebUI reopens after successful sign up #2968

Open
1 task done
dawidhyzy opened this issue Dec 20, 2024 · 3 comments
Open
1 task done

WebUI reopens after successful sign up #2968

dawidhyzy opened this issue Dec 20, 2024 · 3 comments
Labels
question General question

Comments

@dawidhyzy
Copy link

Before opening, please confirm:

Language and Async Model

Kotlin - Coroutines

Amplify Categories

Authentication

Gradle script dependencies

// Put output below this line
amplify = "2.25.1"

amplifyframework-core = { module = "com.amplifyframework:core-kotlin", version.ref = "amplify" }
amplifyframework-aws-auth-cognito = { module = "com.amplifyframework:aws-auth-cognito", version.ref = "amplify" }

Environment information

# Put output below this line
------------------------------------------------------------
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.12 (JetBrains s.r.o. 17.0.12+1-b1207.37)
Daemon JVM:    /Users/dawidhyzy/Library/Java/JavaVirtualMachines/jbr-17.0.12-1/Contents/Home (no JDK specified, using current Java home)
OS:            Mac OS X 15.1.1 aarch64

Please include any relevant guides or documentation you're referencing

No response

Describe the bug

After a successful signup, the WebUI closes and then reopens.
I can see in my logs that I received a successful result.
I can also see logs from AuthClient

2024-12-20 11:45:43.990  9663-9663  AuthClient  com.xxx.yyy.zzz     D  Handling auth redirect response
2024-12-20 11:45:44.037  9663-9663  AuthClient  com.xxx.yyy.zzz     D  CustomTabsManagerActivity was created with a null state.

Reproduction steps (if applicable)

No response

Code Snippet

// Put your code below this line.

Log output

// Put your logs below this line


amplifyconfiguration.json

No response

GraphQL Schema

// Put your schema below this line

Additional information and screenshots

No response

@github-actions github-actions bot added pending-triage Issue is pending triage pending-maintainer-response Issue is pending response from an Amplify team member labels Dec 20, 2024
@tylerjroach
Copy link
Member

Can you show sample code for how you are calling signInWithWebUI and how you have your redirects set in the manifest? Also, what launchMode is set for the activity you are calling signInWithWebUI in?

@github-actions github-actions bot removed the pending-maintainer-response Issue is pending response from an Amplify team member label Dec 20, 2024
@tylerjroach tylerjroach added the question General question label Dec 20, 2024
@github-actions github-actions bot removed the pending-triage Issue is pending triage label Dec 20, 2024
@dawidhyzy
Copy link
Author

dawidhyzy commented Dec 20, 2024

Sure! We have a single Activity with default launch options.

// Manifest

<application>
        <activity
            android:name="com.amplifyframework.auth.cognito.activities.HostedUIRedirectActivity"
            android:exported="true">
            <intent-filter>
                <action android:name="android.intent.action.VIEW" />
                <category android:name="android.intent.category.DEFAULT" />
                <category android:name="android.intent.category.BROWSABLE" />
                <data android:scheme="com.smith" />
            </intent-filter>
        </activity>
    </application>

// UI

@Composable
internal fun LoginScreen(
    state: LoginUiState,
    navigateBack: () -> Unit,
    modifier: Modifier = Modifier,
) {
    val activity = LocalContext.current as Activity

    Box(
        modifier.fillMaxSize()
    ) {
        when (state) {
            LoginUiState.Loading, LoginUiState.LoginStarted -> Loading(Modifier.align(Alignment.Center))
            is LoginUiState.Ready -> {
                LaunchedEffect(Unit) {
                    state.startAuthentication(activity)
                }
            }
            LoginUiState.LoggedIn -> LoggedIn(navigateBack = navigateBack)
            LoginUiState.Failed -> LoginFailed(navigateBack = navigateBack)
        }
    }
}

//ViewModel

@Immutable
sealed interface LoginUiState {
    @Stable
    data object Loading : LoginUiState

    @Stable
    data class Ready(
        val startAuthentication: suspend (activity: Activity) -> Unit,
    ) : LoginUiState

    @Stable
    data object LoginStarted : LoginUiState

    @Stable
    data object LoggedIn : LoginUiState

    @Stable
    data object Failed : LoginUiState
}

private enum class LoginResult {
    None,
    Started,
    Success,
    Failed
}

class LoginViewModel @Inject constructor() : ViewModel() {

    private val signInState = MutableStateFlow(LoginResult.None)

    val uiState = stateStream().stateIn(
        viewModelScope,
        SharingStarted.WhileSubscribed(StateFlowTimeouts.SHARING),
        LoginUiState.Loading
    )

    private fun stateStream(): Flow<LoginUiState> =
        signInState
            .map { loginResult ->
                when (loginResult) {
                    LoginResult.None -> {
                        LoginUiState.Ready(
                            startAuthentication = { activity -> 
                                loginStarted()
                                requireAuth().startAuthentication(activity)
                            }
                        )
                    }

                    LoginResult.Started -> LoginUiState.LoginStarted
                    LoginResult.Success -> LoginUiState.LoggedIn
                    LoginResult.Failed -> LoginUiState.Failed
                }
            }

    private fun Authentication.startAuthentication(activity: Activity) {
        viewModelScope.launch {
            val result = Amplify.Auth.signInWithSocialWebUI(AuthProvider.custom("XYZ"), activity)
            when (result) {
                is AuthSignInResult -> {
                    if (result.isSignedIn) {
                        signInState.emit(LoginResult.Success)
                    } else {
                        signInState.emit(LoginResult.Failed)
                    }
                }

                else -> signInState.emit(LoginResult.Failed)
            }
        }
    }

   private fun loginStarted() {
        viewModelScope.launch {
            signInState.emit(LoginResult.Started)
        }
    }
}

@github-actions github-actions bot added the pending-maintainer-response Issue is pending response from an Amplify team member label Dec 20, 2024
@tylerjroach
Copy link
Member

I can't replicate this but the only thing I'm curious about is the custom auth provider. Who is the provider? My suspicion is that it is related to that. In a test environment, do you have standard signInWithWebUI also configured. I'd be curious if you are seeing the same issue, or seeing the issue on a different provider (if you have multiple).

@github-actions github-actions bot removed the pending-maintainer-response Issue is pending response from an Amplify team member label Dec 20, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
question General question
Projects
None yet
Development

No branches or pull requests

2 participants