Skip to content

Latest commit

 

History

History
163 lines (123 loc) · 5.7 KB

File metadata and controls

163 lines (123 loc) · 5.7 KB

BlackBerry Spark Communications Platform

Quick Start Sample for JavaScript

The Quick Start application demonstrates how you can authenticate with the Spark SDK using the Identity Provider of your application. We demonstrate how you can obtain the user ID and token of your user's account and pass them to the Spark SDK to complete setup.


Getting Started

This sample requires the Spark SDK, which you can find along with related resources at the location below.

YouTube Getting Started Video

Getting started video

Prerequisites

Visit the Getting Started with Web section to see the minimum requirements.

To use this example, you must set up the following elements in config.js:

  • Oauth2 configuration (AUTH_CONFIGURATION)
  • Your Spark user domain (ID_PROVIDER_DOMAIN)
  • User passcode (USER_SECRET)

Walkthrough

Follow this guide for a walkthrough showing how to authenticate with the Spark SDK using Google Sign-in for Web.

Validate that the browser supports the Spark SDK for JavaScript

To verify that the browser has all required features, call BBMEnterprise.validateBrowser. It will return a rejected Promise in browsers which support Promise. If Promise is not supported, it will throw an exception.

// Validate that the browser is supported.
try {
  BBMEnterprise.validateBrowser()
  .catch(reportBrowserError);
} catch(error) {
  reportBrowserError(error);
}

Request an access token using the Google Sign-in API

To retrieve an access token, first update OAUTH_CONFIGURATION.clientId in config.js using web.clientId obtained from the google-services.json.

Next, obtain an object which performs generic oAuth authentication. Use it to obtain an access token and user information. Perform these actions when the 'Sign in' button is pressed.

$('#signInButton').click(LogIn);
function LogIn() {
  // Create the auth manager for the configured auth service.
  const authManager = createAuthManager();
  authManager.authenticate()
  .then(userData => {
    //  ... Initialize the Spark SDK here ...
  }
}

Initialize the Spark SDK

// Construct BBMEnterprise object.
const bbmeSdk = new BBMEnterprise({
  domain: ID_PROVIDER_DOMAIN,
  environment: ID_PROVIDER_ENVIRONMENT,
  userId: userData.userId,
  getToken: authManager.getBbmSdkToken,
  description: navigator.userAgent,
  kmsArgonWasmUrl: KMS_ARGON_WASM_URL
});

Monitor the setup state

When the setup state change, a 'setupState' event will be emitted. Listen for this to determine when the setup state changes.

// Handle changes of BBM Enterprise setup state.
bbmeSdk.on('setupState', state => {
  console.log(`BBMEnterprise setup state: ${state.value}`);
  $('#setupState').text(state.value);
  switch (state.value) {
    case BBMEnterprise.SetupState.Success:
      const userRegId = bbmeSdk.getRegistrationInfo().regId;
      $('#regId').text(userRegId || '?');
    break;
    case BBMEnterprise.SetupState.SyncRequired: {
      if (isSyncStarted) {
        $('#setupState').text(`Failed to get user keys using provided USER_SECRET`);
        return;
      }
      const isNew = bbmeSdk.syncPasscodeState === BBMEnterprise.SyncPasscodeState.New;
      const syncAction = isNew
        ? BBMEnterprise.SyncStartAction.New
        : BBMEnterprise.SyncStartAction.Existing;
      bbmeSdk.syncStart(USER_SECRET, syncAction);
    }
    break;
    case BBMEnterprise.SetupState.SyncStarted:
      isSyncStarted = true;
    break;
  }
});

Monitor for setup errors

The instance of BBMEnterprise will emit 'setupError' event on setup failure.

// Handle setup error.
bbmeSdk.on('setupError', error => {
  // Notify user about the error.
  $('#setupState').text(`Failed to setup the SDK | Error: ${error.value}`);
});

Perform setup

All that is left now is to set up the SDK.

  // Start BBM Enterprise setup.
  bbmeSdk.setupStart();

License

These samples are released as Open Source and licensed under the Apache 2.0 License.

Reporting Issues and Feature Requests

If you find a issue in one of the Samples or have a Feature Request, simply file an issue.