I have intergrated microsoft authentication into my android app via firebase, I as well have the app registered on Microsoft Entra Id with the correct app id and redirect Uris registered on both ends. The authentication works fine on most devices however on my lg g6 running android 9 when i click login and a chrome tab is opened i have to click more option at the top and then select run in chrome for the authentication flow to work, however on another device a xiamo redmi note 9 pro running android 10 and MIUI 12 i completely cannot log in to the application and get an error message from firebase stating “The web operation was canceled by the user.”
Here is my login fragment code
class LoginFragment : Fragment(R.layout.fragment_login_2) {
private lateinit var binding: FragmentLogin2Binding
private lateinit var animationView: LottieAnimationView
private lateinit var dialog: AlertDialog
private val viewModel by viewModels<LoginViewModel>()
private val firebaseAuth by lazy {
FirebaseAuth.getInstance()
}
private val provider by lazy {
OAuthProvider.newBuilder("microsoft.com").apply {
//Force re-consent
addCustomParameter("prompt", "consent")
//Target specific email with login hint
addCustomParameter("tenant", "55cc3b83-0fa3-49c7-8c49-14b9ccdfd9ff")
}.build()
}
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)
// Handle the intent
handleIntent(requireActivity().intent)
binding = FragmentLogin2Binding.bind(view)
if (firebaseAuth.currentUser != null)
navigate()
binding.canUseBiometric = BiometricUtil.isBiometricSet(requireContext())
//re-enable status bar for login screen
requireActivity().window.decorView.systemUiVisibility = View.VISIBLE
initializeViews()
viewModel.isLoginSuccess.observe(viewLifecycleOwner, Observer { success ->
if (success) {
try {
dialog.dismiss()
navigate()
} catch (e: Exception) {
Timber.e(e.message)
}
} else {
Dialog.show(requireContext(), ErrorContent(viewModel.errorMessage.value.toString()))
}
})
binding.btnLogin.setOnClickListener {
val firebaseUser = firebaseAuth.currentUser
Timber.d("current user $firebaseUser")
if (!hasOnGoingRegistration())
appLogin()
}
}
private fun initializeViews() {
animationView = binding.loginAnimation
animationView.playAnimation()
}
private fun hasOnGoingRegistration(): Boolean {
val pending = firebaseAuth.pendingAuthResult ?: return false
pending.addOnSuccessListener {
navigate()
}.addOnFailureListener {
it.message?.let { it1 -> ErrorContent(it1) }
?.let { it2 -> Dialog.show(requireContext(), it2) }
}
return true
}
private fun appLogin() {
Dialog.show(requireContext(), LoadingContent(""))
firebaseAuth
.startActivityForSignInWithProvider(requireActivity(), provider)
.addOnSuccessListener {
//User is signed in
navigate()
Timber.d("User has successfully been authenticated")
}
.addOnFailureListener {
it.printStackTrace()
//Handle failure an error was encountered
Timber.d("error authenticating ${it.cause}")
it.message?.let { it1 -> ErrorContent(it1) }
?.let { it2 -> Dialog.show(requireContext(), it2) }
}
}
private fun handleIntent(intent: Intent?) {
intent?.data?.let { uri ->
// Extract data from the redirect URI
val code = uri.getQueryParameter("code")
val error = uri.getQueryParameter("error")
// Handle the authentication response
if (code != null) {
// Authorization code received, complete the authentication process
// You can exchange the authorization code for an access token here
Toast.makeText(requireContext(), "Success", Toast.LENGTH_LONG).show()
} else if (error != null) {
// An error occurred during the authentication process
// Handle the error appropriately
Toast.makeText(requireContext(), "Error", Toast.LENGTH_LONG).show()
}
}
}
private fun navigate() {
val intent = Intent(context, HomeActivity::class.java)
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP or Intent.FLAG_ACTIVITY_SINGLE_TOP)
startActivity(intent)
}
override fun onResume() {
super.onResume()
hasOnGoingRegistration()
}
}```
Could anyone offer any insight or suggestions into how i can resolve this?