-
Notifications
You must be signed in to change notification settings - Fork 1.9k
feat: Anonymous Auth & Upgrade #2247
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
Merged
Merged
Changes from 9 commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
2323cbf
feat: extract routes from example app to main library
Lyokone 024ec8c
add emulator flag
Lyokone d24256a
fixing tests
Lyokone 15d0565
fix tests
Lyokone 3dcd719
feat: Anonymous sign in and auto upgrade
demolaf 1a2d438
Merge branch 'feat/extract' of https://github.com/firebase/FirebaseUI…
demolaf 473610f
e2e tests anonymous sign in and auto upgrade when enabled
demolaf 1c1efb2
Merge branch 'version-10.0.0-dev' of https://github.com/firebase/Fire…
demolaf c80b763
unit tests for anonymous sign in
demolaf d9fc828
fix feedback
demolaf 7555db2
Merge branch 'version-10.0.0-dev' of https://github.com/firebase/Fire…
demolaf File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
126 changes: 126 additions & 0 deletions
126
...ebase/ui/auth/compose/configuration/auth_provider/AnonymousAuthProvider+FirebaseAuthUI.kt
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,126 @@ | ||
| package com.firebase.ui.auth.compose.configuration.auth_provider | ||
|
|
||
| import androidx.compose.runtime.Composable | ||
| import androidx.compose.runtime.remember | ||
| import androidx.compose.runtime.rememberCoroutineScope | ||
| import com.firebase.ui.auth.compose.AuthException | ||
| import com.firebase.ui.auth.compose.AuthState | ||
| import com.firebase.ui.auth.compose.FirebaseAuthUI | ||
| import kotlinx.coroutines.CancellationException | ||
| import kotlinx.coroutines.launch | ||
| import kotlinx.coroutines.tasks.await | ||
|
|
||
| /** | ||
| * Creates a remembered launcher function for anonymous sign-in. | ||
| * | ||
| * @return A launcher function that starts the anonymous sign-in flow when invoked | ||
| * | ||
| * @see signInAnonymously | ||
| * @see createOrLinkUserWithEmailAndPassword for upgrading anonymous accounts | ||
| */ | ||
| @Composable | ||
| internal fun FirebaseAuthUI.rememberAnonymousSignInHandler(): () -> Unit { | ||
| val coroutineScope = rememberCoroutineScope() | ||
| return remember(this) { | ||
| { | ||
| coroutineScope.launch { | ||
| try { | ||
| signInAnonymously() | ||
| } catch (e: Exception) { | ||
| // Error already handled via auth state flow in signInAnonymously() | ||
| // No additional action needed - ErrorRecoveryDialog will show automatically | ||
| } | ||
| } | ||
| } | ||
| } | ||
| } | ||
|
|
||
| /** | ||
| * Signs in a user anonymously with Firebase Authentication. | ||
| * | ||
| * This method creates a temporary anonymous user account that can be used for testing | ||
| * or as a starting point for users who want to try the app before creating a permanent | ||
| * account. Anonymous users can later be upgraded to permanent accounts by linking | ||
| * credentials (email/password, social providers, phone, etc.). | ||
| * | ||
| * **Flow:** | ||
| * 1. Updates auth state to loading with "Signing in anonymously..." message | ||
| * 2. Calls Firebase Auth's `signInAnonymously()` method | ||
| * 3. Updates auth state to idle on success | ||
| * 4. Handles cancellation and converts exceptions to [AuthException] types | ||
| * | ||
| * **Anonymous Account Benefits:** | ||
| * - No user data collection required | ||
| * - Immediate access to app features | ||
| * - Can be upgraded to permanent account later | ||
| * - Useful for guest users and app trials | ||
| * | ||
| * **Account Upgrade:** | ||
| * Anonymous accounts can be upgraded to permanent accounts by calling methods like: | ||
| * - [signInAndLinkWithCredential] with email/password or social credentials | ||
| * - [createOrLinkUserWithEmailAndPassword] for email/password accounts | ||
| * - [signInWithPhoneAuthCredential] for phone authentication | ||
| * | ||
| * **Example: Basic anonymous sign-in** | ||
| * ```kotlin | ||
| * try { | ||
| * firebaseAuthUI.signInAnonymously() | ||
| * // User is now signed in anonymously | ||
| * // Show app content or prompt for account creation | ||
| * } catch (e: AuthException.AuthCancelledException) { | ||
| * // User cancelled the sign-in process | ||
| * } catch (e: AuthException.NetworkException) { | ||
| * // Network error occurred | ||
| * } | ||
| * ``` | ||
| * | ||
| * **Example: Anonymous sign-in with upgrade flow** | ||
| * ```kotlin | ||
| * // Step 1: Sign in anonymously | ||
| * firebaseAuthUI.signInAnonymously() | ||
| * | ||
| * // Step 2: Later, upgrade to permanent account | ||
| * try { | ||
| * firebaseAuthUI.createOrLinkUserWithEmailAndPassword( | ||
| * context = context, | ||
| * config = authUIConfig, | ||
| * provider = emailProvider, | ||
| * name = "John Doe", | ||
| * email = "[email protected]", | ||
| * password = "SecurePass123!" | ||
| * ) | ||
| * // Anonymous account upgraded to permanent email/password account | ||
| * } catch (e: AuthException.AccountLinkingRequiredException) { | ||
| * // Email already exists - show account linking UI | ||
| * } | ||
| * ``` | ||
| * | ||
| * @throws AuthException.AuthCancelledException if the coroutine is cancelled | ||
| * @throws AuthException.NetworkException if a network error occurs | ||
| * @throws AuthException.UnknownException for other authentication errors | ||
| * | ||
| * @see signInAndLinkWithCredential for upgrading anonymous accounts | ||
| * @see createOrLinkUserWithEmailAndPassword for email/password upgrade | ||
| * @see signInWithPhoneAuthCredential for phone authentication upgrade | ||
| */ | ||
| internal suspend fun FirebaseAuthUI.signInAnonymously() { | ||
| try { | ||
| updateAuthState(AuthState.Loading("Signing in anonymously...")) | ||
| auth.signInAnonymously().await() | ||
| updateAuthState(AuthState.Idle) | ||
| } catch (e: CancellationException) { | ||
| val cancelledException = AuthException.AuthCancelledException( | ||
| message = "Sign in anonymously was cancelled", | ||
| cause = e | ||
| ) | ||
| updateAuthState(AuthState.Error(cancelledException)) | ||
| throw cancelledException | ||
| } catch (e: AuthException) { | ||
| updateAuthState(AuthState.Error(e)) | ||
| throw e | ||
| } catch (e: Exception) { | ||
| val authException = AuthException.from(e) | ||
| updateAuthState(AuthState.Error(authException)) | ||
| throw authException | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -285,7 +285,8 @@ abstract class AuthProvider(open val providerId: String, open val name: String) | |
| * An interface to wrap the static `EmailAuthProvider.getCredential` method to make it testable. | ||
| * @suppress | ||
| */ | ||
| internal interface CredentialProvider { | ||
| // TODO(demolaf): make this internal | ||
|
||
| interface CredentialProvider { | ||
| fun getCredential(email: String, password: String): AuthCredential | ||
| } | ||
|
|
||
|
|
@@ -635,8 +636,7 @@ abstract class AuthProvider(open val providerId: String, open val name: String) | |
| * An interface to wrap the static `FacebookAuthProvider.getCredential` method to make it testable. | ||
| * @suppress | ||
| */ | ||
| @VisibleForTesting(otherwise = VisibleForTesting.PROTECTED) | ||
| interface CredentialProvider { | ||
| internal interface CredentialProvider { | ||
| fun getCredential(token: String): AuthCredential | ||
| } | ||
|
|
||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Should we rethrow the error? Or remove the try catch?