| 1 | import android.content.Intent |
| 2 | import android.net.Uri |
| 3 | import android.os.Bundle |
| 4 | import android.util.Log |
| 5 | import androidx.activity.enableEdgeToEdge |
| 6 | import androidx.activity.result.ActivityResultLauncher |
| 7 | import androidx.annotation.OptIn |
| 8 | import androidx.appcompat.app.AppCompatActivity |
| 9 | import androidx.browser.auth.AuthTabIntent |
| 10 | import androidx.browser.auth.ExperimentalAuthTab |
| 11 | |
| 12 | // MARK: - Configuration Constants |
| 13 | |
| 14 | const val CONST_BASE_URL = "https://example.com" // Set your backend authentication URL here |
| 15 | const val CONST_SCHEME = "mgpandroid://auth" // Your custom URL scheme |
| 16 | |
| 17 | class MainActivity : AppCompatActivity() { |
| 18 | private lateinit var authTabLauncher: ActivityResultLauncher<Intent> |
| 19 | |
| 20 | @OptIn(ExperimentalAuthTab::class) |
| 21 | override fun onCreate(savedInstanceState: Bundle?) { |
| 22 | super.onCreate(savedInstanceState) |
| 23 | enableEdgeToEdge() |
| 24 | setContentView(R.layout.activity_main) |
| 25 | |
| 26 | // Register the Auth Tab launcher |
| 27 | authTabLauncher = AuthTabIntent.registerActivityResultLauncher(this, this::handleAuthResult) |
| 28 | } |
| 29 | |
| 30 | override fun onNewIntent(intent: Intent) { |
| 31 | super.onNewIntent(intent) |
| 32 | // Handle intent when app is opened from CustomTabs via our custom scheme |
| 33 | intent.data?.let { uri -> |
| 34 | handleIncomingURL(uri) |
| 35 | } |
| 36 | } |
| 37 | |
| 38 | /** |
| 39 | * Starts the web authentication session |
| 40 | */ |
| 41 | @OptIn(ExperimentalAuthTab::class) |
| 42 | fun startAuthentication() { |
| 43 | val strUrl = CONST_BASE_URL + "&returnUrl=" + CONST_SCHEME |
| 44 | val authUrl = Uri.parse(strUrl) |
| 45 | |
| 46 | // Create and launch AuthTab directly |
| 47 | val authTabIntent = AuthTabIntent.Builder().build() |
| 48 | authTabIntent.launch(authTabLauncher, authUrl, "mgpandroid") |
| 49 | } |
| 50 | |
| 51 | /** |
| 52 | * Handle the authentication result from AuthTab |
| 53 | */ |
| 54 | @OptIn(ExperimentalAuthTab::class) |
| 55 | private fun handleAuthResult(result: AuthTabIntent.AuthResult) { |
| 56 | when (result.resultCode) { |
| 57 | AuthTabIntent.RESULT_OK -> { |
| 58 | // Process the result URI |
| 59 | val callbackURL = result.resultUri |
| 60 | |
| 61 | // Handle the callback URL received from the authentication flow |
| 62 | handleIncomingURL(callbackURL) |
| 63 | } |
| 64 | AuthTabIntent.RESULT_CANCELED -> { |
| 65 | Log.d("Authentication", "Authentication was canceled") |
| 66 | } |
| 67 | AuthTabIntent.RESULT_VERIFICATION_FAILED -> { |
| 68 | Log.e("Authentication", "Verification failed") |
| 69 | } |
| 70 | AuthTabIntent.RESULT_VERIFICATION_TIMED_OUT -> { |
| 71 | Log.e("Authentication", "Verification timed out") |
| 72 | } |
| 73 | } |
| 74 | } |
| 75 | |
| 76 | /** |
| 77 | * Handles the URL returned from the authentication flow |
| 78 | * This handles URLs from both the AuthTab result and from CustomTabs fallback |
| 79 | */ |
| 80 | private fun handleIncomingURL(url: Uri?) { |
| 81 | if (url == null) { |
| 82 | Log.e("Authentication", "No callback URL received") |
| 83 | return |
| 84 | } |
| 85 | |
| 86 | Log.d("Authentication", "Incoming URL: $url") |
| 87 | |
| 88 | // Ensure the URL uses the expected custom scheme |
| 89 | val scheme = url.scheme |
| 90 | if (scheme != "mgpandroid") { |
| 91 | Log.e("Authentication", "Invalid URL scheme") |
| 92 | return |
| 93 | } |
| 94 | |
| 95 | // Extract query parameter |
| 96 | val controlStatus = url.getQueryParameter("controlStatus") ?: "" |
| 97 | |
| 98 | // Check if authentication was successful |
| 99 | if (controlStatus == "VALIDATED") { |
| 100 | Log.d("Authentication", "Action succeeded") |
| 101 | // Handle successful authentication |
| 102 | } else { |
| 103 | // Handle authentication failure or unexpected status |
| 104 | Log.e("Authentication", "Action Failed: controlStatus=$controlStatus") |
| 105 | } |
| 106 | } |
| 107 | } |