JavaScript API

NIKEPLUS.api

NIKEPLUS.api(endpoint, options)
API calls are made by using NIKEPLUS.api. The first argument is the endpoint (see API documentation for available endpoints); the second argument is an object of options.

Possible NIKEPLUS.api options

Option Type Default Description
success Function
Called when the API call succeeds. It receives the result of the API call.
error Function Called when the API call results in an error. It receives information about the error.
data Object An object of the data to be sent with the API request.
method HTTP method GET The HTTP method of the API call.
cache Boolean true If false, a timestamp will be added to get requests so the browser does not serve a cached response.

NIKEPLUS.auth.setState

NIKEPLUS.auth.setState(state)

Use NIKEPLUS.auth.setState to set the state parameter in the authorization process. This can be done only after NIKEPLUS.init is called.

If setState is called after NIKEPLUS.login was called then NIKEPLUS.login will need to be called again. Any existing login button will be replaced.

NIKEPLUS.events

The SDK exposes events for which you can register and unregister callbacks.
Event
When Triggered
Returns
api.error
Triggered when a call to the API returns an error response. Response from the API
api.response
Triggered when a call to the API returns a successful response. Response from the API
auth.expiredToken Triggered when making a call that requires the stored access token and it has expired. If the implicit flow was used for authentication, then a choice can be made on whether to ask the user to login again or not. If authorization code was used for authentication, then there can be a server side call to refresh the token and pass it back to the SDK. Nothing
auth.logout Triggered when a user logs out. Nothing
auth.noToken Triggered when making a call that requires the stored access token and it has expired or there never was a token. If the implicit flow was used for authentication, then a choice can be made on whether to ask the user to login again or not. An object with the reason there is no token, either 'expired token' or 'no token'

NIKEPLUS.events.subscribe

NIKEPLUS.events.subscribe(event, callback)
Subscribe a callback to one of the events listed above with NIKEPLUS.events.subscribe.

Arguments for NIKEPLUS.events.subscribe

Argument Type Description
event String An event from the list above.
callback Function The function called when the event is triggered.

NIKEPLUS.events.unsubscribe

NIKEPLUS.events.unsubscribe(event, callback)
Unsubscribe a callback to one of the events listed above with NIKEPLUS.events.unsubscribe. If you call unsubscribe for an event and do not pass a callback, all callbacks subscribed to that event will be unsubscribed.

Arguments for NIKEPLUS.events.unsubscribe

Argument Type Description
event String An event from the list above.
callback Function Stop calling the passed in function when the event is triggered.

NIKEPLUS.getAuthData

NIKEPLUS.getAuthData(callback(authData))
Use NIKEPLUS.getAuthData to retrieve any authorization data stored in the SDK.The authorization data will be passed to the callback you pass in to NIKEPLUS.getAuthData as an object that will always contain a status property.

Possible status returned by getAuthData

Status Description
noToken The SDK has no access token stored.
expiredToken An existing token has expired and erased from storage.
validToken The SDK has stored what it believes to be a valid access token.

Examples of Responses

{
    status: "noToken"
}
{
    status: "expiredToken"
}
{
    status: "validToken",
    authData: {
        access_token: "the_access_token",
        expires_in: 3599,
        token_type: "bearer",
        user_id: "Nike User Id (NUID)"
    }
}

Authorization Data Stored by SDK

Data Description
access_token The access token for the user.
expires_in The time in seconds until the access token expires.
token_type The bearer when retrieved via the implicit flow.
user_id Nike User ID (NUID)
profile_img_url If there is a profile image for the user, the URL will be returned.

NIKEPLUS.helpers.conversions

NIKEPLUS.helpers.conversions is a collection of methods to help convert data returned from the APIs to common units of measurement.

Arguments for NIKEPLUS.helpers.conversions

All of the methods in NIKEPLUS.helpers.conversions take the same arguments:
Argument Required Type Description
valueToConvert yes number The value you want to convert.
precision no integer The number of decimals places you would like in the response.  Must be an integer greater than zero.

NIKEPLUS.helpers.conversions.kilometersToMiles

NIKEPLUS.helpers.conversions.kilometersToMiles(valueToConvert, precision)
Use NIKEPLUS.helpers.conversions.kilometersToMiles to convert values in kilometers to miles.

NIKEPLUS.helpers.conversions.metersToFeet

NIKEPLUS.helpers.conversions.metersToFeet(valueToConvert, precision)
Use NIKEPLUS.helpers.conversions.metersToFeet to convert values in meters to feet.

NIKEPLUS.init

NIKEPLUS.init(options)
Use the NIKEPLUS.init method to pass settings to the SDK.

Possible NIKEPLUS.init Settings

Setting
Type
Default
Required Description
client_id
String Yes
The Nike provided Client ID for the application.
sitePrefix
String
When multiple sites have the same client id, use this site prefix to keep sessions separate per site. This is an unique String.
oauthPopup
Boolean
false
When True, the login screen will open in a new window and the OAuth redirect will happen in that window. The application will need to handle the closing of the window after the redirect. When False, the login screen will load in the existing window, and redirect in this window.
state
String
   

Returned on the OAuth redirect and used to maintain state through the redirect and prevent cross-site forgery.

response_type
String
token
  Two possible values: 'token' or 'code'. This determines the OAuth flow to be used for authorization. When 'token', the implicit flow will be used. When 'code', the authorization code will be used.
redirect_uri
String
  Yes
The redirect URI after authorization. All redirect URIs need to be whitelisted.
authSuccess
Function     Called after a successful redirect when response type is 'token'. It will be passed an object with all the data from the authorization.
authError
Function
    Called after an error redirect, and the response type is 'token'. It will be passed an object with error data from the URL.

Example

function nikeplusSDKAsyncInit() {
    NIKEPLUS.init({
        client_id: "your_client_id",
        redirect_uri: "http://your_redirect_uri.com/redirect.html",
        authSuccess: function(authData){}
    });
}

NIKEPLUS.login

NIKEPLUS.login(buttonContainer)

Calling NIKEPLUS.login will append a "Nike+ login" button in the HTML element passed in to login. If no HTML element is passed in, the login button will be appended to the document body. When the user clicks the button, the Nike+ login screen will open and start the OAuth process. The button will be the below:

Calling NIKEPLUS.login a second time will remove any existing "Nike+ login" button on the page before adding the button again.

NIKEPLUS.logout

NIKEPLUS.logout()
Calling NIKEPLUS.logout will clear all data cached by the SDK for the user.

NIKEPLUS.setAuthData

NIKEPLUS.setAuthData(authData, callback(authData))
Use NIKEPLUS.setAuthData to set authorization data by passing in an object with these properties: access_token, expires_in, token_type, user_id, profile_img_url (access_token and expires_in are the only required properties). A common use case for this method is passing a token to the SDK when using authorization code. If a callback is provided, then it will be passed to the newly stored authData.
NIKEPLUS.setAuthData({
    access_token: "an access token",
    expires_in: 3599
}, function(authData){
    console.log(authData);
});