Created
August 4, 2022 18:05
-
-
Save webserveis/d6a0b00dbda75a27f16d8cc699820f52 to your computer and use it in GitHub Desktop.
FirebaseUI para Android usando Kotlin
This file contains 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
class MainActivity : AppCompatActivity() { | |
private lateinit var appBarConfiguration: AppBarConfiguration | |
private lateinit var binding: ActivityMainBinding | |
private val mViewModel by viewModels<AuthViewModel> { | |
AuthViewModelFactory(this.application) | |
} | |
private val auth by lazy { | |
FirebaseAuth.getInstance() | |
} | |
private val signInLauncher = registerForActivityResult( | |
FirebaseAuthUIActivityResultContract() | |
) { res -> | |
this.onSignInResult(res) | |
} | |
private 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 { | |
arrayListOf( | |
AuthUI.IdpConfig.GoogleBuilder().build() | |
) | |
} | |
return AuthUI.getInstance() | |
.createSignInIntentBuilder() | |
.setAvailableProviders(providers) | |
.enableAnonymousUsersAutoUpgrade() | |
.build() | |
} | |
override fun onCreate(savedInstanceState: Bundle?) { | |
super.onCreate(savedInstanceState) | |
binding = ActivityMainBinding.inflate(layoutInflater) | |
setContentView(binding.root) | |
setSupportActionBar(binding.toolbar) | |
val navController = findNavController(R.id.nav_host_fragment_content_main) | |
appBarConfiguration = AppBarConfiguration(navController.graph) | |
setupActionBarWithNavController(navController, appBarConfiguration) | |
binding.fab.setOnClickListener { view -> | |
Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG) | |
.setAction("Action", null).show() | |
launchSignIn() | |
} | |
getCurrentUser() | |
} | |
override fun onCreateOptionsMenu(menu: Menu): Boolean { | |
menuInflater.inflate(R.menu.menu_main, menu) | |
return true | |
} | |
override fun onOptionsItemSelected(item: MenuItem): Boolean { | |
return when (item.itemId) { | |
R.id.action_settings -> { | |
authSignOut() | |
true | |
} | |
else -> super.onOptionsItemSelected(item) | |
} | |
} | |
override fun onSupportNavigateUp(): Boolean { | |
val navController = findNavController(R.id.nav_host_fragment_content_main) | |
return navController.navigateUp(appBarConfiguration) | |
|| super.onSupportNavigateUp() | |
} | |
private fun isAuthClient(): Boolean { | |
return auth.currentUser != null | |
} | |
private fun launchSignIn() { | |
signInLauncher.launch(getAuthIntent()) | |
} | |
private fun authSignOut() { | |
AuthUI.getInstance() | |
.signOut(this) | |
.addOnCompleteListener { | |
Toast.makeText(this, "Sign out completed", Toast.LENGTH_SHORT).show() | |
} | |
} | |
private fun authDelete() { | |
AuthUI.getInstance() | |
.delete(this) | |
.addOnCompleteListener { | |
Toast.makeText(this, "Deleted user auth completed", Toast.LENGTH_SHORT).show() | |
} | |
} | |
private fun getCurrentUser() { | |
if (isAuthClient()) { | |
val user = auth.currentUser | |
val userEmail = user?.email | |
val userDisplayName = user?.displayName | |
val userUID = user?.uid | |
val userPhoto = user?.photoUrl | |
Toast.makeText(this, "Welcome $userDisplayName", Toast.LENGTH_SHORT).show() | |
} else { | |
Toast.makeText(this, "Need Sign In", Toast.LENGTH_SHORT).show() | |
} | |
} | |
private fun onSignInResult(result: FirebaseAuthUIAuthenticationResult) { | |
val response = result.idpResponse | |
if (result.resultCode == RESULT_OK) { | |
// Successfully signed in | |
val user = FirebaseAuth.getInstance().currentUser | |
Toast.makeText(this, "Auth with $user", Toast.LENGTH_SHORT).show() | |
} else { | |
// Sign in failed. If response is null the user canceled the | |
// sign-in flow using the back button. Otherwise check | |
// response.getError().getErrorCode() and handle the error. | |
// ... | |
Toast.makeText(this, "User canceled Auth Flow", Toast.LENGTH_SHORT).show() | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment