-
Notifications
You must be signed in to change notification settings - Fork 1.9k
feat: Core: Auth State & User Accessors #2217
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 all commits
Commits
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
222 changes: 222 additions & 0 deletions
222
auth/src/main/java/com/firebase/ui/auth/compose/AuthState.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,222 @@ | ||
/* | ||
* Copyright 2025 Google Inc. All Rights Reserved. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except | ||
* in compliance with the License. You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software distributed under the | ||
* License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either | ||
* express or implied. See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package com.firebase.ui.auth.compose | ||
|
||
import com.google.firebase.auth.AuthResult | ||
import com.google.firebase.auth.FirebaseUser | ||
import com.google.firebase.auth.MultiFactorResolver | ||
|
||
/** | ||
* Represents the authentication state in Firebase Auth UI. | ||
* | ||
* This class encapsulates all possible authentication states that can occur during | ||
* the authentication flow, including success, error, and intermediate states. | ||
* | ||
* Use the companion object factory methods or specific subclass constructors to create instances. | ||
* | ||
* @since 10.0.0 | ||
*/ | ||
abstract class AuthState private constructor() { | ||
|
||
/** | ||
* Initial state before any authentication operation has been started. | ||
*/ | ||
class Idle internal constructor() : AuthState() { | ||
override fun equals(other: Any?): Boolean = other is Idle | ||
override fun hashCode(): Int = javaClass.hashCode() | ||
override fun toString(): String = "AuthState.Idle" | ||
} | ||
|
||
/** | ||
* Authentication operation is in progress. | ||
* | ||
* @property message Optional message describing what is being loaded | ||
*/ | ||
class Loading(val message: String? = null) : AuthState() { | ||
override fun equals(other: Any?): Boolean { | ||
if (this === other) return true | ||
if (other !is Loading) return false | ||
return message == other.message | ||
} | ||
|
||
override fun hashCode(): Int = message?.hashCode() ?: 0 | ||
|
||
override fun toString(): String = "AuthState.Loading(message=$message)" | ||
} | ||
|
||
/** | ||
* Authentication completed successfully. | ||
* | ||
* @property result The [AuthResult] containing the authenticated user, may be null if not available | ||
* @property user The authenticated [FirebaseUser] | ||
* @property isNewUser Whether this is a newly created user account | ||
*/ | ||
class Success( | ||
val result: AuthResult?, | ||
val user: FirebaseUser, | ||
val isNewUser: Boolean = false | ||
) : AuthState() { | ||
override fun equals(other: Any?): Boolean { | ||
if (this === other) return true | ||
if (other !is Success) return false | ||
return result == other.result && | ||
user == other.user && | ||
isNewUser == other.isNewUser | ||
} | ||
|
||
override fun hashCode(): Int { | ||
var result1 = result?.hashCode() ?: 0 | ||
result1 = 31 * result1 + user.hashCode() | ||
result1 = 31 * result1 + isNewUser.hashCode() | ||
return result1 | ||
} | ||
|
||
override fun toString(): String = | ||
"AuthState.Success(result=$result, user=$user, isNewUser=$isNewUser)" | ||
} | ||
|
||
/** | ||
* An error occurred during authentication. | ||
* | ||
* @property exception The [Exception] that occurred | ||
* @property isRecoverable Whether the error can be recovered from | ||
*/ | ||
class Error( | ||
val exception: Exception, | ||
val isRecoverable: Boolean = true | ||
) : AuthState() { | ||
override fun equals(other: Any?): Boolean { | ||
if (this === other) return true | ||
if (other !is Error) return false | ||
return exception == other.exception && | ||
isRecoverable == other.isRecoverable | ||
} | ||
|
||
override fun hashCode(): Int { | ||
var result = exception.hashCode() | ||
result = 31 * result + isRecoverable.hashCode() | ||
return result | ||
} | ||
|
||
override fun toString(): String = | ||
"AuthState.Error(exception=$exception, isRecoverable=$isRecoverable)" | ||
} | ||
|
||
/** | ||
* Authentication was cancelled by the user. | ||
*/ | ||
class Cancelled internal constructor() : AuthState() { | ||
override fun equals(other: Any?): Boolean = other is Cancelled | ||
override fun hashCode(): Int = javaClass.hashCode() | ||
override fun toString(): String = "AuthState.Cancelled" | ||
} | ||
|
||
/** | ||
* Multi-factor authentication is required to complete sign-in. | ||
* | ||
* @property resolver The [MultiFactorResolver] to complete MFA | ||
* @property hint Optional hint about which factor to use | ||
*/ | ||
class RequiresMfa( | ||
val resolver: MultiFactorResolver, | ||
val hint: String? = null | ||
) : AuthState() { | ||
override fun equals(other: Any?): Boolean { | ||
if (this === other) return true | ||
if (other !is RequiresMfa) return false | ||
return resolver == other.resolver && | ||
hint == other.hint | ||
} | ||
|
||
override fun hashCode(): Int { | ||
var result = resolver.hashCode() | ||
result = 31 * result + (hint?.hashCode() ?: 0) | ||
return result | ||
} | ||
|
||
override fun toString(): String = | ||
"AuthState.RequiresMfa(resolver=$resolver, hint=$hint)" | ||
} | ||
|
||
/** | ||
* Email verification is required before the user can access the app. | ||
* | ||
* @property user The [FirebaseUser] who needs to verify their email | ||
* @property email The email address that needs verification | ||
*/ | ||
class RequiresEmailVerification( | ||
val user: FirebaseUser, | ||
val email: String | ||
) : AuthState() { | ||
override fun equals(other: Any?): Boolean { | ||
if (this === other) return true | ||
if (other !is RequiresEmailVerification) return false | ||
return user == other.user && | ||
email == other.email | ||
} | ||
|
||
override fun hashCode(): Int { | ||
var result = user.hashCode() | ||
result = 31 * result + email.hashCode() | ||
return result | ||
} | ||
|
||
override fun toString(): String = | ||
"AuthState.RequiresEmailVerification(user=$user, email=$email)" | ||
} | ||
|
||
/** | ||
* The user needs to complete their profile information. | ||
* | ||
* @property user The [FirebaseUser] who needs to complete their profile | ||
* @property missingFields List of profile fields that need to be completed | ||
*/ | ||
class RequiresProfileCompletion( | ||
val user: FirebaseUser, | ||
val missingFields: List<String> = emptyList() | ||
) : AuthState() { | ||
override fun equals(other: Any?): Boolean { | ||
if (this === other) return true | ||
if (other !is RequiresProfileCompletion) return false | ||
return user == other.user && | ||
missingFields == other.missingFields | ||
} | ||
|
||
override fun hashCode(): Int { | ||
var result = user.hashCode() | ||
result = 31 * result + missingFields.hashCode() | ||
return result | ||
} | ||
|
||
override fun toString(): String = | ||
"AuthState.RequiresProfileCompletion(user=$user, missingFields=$missingFields)" | ||
} | ||
|
||
companion object { | ||
/** | ||
* Creates an Idle state instance. | ||
* @return A new [Idle] state | ||
*/ | ||
@JvmStatic | ||
val Idle: Idle = Idle() | ||
|
||
/** | ||
* Creates a Cancelled state instance. | ||
* @return A new [Cancelled] state | ||
*/ | ||
@JvmStatic | ||
val Cancelled: Cancelled = Cancelled() | ||
} | ||
} |
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.
Uh oh!
There was an error while loading. Please reload this page.
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.
With the potential of having to handle more states, would it be better to use when instead?
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.
Hum, we are determining states here depending on conditions, not sure it's required.