Search code examples
androidkotlinandroid-jetpack-composeauth0

Android Jetpack compose How to retrieve access token from within the OnSuccess using Auth0?


I have a client project where I need to retrieve the access token from the users credentials after they make an account. The problem I am having is that its just a test theres no repository or viewmodel. I need to take the access token directly from the login function and give it to the composable that is calling the api function. The api function that makes the network call works fine but it need the access token for authorization and idk how to pass it. Inside onSuccess i need to be able to save result.accessToken and use it in the api call. I wanted to use a datastore but it seems complicated for a simple test. If anyone has an idea please let me know. here is the login function.

 fun login(
        account: Auth0,
        ){
        
        WebAuthProvider.login(account)
            .withScheme("removed for privacy")
            .withScope("removed for privacy")
            //.withScope("removed for privacy")
            .withAudience("removed for privacy")        
            .start(context, object : Callback<Credentials, AuthenticationException> {
                override fun onFailure(error: AuthenticationException) {
                    Log.d("LoginOnFailure", "Failure: ${error.getCode()}")
                }
                override fun onSuccess(result: Credentials) {
                    cachedCredentials = result
                    Log.d("LoginOnSuccess", "Success: ${result.accessToken}")
                    
                 
                }
            })
    }

Solution

  • Modify your login function to take an additional parameter for the callback that will be called with the access token:

    fun login(
        account: Auth0,
        onSuccess: (accessToken: String) -> Unit, // Callback for success
        onFailure: () -> Unit // Callback for failure
    ) {
        WebAuthProvider.login(account)
            // ... (other configuration)
            .start(context, object : Callback<Credentials, AuthenticationException> {
                override fun onFailure(error: AuthenticationException) {
                    Log.d("LoginOnFailure", "Failure: ${error.getCode()}")
                    onFailure()
                }
    
                override fun onSuccess(result: Credentials) {
                    val accessToken = result.accessToken
                    Log.d("LoginOnSuccess", "Success: $accessToken")
                    onSuccess(accessToken)
                }
            })
    }
    

    In your Composable function, you can call the login function and pass it a lambda that will be executed on success. This lambda will receive the access token:

    @Composable
    fun YourApiComposable() {
        var accessToken: String? by remember { mutableStateOf(null) }
    
        // Make the login call and handle success and failure
        login(
            account = yourAuth0Account,
            onSuccess = { token ->
                accessToken = token
                // Now you have the access token, you can make your API call here.
                // Example: apiFunction(token)
            },
            onFailure = {
                // Handle the login failure, if needed.
            }
        )
    
        // Use accessToken as needed in your Composable.
    }