Configuration

Before you call any PowerAuth method, you need to configure it first. Unconfigured instance will throw exceptions. Use await PowerAuth.isConfigured() to check if configured.

1. Parameters

The configure method will need the following parameters:

  • instanceId - Identifier of the app - the aplication package name/identifier is recommended.
  • appKey - APPLICATION_KEY as defined in PowerAuth specification - a key identifying an application version.
  • appSecret - APPLICATION_SECRET as defined in PowerAuth specification - a secret associated with an application version.
  • masterServerPublicKey - KEY_SERVER_MASTER_PUBLIC as defined in PowerAuth specification - a master server public key.
  • baseEndpointUrl - Base URL to the PowerAuth Standard RESTful API (the URL part before “/pa/…”).
  • enableUnsecureTraffic - If HTTP or invalid HTTPS communication should be enabled (do not set true in production).

2. Configuration

Configuration from JavaScript

You can configure the PowerAuth singleton directly in JavaScript. Simply import the module and use the following snippet.

import PowerAuth from 'react-native-powerauth-mobile-sdk';

async setupPowerAuth() {
    // powerauth instance can be configured only once
    const isConfigured = await PowerAuth.isConfigured();
    if (isConfigured) {
        console.log("PowerAuth was already configured.");
    } else {
        try {
            await PowerAuth.configure("your-app-activation", "APPLICATION_KEY", "APPLICATION_SECRET", "KEY_SERVER_MASTER_PUBLIC", "https://your-powerauth-endpoint.com/", false);
            console.log("PowerAuth configuration successfull.");
        } catch(e) {
            console.log(`PowerAuth failed to configure: ${e.code}`);
        }
    }
}

Configuration from Native Code

In some cases (for example when you don’t want to leave the configuration info in your .js files or when you need more advanced configuration) you might want to configure the PowerAuth directly from the platform native code.

Android (JAVA):

The following code is an example based on MainApplication.java file that is generated by the React Native and can be found inside the YOUR_APP/android/app/src/main/java/YOUR/PACKAGE folder.
Your implementation might differ.

import com.wultra.android.powerauth.reactnative.PowerAuthRNPackage;
import io.getlime.security.powerauth.networking.ssl.PA2ClientSslNoValidationStrategy;
import io.getlime.security.powerauth.sdk.PowerAuthClientConfiguration;
import io.getlime.security.powerauth.sdk.PowerAuthConfiguration;
import io.getlime.security.powerauth.sdk.PowerAuthSDK;

public class MainApplication extends Application implements ReactApplication {
		
  @Override
  public void onCreate() {
    super.onCreate();
    initializePowerAuth();
  }
  	
  private void initializePowerAuth() {
    for (ReactPackage pkg : this.getReactNativeHost().getReactInstanceManager().getPackages()) {
      if (pkg instanceof PowerAuthRNPackage) {
        try {
          PowerAuthSDK.Builder builder = new PowerAuthSDK.Builder(
                 new PowerAuthConfiguration.Builder("your-app-activation", "https://your-powerauth-endpoint.com/", "APPLICATION_KEY", "APPLICATION_SECRET", "KEY_SERVER_MPK").build()
         );
         ((PowerAuthRNPackage) pkg).configure(builder);
        } catch (Exception e) {
          e.printStackTrace();
        }
      }
    }
  }
}

For more information about the native configuration, you can visit official documentation of the native SDK.

iOS (OBJECTIVE-C):

The following code is an example based on AppDelegate.m file that is generated by the React Native and can be found inside the YOUR_APP/ios/PROJECT_NAME folder.
Your implementation might differ.

#import "AppDelegate.h"
#import <PowerAuth.h>
#import <React/RCTRootView.h>

@interface AppDelegate ()
@property (nonatomic, strong) NSDictionary *launchOptions;
@end

@implementation AppDelegate

- (RCTBridge *)initializeReactNativeApp
{
  RCTBridge *bridge = [[RCTBridge alloc] initWithDelegate:self launchOptions:self.launchOptions];
  
  // POWERAUTH CONFIGURATION
  PowerAuth *pa = [bridge moduleForClass:PowerAuth.class];
  if (pa) {
    PowerAuthConfiguration *config = [[PowerAuthConfiguration alloc] init];
    config.instanceId = @"your-instance-id";
    config.appKey = @"APPLICATION_KEY";
    config.appSecret = @"APPLICATION_SECRET";
    config.masterServerPublicKey = @"KEY_SERVER_MASTER_PUBLIC";
    config.baseEndpointUrl = @"https://your-powerauth-endpoint.com/";
    if(![pa configureWithConfig:config keychainConfig:nil clientConfig:nil]) {
      NSLog(@"Failed to configure PowerAuth module");
    }
  } else {
    NSLog(@"PowerAuth module not found");
  }
  
  // CODE GENERATED BY THE REACT NATIVE TEMPLATE
  
  RCTRootView *rootView = [[RCTRootView alloc] initWithBridge:bridge moduleName:@"main" initialProperties:nil];
  rootView.backgroundColor = [[UIColor alloc] initWithRed:1.0f green:1.0f blue:1.0f alpha:1];

  UIViewController *rootViewController = [UIViewController new];
  rootViewController.view = rootView;
  self.window.rootViewController = rootViewController;
  [self.window makeKeyAndVisible];

  return bridge;
}

- (void)appController:(EXUpdatesAppController *)appController didStartWithSuccess:(BOOL)success
{
  appController.bridge = [self initializeReactNativeApp];
}

@end

For more information about the native configuration, you can visit official documentation of the native SDK.

Last updated on May 11, 2021 (12:54) Edit on Github Send Feedback
Search

1.5.x

PowerAuth for React Native