Android FirebaseUI Auth: Upgrade From Anonymous Account to Permanent Account
October 14, 2019Assuming you are using firebase-ui-auth for handle signin flow, you can easy convert anonymous account to permanent account (new permanent account will share same uid as anonymouse account).
fun getAuthIntent(): Intent {
val currentUser = FirebaseAuth.getInstance().currentUser
val providers = if (currentUser == null) {
arrayListOf(
AuthUI.IdpConfig.GoogleBuilder().build(),
// AuthUI.IdpConfig.EmailBuilder().build(),
AuthUI.IdpConfig.AnonymousBuilder().build()
)
} else { // remove anonymous if already sign in
arrayListOf(
AuthUI.IdpConfig.GoogleBuilder().build()
)
}
val intent = AuthUI.getInstance()
.createSignInIntentBuilder()
.setAvailableProviders(providers)
// if current user is anonymous, the new signin will have the same uid
// if the signin is an existing user, a conflict will happen
.enableAnonymousUsersAutoUpgrade()
.build()
startActivityForResult(intent, REQUEST_SIGN_IN)
}
class TestFragment : Fragment() {
companion object {
private const val REQUEST_SIGN_IN = 1
}
override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
super.onActivityResult(requestCode, resultCode, data)
if (requestCode == REQUEST_SIGN_IN) {
val response = IdpResponse.fromResultIntent(data)
if (resultCode == RESULT_OK) {
val user = FirebaseAuth.getInstance().currentUser
if (user != null) {
Timber.d("name=${user.displayName}, email=${user.email}, uid=${user.uid}")
}
}
else {
response?.also { response ->
// when the signin credential is not a new user, while current user is anonymous
// as existing user cannot be linked to existing anonymous user
if (response.error?.errorCode == ErrorCodes.ANONYMOUS_UPGRADE_MERGE_CONFLICT) {
val currentAnonymousUser = FirebaseAuth.getInstance().currentUser
// TODO: save anonymous data
val newCredential = response.credentialForLinking
if (newCredential != null) {
FirebaseAuth.getInstance().signInWithCredential(newCredential)
.addOnSuccessListener {
val newUser = FirebaseAuth.getInstance().currentUser
// TODO: migrate/merge anonymous data to existing user
}
}
}
else {
Timber.e("REQUEST_SIGN_IN error", response.error)
}
}
}
}
}
- algo-trading
- algolia
- analytics
- android
- android-ktx
- android-permission
- android-studio
- apps-script
- bash
- binance
- bootstrap
- bootstrapvue
- chartjs
- chrome
- cloud-functions
- coding-interview
- contentresolver
- coroutines
- crashlytics
- crypto
- css
- dagger2
- datastore
- datetime
- docker
- eslint
- firebase
- firebase-auth
- firebase-hosting
- firestore
- firestore-security-rules
- flask
- fontawesome
- fresco
- git
- github
- glide
- godot
- google-app-engine
- google-cloud-storage
- google-colab
- google-drive
- google-maps
- google-places
- google-play
- google-sheets
- gradle
- html
- hugo
- inkscape
- java
- java-time
- javascript
- jetpack-compose
- jetson-nano
- kotlin
- kotlin-serialization
- layout
- lets-encrypt
- lifecycle
- linux
- logging
- lubuntu
- markdown
- mate
- material-design
- matplotlib
- md5
- mongodb
- moshi
- mplfinance
- mysql
- navigation
- nginx
- nodejs
- npm
- nuxtjs
- nvm
- pandas
- payment
- pip
- pwa
- pyenv
- python
- recylerview
- regex
- room
- rxjava
- scoped-storage
- selenium
- social-media
- ssh
- ssl
- static-site-generator
- static-website-hosting
- sublime-text
- ubuntu
- unit-test
- uwsgi
- viewmodel
- viewpager2
- virtualbox
- vue-chartjs
- vue-cli
- vue-router
- vuejs
- vuelidate
- vuepress
- web-development
- web-hosting
- webpack
- windows
- workmanager
- wsl
- yarn