Configuration
Before you call any method on the newly created const powerAuth = new PowerAuth(instanceId)
object, you need to configure it first. Unconfigured instance will throw exceptions. Use await powerAuth.isConfigured()
to check if configured.
1. Parameters
You will need the following parameters to prepare and configure a PowerAuth instance:
- 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 instance directly in JavaScript. Simply import it from the module and use the following snippet.
import { PowerAuth } from 'react-native-powerauth-mobile-sdk';
import { Component } from 'react';
export default class AppMyApplication extends Component {
private powerAuth = new PowerAuth("your-app-instance-id");
async setupPowerAuth() {
// already configured instance will throw an
// exception when you'll try to configure it again
const isConfigured = this.powerAuth.isConfigured();
if (isConfigured) {
console.log("PowerAuth was already configured.");
} else {
try {
await this.powerAuth.configure("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.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 {
String instanceId = "your-app-activation";
PowerAuthSDK.Builder builder = new PowerAuthSDK.Builder(
new PowerAuthConfiguration.Builder(instanceId, "https://your-powerauth-endpoint.com/", "APPLICATION_KEY", "APPLICATION_SECRET", "KEY_SERVER_MPK").build()
);
((PowerAuthRNPackage) pkg).configure(instanceId, 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.