Authentication
Once you've initialised ZumoKit within your application, you'll need to authenticate a user to start using the SDK. SDK assumes your platform uses JWT tokens to authenticate your users. When calling SDK’s auth method make sure the JWT token for the current user is passed to the method. To get started follow these steps:
- Register/populate your API's data storage with user accounts.
- Login into your API using a user account.
- Pass the user's JWT token to ZumoKit SDK's authentication method.
- If authentication is successful you can continue with wallet creation and other SDK related operations.
Android
In the following example we provide authenticated user's JWT token and AuthCallback
object to the auth
method on the ZumoKit
instance we created in the Initialization section. If your API requires any additional headers for authenticated request, for instance some kind of API key, you can propagate these via headers parameter of auth
method.
The onError
method of the callback will called when authentication was not successful. Read the httpCode
and data
parameters for more information as to why. If onSuccess
is called then authentication succeeded and you can proceed to the next step.
import com.zumo.zumokit.ZumoKit;
import com.zumo.zumokit.AuthCallback;
public class MainActivity extends AppCompatActivity {
private ZumoKit zumoKit;
@Override
protected void onCreate(Bundle savedInstanceState) {
// ...
String token = "eyJ0eXAi...JSUzI1N"; // JWT token
HashMap<String, String> headers = new HashMap<String, String>();
headers.put("Optional-Header-1", "example-value");
this.zumoKit.auth(token, headers, new AuthCallback() {
@Override
public void onError(short httpCode, String data) {
// Something went wrong authenticating…
// Check the `httpCode` and `data` for info.
}
@Override
public void onSuccess() {
// Authentication was successful!
}
});
}
}
iOS
Using the instance of ZumoKitImpl
we created in the Initialization section, we can access the zumoCore
property and its relevant methods. In the following example we provide authenticated user's JWT token and callback object to the -auth:callback:
method. If your API requires any additional headers for authenticated request, for instance some kind of API key, you can propagate these via headers parameter of auth
method.
In the callback's completionHandler
block, check the success
flag to see if authentication was successful or not. The errorCode
and data
arguments will provide more detail should authentication fail.
#import <ZumoKit/ZumoKit.h>
#import <ZumoKitSDK/iOSAuthCallback.h>
@implementation ViewController
- (void)viewDidLoad {
// ...
iOSAuthCallback *callback = [[iOSAuthCallback alloc]
initWithCompletionHandler:^(bool success, short errorCode, NSString * _Nullable data) {
// Check the `success` flag to see if there was an issue authenticating.
// The `errorCode` and `data` arguments will provide more information if there was a problem.
}];
NSString token = @"eyJ0eXAi...JSUzI1N"; // JWT token
NSDictionary *headers = @{ @"Optional-Header-1" : @"example-value"};
[[_zumoKit zumoCore] auth:token headers:headers callback:callback];
}
@end
React Native
Once the ZumoKit
singleton has been initialized, users can be authenticated with the auth
method. As all methods in ZumoKit use promises, we can take advantage of ES6's async/await syntax.
An exception is thrown if the promise is rejected and the error can be caught and handled.
import React, { Component } from 'react';
import ZumoKit from 'react-native-zumo-kit';
class HelloZumoKit extends Component {
async componentDidMount() {
// ...
try {
token = "eyJ0eXAi...JSUzI1N"; // JWT token
await ZumoKit.auth(token);
// Authentication was a success!
} catch (error) {
// There was a problem authenticating.
}
}
}