Events
Subscribing to the authentication events fired by the JS SDK means your application will be notified if the user's session state changes. This is important because the session state may change due to user interactions beyond your application's control. The only way your application can be notified of these changes is through subscribing to these events.
Using SPiD.event.subscribe("event name", callback)
attaches an event handler that will be invoked when the event fires. When an event is fired all subscribers to the event will be notified and given the response object.
The example repository contains a fully working example. Open index.html in your browser, and it will reveal your own login status, and allow you to log in/out. The JavaScript powering this example looks like the following.
SPiD.event.subscribe("SPiD.login", function (data) { console.log("SPiD.login", data); });
SPiD.event.subscribe("SPiD.logout", function (data) { console.log("SPiD.logout", data); });
SPiD.event.subscribe("SPiD.sessionChange", function (data) {
console.log("SPiD.sessionChange", data);
var output = document.getElementById("spid");
if (!data.session) {
output.innerHTML = "Welcome. Please <a href=\"" + SPiD_Uri.login() + "\">log in</a>";
} else {
output.innerHTML = "Welcome <a href=\"" + SPiD_Uri.account() + "\">" +
data.session.displayName + "</a>" +
" <a href=\"" + SPiD_Uri.logout() + "\">Log out</a>";
}
});
var config = {
client_id: "52f8e3d9efd04bb749000000",
server: "identity-pre.schibsted.com",
useSessionCluster: false
}
SPiD.init(config);
SPiD_Uri.init(config)
The useSessionCluster
property should be set to true
(or omitted) when running against the production server.
Available SDK events:
Name | Fired when ... |
---|---|
SPiD.notLoggedin |
There is no session on page load, user is not logged in |
SPiD.statusChange |
Page loads, status changes from unknown to Connected or notConnected |
SPiD.login |
|
SPiD.logout |
User logs out (either by you or another site (SPiD, other client). |
SPiD.userChange |
User in session has changed to another user |
SPiD.sessionInit |
Session is successfully initiated for the first time, on page load |
SPiD.sessionChange |
Always. This is a wrapping event, which is fired as a result of all other events that changes the session. |
SPiD.visitor |
SPiD identifies the current visitor. Yields a unique visitor id that can be used to track the user even when not logged in. Used in analytics (Mixpanel) tracking, etc |
SPiD.error |
An error occurred. Like communication timeouts, invalid responses or abuse (too many requests in a short time period). |
Depending on the user's actions, multiple events may be fired.
Page loads, user logged in
SPiD.login
SPiD.statusChange
SPiD.sessionInit
SPiD.sessionChange
Page loads, user not logged in
SPiD.notLoggedin
SPiD.sessionChange
Polling, and there is no change
SPiD.sessionChange
The change in session are the fields clientTime
and serverTime
for synchronization between client and server.
User logs out
SPiD.logout
SPiD.statusChange
SPiD.sessionChange
User logs out, and logs in with another user account
SPiD.logout
SPiD.userChange
SPiD.login
SPiD.sessionChange
Response Object
The response object is different, whether user is logged in or not.
Response, when logged in
{
session: {
baseDomain: "sdk.dev"
defaultAgreementAccepted: true
clientAgreementAccepted: true
clientTime: 1360329602301
displayName: "Anna Andersson"
expiresIn: 7138
familyName: "Andersson"
gender: "undisclosed"
givenName: "Anna"
id: "50604a7ddcb114ed0e000004"
photo: "https://secure.gravatar.com/avatar/ec32937c22d1a4b1474657b776d0f398?s=200"
result: true
serverTime: 1360329601
userId: 270177
userStatus: "connected"
sig: "9toJgvXfPgk-5W2162sD8ueHzZ8Ya1ibBWvELv-I-lk.eyJyZXN1bHQiOnRydWUsInNlcnZlclRpbWUiOjEzNjAzMjk2MDEsInVzZXJTdGF0dXMiOiJjb25uZWN0ZWQiLCJ1c2VySWQiOjI3MDE3NywiaWQiOiI1MDYwNGE3ZGRjYjExNGVkMGUwMDAwMDQiLCJkaXNwbGF5TmFtZSI6IkpvYWtpbSBXYW5nZ3JlbiIsImdpdmVuTmFtZSI6IkpvYWtpbSIsImZhbWlseU5hbWUiOiJXYW5nZ3JlbiIsImdlbmRlciI6InVuZGlzY2xvc2VkIiwicGhvdG8iOiJodHRwczpcL1wvc2VjdXJlLmdyYXZhdGFyLmNvbVwvYXZhdGFyXC9lYzMyOTM3YzIyZDFhNGIxNDc0NjU3Yjc3NmQwZjM5OD9zPTIwMCIsImV4cGlyZXNJbiI6NzEzOCwiYmFzZURvbWFpbiI6InNkay5kZXYiLCJhbGdvcml0aG0iOiJITUFDLVNIQTI1NiJ9"
},
status: "connected"
}
Response, when not logged in
{
session: null,
status: "unknown"
}
On this page
Read more about the JavaScript SDK
See also
Help us improve
Did you spot an error? Or maybe you just have a suggestion for how we can improve? Leave a comment, or better yet, send us a pull request on GitHub to fix it (in-browser editing, only takes a moment).
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.