Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Issue with Klarna signin integration in my expo51 via native module #281

Open
mikeAdutskevich opened this issue Dec 12, 2024 · 0 comments
Open

Comments

@mikeAdutskevich
Copy link

mikeAdutskevich commented Dec 12, 2024

I'm having issues with Klarna signin integration in my expo(v51.0.0) project.
I use expo managed workflow with dev-builds.

The Klarnas' documentation:
https://docs.klarna.com/conversion-boosters/sign-in-with-klarna/integrate-sign-in-with-klarna/mobile-integration/

What I'm trying to do is to create a native module which will handle signin process.
I want to pass props to this native module and handle the response/errors of the signing proces. The main goal is to get idToken from the response.

I created a native module via npx create-expo-module klarna-auth. It creates a template for native module and example folder where I can test it with all predefined settings. The template works well.

Then I go to Android klarna integration and updated AndroidManifest.xml with:

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
  xmlns:tools="http://schemas.android.com/tools">
  <application>
    <activity android:name="com.klarna.mobile.sdk.activity.KlarnaRedirectReceiverActivity" android:exported="true" tools:node="replace">
      <intent-filter>
        <action android:name="android.intent.action.VIEW"/>
        <category android:name="android.intent.category.DEFAULT"/>
        <category android:name="android.intent.category.BROWSABLE"/>
        <data android:scheme="citygross" android:host="example"/>
      </intent-filter>
    </activity>
  </application>
</manifest>

This not causing errors at least now.

Then I defined an Event Handler. I took this code from the Klarna signIn docs as well:

package expo.modules.klarna

import expo.modules.kotlin.modules.Module
import expo.modules.kotlin.modules.ModuleDefinition

class KlarnaAuthModule : Module() {
  override fun definition() = ModuleDefinition {
    Name("KlarnaAuth")

    Function("getTheme") {
      return@Function "system"
    }
  }

  private val eventHandler = object : KlarnaEventHandler {
    override fun onEvent(klarnaComponent: KlarnaComponent, event: KlarnaProductEvent) {
      when(event.action) {
        KlarnaSignInEvent.USER_TAPPED_BUTTON -> {
          // User tapped the KlarnaSignInButton, auth process starting
        }
        KlarnaSignInEvent.USER_AUTH -> {
          // User completed interactive auth, tokens will be fetched
        }
        KlarnaSignInEvent.USER_CANCELLED -> {
          // User manually canceled sign in
        }
        KlarnaSignInEvent.SIGN_IN_TOKEN -> {
          // User is authorized. You can read the results      
          // in event.params attribute by casting it to the
          // KlarnaSignInToken model.

          val token = event.params[KlarnaSignInEvent.ParamKey.KlarnaSignInToken] as? KlarnaSignInToken
          val accessToken = token?.accessToken
          // Handle the access token
        }
      }
    }

    override fun onError(klarnaComponent: KlarnaComponent, error: KlarnaMobileSDKError) {
      // In case of any errors, check the 'error' parameter for more details, 
      // for example if the error is fatal or not.

      val errorMessage = error.message
      val isFatal = error.isFatal
      when(error.name) {         
        KlarnaSignInError.InvalidClientID -> {
          // The client ID value is invalid
        }
        KlarnaSignInError.InvalidScope -> {
          // The scope value is invalid
        }
        KlarnaSignInError.InvalidMarket -> {
          // The market value is invalid
        }
        KlarnaSignInError.InvalidCustomTabsReturnUrl -> {
          // The AndroidManifest needs to be set up for KlarnaCustomTabActivity
        }
        KlarnaSignInError.SignInFailed -> {
          // User authorization step failed
          val signInError = error.params[KlarnaSignInError.ParamKey.Error]
          val signInErrorDescription = error.params[KlarnaSignInError.ParamKey.ErrorDescription]
        }
        KlarnaSignInError.RenderButtonFailed -> {
          // Button failed to render
        }
        KlarnaSignInError.AlreadyInProgress -> {
          // Sign in is already in progress, user tap or signIn method call is ignored
        }
      }
    }
  }
}

Then I ran npm run build in native module folder('klarna-auth').
And then ran npx expo run:android in example folder to test the module.

And got the error:

e: file:///Users/mike/Documents/Projects/nativeModules/klarna-auth/android/src/main/java/expo/modules/klarna/KlarnaAuthModule.kt:23:31 Unresolved reference: KlarnaEventHandler
e: file:///Users/mike/Documents/Projects/nativeModules/klarna-auth/android/src/main/java/expo/modules/klarna/KlarnaAuthModule.kt:24:5 'onEvent' overrides nothing
e: file:///Users/mike/Documents/Projects/nativeModules/klarna-auth/android/src/main/java/expo/modules/klarna/KlarnaAuthModule.kt:24:43 Unresolved reference: KlarnaComponent
e: file:///Users/mike/Documents/Projects/nativeModules/klarna-auth/android/src/main/java/expo/modules/klarna/KlarnaAuthModule.kt:24:67 Unresolved reference: KlarnaProductEvent
e: file:///Users/mike/Documents/Projects/nativeModules/klarna-auth/android/src/main/java/expo/modules/klarna/KlarnaAuthModule.kt:26:9 Unresolved reference: KlarnaSignInEvent
e: file:///Users/mike/Documents/Projects/nativeModules/klarna-auth/android/src/main/java/expo/modules/klarna/KlarnaAuthModule.kt:29:9 Unresolved reference: KlarnaSignInEvent
e: file:///Users/mike/Documents/Projects/nativeModules/klarna-auth/android/src/main/java/expo/modules/klarna/KlarnaAuthModule.kt:32:9 Unresolved reference: KlarnaSignInEvent
e: file:///Users/mike/Documents/Projects/nativeModules/klarna-auth/android/src/main/java/expo/modules/klarna/KlarnaAuthModule.kt:35:9 Unresolved reference: KlarnaSignInEvent
e: file:///Users/mike/Documents/Projects/nativeModules/klarna-auth/android/src/main/java/expo/modules/klarna/KlarnaAuthModule.kt:40:36 Unresolved reference: KlarnaSignInEvent
e: file:///Users/mike/Documents/Projects/nativeModules/klarna-auth/android/src/main/java/expo/modules/klarna/KlarnaAuthModule.kt:40:86 Unresolved reference: KlarnaSignInToken

Looks like i need link/install Klarna signin sdk, but I don't understand how to do it. Need your help.

@mikeAdutskevich mikeAdutskevich changed the title Issue with Klarna signin integration in my expo Issue with Klarna signin integration in my expo51 Dec 12, 2024
@mikeAdutskevich mikeAdutskevich changed the title Issue with Klarna signin integration in my expo51 Issue with Klarna signin integration in my expo51 via native module Dec 18, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant