3

I'm trying to get the code from GitHub Oauth API playground://gh?code=XYZ in the ViewModel through savedStateHandler. My composable screen configuration:

NavGraphBuilder.composable(
         route = "login",
         deepLinks = listOf(navDeepLink {
            uriPattern = "playground://gh?code={code}"
         })
      ) {
         Log.d("MyLog", it.arguments?.getString("code", "nothing") ?: "null")
         //...
      }

MyLog is always nothing

I also tried to implement the NavGraphBuilder.composable.arguments parameter, but the result was the same.

I'm just getting familiar with Compose and went through the Google Code labs. https://developer.android.com/codelabs/jetpack-compose-navigation#4 - this worked as expected, but the issue is the argument is required.

I also have a WebViewScreen composable that needs a URL and that works.

builder.composable(
         route = "browser",
         arguments = listOf(
            navArgument("url") {
               type = NavType.StringType
            }
         ),
         deepLinks = listOf(
            navDeepLink {
               uriPattern = "playground://gh/browser/{url}"
            }
         )
      ) {
         //...
      }

Not sure if this can be achieved with Jetpack Compose Navigation or do I have to use the Intent approach a pass it to the ViewModel?

peter.o
  • 3,460
  • 7
  • 51
  • 77

1 Answers1

3

I'm in the same situation as you, getting familiar with Jetpack Compose and building an app that uses OAuth authentication.

I've managed to get the authorisation code from the deeplink uri after going through the same codelab that you mentioned.

Try adding this code to your MainActivity to ensure that your callback uri contains the auth code:

    override fun onResume() {
        super.onResume()
        val data: Uri? = intent?.data
        val code = data?.getQueryParameter("code")
        Log.d("MainActivity", "oauth code - $code")
    }

It works for me and I can see the code in the log when the authentication is redirected to my app.

I'm using a different method though, I'm using an Implicit Intent to redirect the user to an endpoint for authentication and I'm not using the Github API.

EDIT:

Here is the composable parameters:

    composable(
        route = "OAuthRedirectWithCode",
        arguments = listOf(navArgument("code") { type = NavType.StringType }),
        deepLinks = listOf(navDeepLink { uriPattern = "$uri?code={code}" })
    ) { backStackEntry ->
        val code = backStackEntry.arguments?.getString("code")
        Log.d("NavHost", "oauth code - $code")
      }


Hope it helps!

cgontijo
  • 286
  • 2
  • 7
  • Hi. Yes, I'm using the implicit intent as well to open the GitHub login page. After login, the deep link opens my app but the code is empty/null. – peter.o Jun 03 '22 at 13:52
  • I can see the code in the `onResume`. Would you mind sharing the `NavBuilder` composable parameters, please? – peter.o Jun 03 '22 at 14:00
  • I got it. I need to copy the exact URI that comes back to the `uriPattern`. I thought I could just read everything. – peter.o Jun 09 '22 at 00:41