I am trying to fix one issue with my implementation. I am using firebase authentication with Play Games for my gaming app. Authentication with Play Games working fine no issue at all.
But there is one case, when I uninstall the game and delete the game data from Play Games app (Settings > Delete Play Games account & data > Delete my game data), After re-installing I get the error status code 4: SIGN_IN_REQUIRED and if I close the game and open again it working fine.
Why is this strange behaviour in implementation, for the first time it is not working but on the second launch it starts working.
here is my code:
class FirebasePlayGamesAuth {
private var auth: FirebaseAuth = FirebaseAuth.getInstance()
private lateinit var gamesSignInClient: GamesSignInClient
private lateinit var webClientId: String
private var nativeAuthComplete = CompletableFuture<FirebaseUser>()
//1
fun triggerLogin(activity: Activity): CompletableFuture<FirebaseUser> {
webClientId = activity.resources.getString(R.string.default_web_client_id)
gamesSignInClient = PlayGames.getGamesSignInClient(activity)
val currentUser = auth.currentUser
if (currentUser == null) {
checkPlayGamesAuthStatus()
} else {
nativeAuthComplete.complete(currentUser)
}
return nativeAuthComplete
}
//2
private fun checkPlayGamesAuthStatus() {
gamesSignInClient.isAuthenticated.addOnCompleteListener { isAuthenticatedTask: Task<AuthenticationResult> ->
val isAuthenticated =
isAuthenticatedTask.isSuccessful && isAuthenticatedTask.result.isAuthenticated
if (isAuthenticated) {
requestFirebaseCredentials()
} else {
nativeAuthComplete.completeExceptionally(FirebasePlayGameAutoLoginFailed())
}
}
}
//3
private fun requestFirebaseCredentials() {
gamesSignInClient.requestServerSideAccess(webClientId, false)
.addOnCompleteListener { task: Task<String?> ->
if (task.isSuccessful) {
task.result?.let { serverAuthToken ->
authenticateWithFirebaseCredential(serverAuthToken)
} ?: run {
nativeAuthComplete.completeExceptionally(FirebaseServerSideAccessFailed())
}
} else {
task.exception?.let { exception ->
println("credential error: ${exception.message}")
if (exception is ResolvableApiException) {
println("auth resolution statred")
exception.startResolutionForResult(myActivity, 1101)
} else {
nativeAuthComplete.completeExceptionally(exception)
}
} ?: run {
nativeAuthComplete.completeExceptionally(FirebaseServerSideAccessFailed())
}
}
}
}
//4
private fun authenticateWithFirebaseCredential(idToken: String) {
val credential = PlayGamesAuthProvider.getCredential(idToken)
auth.signInWithCredential(credential).addOnCompleteListener { task ->
if (task.isSuccessful) {
nativeAuthComplete.complete(auth.currentUser)
} else {
task.exception?.let { exception ->
nativeAuthComplete.completeExceptionally(exception)
} ?: run {
nativeAuthComplete.completeExceptionally(
FirebaseAuthenticationWithCredentialFailed()
)
}
}
}
}
}
And in MainAcitivity
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
PlayGamesSdk.initialize(this)
firebasePlayGamesAuth.triggerLogin(this)
}
any suggestion or idea would be greatly appreciated