API Docs

All arguments to the *URI functions are optional. When omitted, client_id defaults to the client_id used in SPiD.init, and redirect_uri defaults to window.location.

For the following examples, assume the following variable declarations are available:

var redirectUri = "http://example.com";
var clientId = "4321abc0000";

SPiD.init

SPiD.init(options)

Initialises the SDK. The options parameter must contain the properties server and client_id.

SPiD.hasSession

SPiD.hasSession(callback)

Checks the current session status, and call the provided callback with two arguments the first being error and the second being the result. Calling hasSession will trigger a number of events (depending on the user status et cetera), to SPiD.event subscribers, notably the SPiD.sessionChange event.

SPiD.hasSession(function (err, response) {
    // do something with the response, if !err ...
});

If the user is not signed-on to SPiD the callback will be called with err set to: js { code: 401, type: "UserException", description: "No session found" } Note: this will happen also if third-party cookies are blocked, regardless of the user state

SPiD.hasProduct

SPiD.hasProduct(product_id, callback, force)

Checks if user has access to a product. Set force to true to force a roundtrip (e.g., bypass the cache). The callback will be called with two arguments the first being error and the second an object, whose result property will be true if the user has access to the product in question.

SPiD.hasProduct(2134423, function (err, response) {
    response === {
        baseDomain: "sdk.dev",
        expiresIn: 6967,
        id: "4ebb7ce59caf7c1f22000001",
        userId: 2200021,
        uuid: "7c4d3f05-0829-42c4-a6e4-202ea91012f5",
        productId: 10010,
        result: true,
        serverTime: 1360342353,
        sig: "layQctw_mMOAM5RJnr9nr7RgPv_0lZqVAojXBVpQSBY.eyJyZXN1bHQiOnRydWUsInNlcnZlclRpbWUiOjEzNjAzNDIzNTMsInByb2R1Y3RJZCI6MTAwMTAsInVzZXJTdGF0dXMiOiJjb25uZWN0ZWQiLCJ1c2VySWQiOjIyMDAwMjEsImlkIjoiNGViYjdjZTU5Y2FmN2MxZjIyMDAwMDAxIiwiZGlzcGxheU5hbWUiOiJKb2FraW0gV1x1MDBlNW5nZ3JlbiIsImdpdmVuTmFtZSI6IkpvYWtpbSIsImZhbWlseU5hbWUiOiJXXHUwMGU1bmdncmVuIiwiZ2VuZGVyIjoibWFsZSIsInBob3RvIjoiaHR0cDpcL1wvd3d3LmdyYXZhdGFyLmNvbVwvYXZhdGFyXC9lYzMyOTM3YzIyZDFhNGIxNDc0NjU3Yjc3NmQwZjM5OD9zPTIwMCIsImV4cGlyZXNJbiI6Njk2NywiYmFzZURvbWFpbiI6InNkay5kZXYiLCJhbGdvcml0aG0iOiJITUFDLVNIQTI1NiJ9"
    };
});

SPiD.hasSubscription

SPiD.hasSubscription(product_id, callback, force)

Checks if user has access to a subscription. Set force to true to force a roundtrip (e.g., bypass the cache). The callback will be called with two arguments the first being error and the second an object, whose result property will be true if the user has access to the product in question.

SPiD.hasSubscription(324892374, function (err, response) {
    response === {
        baseDomain: "sdk.dev",
        expiresIn: 7173,
        id: "4ebb7ce59caf7c1f22000001",
        uuid: "7c4d3f05-0829-42c4-a6e4-202ea91012f5",
        userId: 2200021,
        productId: 10010,
        subscriptionId: 165572,
        result: true,
        serverTime: 1360342380,
        sig: "ynj1V3nZhRa3JBw9_Up97e5yX0PLkWM0bc6M2EcHmLs.eyJyZXN1bHQiOnRydWUsInNlcnZlclRpbWUiOjEzNjAzNDIzODAsInByb2R1Y3RJZCI6MTAwMTAsInVzZXJTdGF0dXMiOiJjb25uZWN0ZWQiLCJ1c2VySWQiOjIyMDAwMjEsImlkIjoiNGViYjdjZTU5Y2FmN2MxZjIyMDAwMDAxIiwic3Vic2NyaXB0aW9uSWQiOjE2NTU3MiwiZGlzcGxheU5hbWUiOiJKb2FraW0gV1x1MDBlNW5nZ3JlbiIsImdpdmVuTmFtZSI6IkpvYWtpbSIsImZhbWlseU5hbWUiOiJXXHUwMGU1bmdncmVuIiwiZ2VuZGVyIjoibWFsZSIsInBob3RvIjoiaHR0cDpcL1wvd3d3LmdyYXZhdGFyLmNvbVwvYXZhdGFyXC9lYzMyOTM3YzIyZDFhNGIxNDc0NjU3Yjc3NmQwZjM5OD9zPTIwMCIsImV4cGlyZXNJbiI6NzE3MywiYmFzZURvbWFpbiI6InNkay5kZXYiLCJhbGdvcml0aG0iOiJITUFDLVNIQTI1NiJ9"
    };
});

SPiD.logout

Logs the user out, and calls the callback when done.

SPiD.logout(function (data) {
    alert("See ya next time!");
});

SPiD.acceptAgreement

Calls the acceptAgreement endpoint, and (if the acceptAgreement call was successful) re-calls hasSession, clearing any cached value before doing so.

SPiD.acceptAgreement(callback)

SPiD.event.subscribe

Register subscriber for event. The event handler function will be called whenever the named event occurs.

SPiD.event.subscribe("SPiD:login", function (data) {
    alert("Welcome!");
});

SPiD.event.unsubscribe

Remove previously subscribed function. Pass in the event name and the function originally passed to SPiD.event.subscribe.

var callback = function (data) {
    alert("Welcome!");
};

SPiD.event.subscribe("SPiD:login", callback);

// ...

SPiD.event.unsubscribe("SPiD:login", callback);

Note that it must be the same function reference, you cannot inline the function in the subscribe call when you want to later unsubscribe it.

SPiD.event.fire

Fire an event. The first argument is the event name, additional arguments are passed to subscribers. This function is used by the JS SDK to propagate events to client subscribers, but is not recommended for external use.

SPiD.sessionCache.get

Gets the currently cached session object.

SPiD.sessionCache.get();

SPiD.sessionCache.set

Sets the currently cached session object, with an optional expiration time.

SPiD.sessionCache.set(value, exipresInMillis);

SPiD.sessionCache.clear

Clears the session cache. Use this when you want to force a request to the hasSession endpoint.

SPiD.sessionCache.clear();

SPiD.Uri

The SPiD.Uri module is separated from the main JS SDK, and is optional. It's assigned to the global variable SPiD_Uri when using the vanilla JS variant. The SPiD.Uri module is also available as with AMD on CommonJS packaging.

SPiD.Uri.login

Returns the login URI.

SPiD_Uri.login(); //=> "https://identity-pre.schibsted.com/flow/login?response_type=code&flow=signup&client_id=4321abc0000&redirect_uri=http%3A%2F%2Flocalhost"
SPiD_Uri.login(redirectUri, clientId); //=> "https://identity-pre.schibsted.com/flow/login?response_type=code&flow=signup&client_id=abcdef&redirect_uri=http%3A%2F%2Fexample.com"

SPiD.Uri.signup

Returns the signup URI.

SPiD_Uri.signup(); //=> "https://identity-pre.schibsted.com/flow/signup?response_type=code&flow=signup&client_id=52f8e3d9efd04bb749000000&redirect_uri=http%3A%2F%2Flocalhost"
SPiD_Uri.signup(redirectUri, clientId); //=> "https://identity-pre.schibsted.com/flow/signup?response_type=code&flow=signup&client_id=4321abc0000&redirect_uri=http%3A%2F%2Fexample.com"

SPiD.Uri.logout

Returns the logout URI.

SPiD_Uri.logout(); //=> "https://identity-pre.schibsted.com/logout?client_id=52f8e3d9efd04bb749000000&redirect_uri=http%3A%2F%2Flocalhost"
SPiD_Uri.logout(redirectUri, clientId); //=> "https://identity-pre.schibsted.com/logout?client_id=4321abc0000&redirect_uri=http%3A%2F%2Fexample.com"

SPiD.Uri.account

Returns the account summary page URI.

SPiD_Uri.account(); //=> "https://identity-pre.schibsted.com/account/summary?response_type=code&client_id=52f8e3d9efd04bb749000000&redirect_uri=http%3A%2F%2Flocalhost"
SPiD_Uri.account(redirectUri, clientId); //=> "https://identity-pre.schibsted.com/account/summary?response_type=code&client_id=4321abc0000&redirect_uri=http%3A%2F%2Fexample.com"

SPiD.Uri.purchaseHistory

Returns the purchase history page URI.

SPiD_Uri.purchaseHistory(); //=> "https://identity-pre.schibsted.com/account/purchasehistory?response_type=code&client_id=52f8e3d9efd04bb749000000&redirect_uri=http%3A%2F%2Flocalhost"
SPiD_Uri.purchaseHistory(redirectUri, clientId); //=> "https://identity-pre.schibsted.com/account/purchasehistory?response_type=code&client_id=4321abc0000&redirect_uri=http%3A%2F%2Fexample.com"

SPiD.Uri.subscriptions

Returns the subscriptions page URI.

SPiD_Uri.subscriptions(); //=> "https://identity-pre.schibsted.com/account/subscriptions?response_type=code&client_id=52f8e3d9efd04bb749000000&redirect_uri=http%3A%2F%2Flocalhost"
SPiD_Uri.subscriptions(redirectUri, clientId); //=> "https://identity-pre.schibsted.com/account/subscriptions?response_type=code&client_id=4321abc0000&redirect_uri=http%3A%2F%2Fexample.com"

SPiD.Uri.products

Returns the products page URI.

SPiD_Uri.products(); //=> "https://identity-pre.schibsted.com/account/products?response_type=code&client_id=52f8e3d9efd04bb749000000&redirect_uri=http%3A%2F%2Flocalhost"
SPiD_Uri.products(redirectUri, clientId); //=> "https://identity-pre.schibsted.com/account/products?response_type=code&client_id=4321abc0000&redirect_uri=http%3A%2F%2Fexample.com"

SPiD.Uri.redeem

Returns the redeem voucher page URI. If voucher_code is supplied, the voucher will automatically be redeemed when the URI is visited.

SPiD_Uri.redeem();
//=> "https://identity-pre.schibsted.com/account/redeem?response_type=code&client_id=52f8e3d9efd04bb749000000&redirect_uri=http%3A%2F%2Flocalhost"

var voucherCode = "12312314";
SPiD_Uri.redeem(voucherCode, redirectUri, clientId);
//=> "https://identity-pre.schibsted.com/account/redeem?response_type=code&client_id=4321abc0000&redirect_uri=http%3A%2F%2Fexample.com&voucher_code=12312314"

SPiD.Uri.purchaseHistory

Returns the purchase product page URI. If product_id is supplied, the URI will lead to the "choose payment method" page, otherwise it will lead to the "choose product" page.

SPiD_Uri.purchaseHistory();
//=> "https://identity-pre.schibsted.com/flow/checkout?response_type=code&flow=payment&client_id=52f8e3d9efd04bb749000000&redirect_uri=http%3A%2F%2Flocalhost"

var productId = "12312314";
SPiD_Uri.purchaseHistory(productId, redirectUri, clientId);
//=> "https://identity-pre.schibsted.com/flow/checkout?response_type=code&flow=payment&client_id=4321abc0000&redirect_uri=http%3A%2F%2Fexample.com&product_id=12312314"

SPiD.Uri.purchaseProduct

Returns the purchase product page URI. If product_id is supplied, the URI will lead to the "choose payment method" page, otherwise it will lead to the "choose product" page.

SPiD_Uri.purchaseProduct();
//=> "https://identity-pre.schibsted.com/flow/checkout?response_type=code&flow=payment&client_id=52f8e3d9efd04bb749000000&redirect_uri=http%3A%2F%2Flocalhost"

var productId = "12312314";
SPiD_Uri.purchaseProduct(productId, redirectUri, clientId);
//=> "https://identity-pre.schibsted.com/flow/checkout?response_type=code&flow=payment&client_id=4321abc0000&redirect_uri=http%3A%2F%2Fexample.com&product_id=12312314"

SPiD.Uri.purchaseCampaign(campaign_id, product_id, voucher_code redirect_uri, client_id)`

Get URI for purchase campaign page. If campaign_id is supplied, the URI will take the user to the products in the campaign.

SPiD_Uri.purchaseCampaign();
//=> "https://identity-pre.schibsted.com/flow/checkout?response_type=code&flow=payment&client_id=52f8e3d9efd04bb749000000&redirect_uri=http%3A%2F%2Flocalhost"

var campaignId = "39843984";
var productId = "12312314";
var voucherCode = "89878966";
SPiD_Uri.purchaseCampaign(campaignId, productId, voucherCode, redirectUri, clientId);
//=> "https://identity-pre.schibsted.com/flow/checkout?response_type=code&flow=payment&client_id=4321abc0000&redirect_uri=http%3A%2F%2Fexample.com&campaign_id=39843984&product_id=12312314&voucher_code=89878966"

Comments/feedback

Do you have questions, or just want to contribute some newly gained insight? Want to share an example? Please leave a comment. SPiD reads and responds to every question. Additionally, your experience can help others using SPiD, and it can help us continuously improve our documentation.