Fliplet.OAuth2 (Beta)

The fliplet-oauth2 package contains helpers for standardizing requests to OAuth2 web services with a client-side integration.

Note: The Fliplet OAuth2 library is currently in beta. We suggest specifying the library version using fliplet-oauth2:0.1 when including the Fliplet OAuth2 library to avoid the functionality breaking when new versions of the feature are released.

To configure an OAuth2 service provider, call Fliplet.OAuth2.configure() with the service specificaions and use Fliplet.OAuth2(service) with the specified service name to access the available methods, e.g. Fliplet.OAuth2(service).api(path).

// Configure services
Fliplet.OAuth2.configure(service, configuration);
// Make API requests using configured service
Fliplet.OAuth2(service).api(path)
  .then(function (response) {
    // Process response from API service as necessary
  });

Examples

1. Configure a connection with GitHub

Note: The service configuration is likely executed via the Global JS code in Fliplet Studio.

Flipet.OAuth2.configure('github', {
  authUrl: 'http://github.com/login/oauth/authorize', // from OAuth2 service provider
  grantType: 'implicit', // as supported by OAuth2 service provider
  grantUrl: 'https://github.com/login/oauth/access_token', // from OAuth2 service provider
  baseUrl: 'https://api.github.com/', // from OAuth2 service provider
  clientId: 'uztcbv3bwtkxmmej1lxv', // from OAuth2 service provider
  clientSecret: 'jepbrhknlxjxriltyjrtprbevdfclnagn2uc1dsq', // from OAuth2 service provider
  redirectUrl: 'https://fliplet.com/oauth2-success', // as configured with OAuth2 service provider
  state: 'cbv3bwhJv6K5l9-1' // optional state parameter during login
});

2. Log in using the configured GitHub service

Note: The service name as configured through Fliplet.OAuth2.configure() is passed to Fliplet.OAuth2() to make sure the correct connection configurations are used.

// Start login process
Fliplet.OAuth2('github').login()
  .then(function (response) {
    // Authentication response is cached and returned
  });

3. Make API requests using the configured GitHub service

Note: The service name as configured through Fliplet.OAuth2.configure() is passed to Fliplet.OAuth2() to make sure the correct connection configurations are used.

The Fliplet OAuth2 library will manage authentication tokens and any token refreshing (if configured) before making API calls.

The login page can process these query parameters via Fliplet.Navigate.query to customize the user experience accordingly.

Fliplet.OAuth2('github').api('user')
  .then(function (response) {
    // Process response from API service as necessary
  });

Cross-Domain AJAX requests

A common problem for developers is a browser to refuse access to a remote resource. Usually, this happens when you execute AJAX cross domain request using jQuery Ajax interface, Fetch API, or plain XMLHttpRequest. As result is that the AJAX request is not performed and data are not retrieved.

This can occur with OAuth2 services if the service provider is not configured to allow Fliplet’s app domains.

Cross-Origin Request Sharing (CORS) sometimes needs to be configured with the service provider to ensure API requests can be made across domains. See AJAX cross domain and cross-origin requests for more information.

Methods

Fliplet.OAuth2.configure()

(Returns null)

Configure an OAuth2 service or multiple OAuth2 services.

Fliplet.OAuth2.configure(service, configuration)
Fliplet.OAuth2.configure(services)

Fliplet.OAuth2().login()

(Returns Promise)

Initialize the OAuth2 authentication process with the in-app browser.

Fliplet.OAuth2(service).login()

Fliplet.OAuth2().logout()

(Returns Promise)

Logout and remove session with a specific service.

Fliplet.OAuth2(service).logout()

Fliplet.OAuth2().getAuthResponse()

(Returns Promise)

Get the current status of sessions with a selected service. This does not validate any sessions which may have expired.

Fliplet.OAuth2(service).getAuthResponse()

Fliplet.OAuth2().api()

(Returns Promise)

Make calls to the API for getting and posting data.

Note: The Fliplet OAuth2 library will manage authentication tokens and any token refreshing (if configured) before making API calls.

Fliplet.OAuth2(service).api(path)
Fliplet.OAuth2(service).api(options)

Fliplet.OAuth2().on()

(Returns null)

Add event listeners to OAuth2 responses.

Fliplet.Oauth2(service).on(eventName, fn)

Events

Back to API documentation