Session JS APIs
Session JS APIs
Get and set values
Get the current session
// Get the session using the locally cached offline data.
// This is fast and work across all network conditions.
Fliplet.User.getCachedSession().then(function onCachedSessionRetrieved(session) {
console.log(session);
});
// This only works if the app is online as it makes an API request
// to Fliplet servers to return the live data.
Fliplet.Session.get().then(function onSessionRetrieved(session) {
console.log(session);
});
Get a key from the current session
Fliplet.Session.get(key).then(function onSessionKeyRetrieved(value) {
console.log(value);
});
Set new values into the current session
Fliplet.Session.set({ foo: 'bar' }).then(function onSessionUpdated() {
// session data has successfully been set here
});
Clear all values from the current session
Fliplet.Session.clear().then(function onSessionCleared() {
// session has been cleared when this runs
});
Passports
Log out the user from a passport
The logout()
function requires the first parameter to be the passport type to log out from. Valid inputs are saml2
, dataSource
and flipletLogin
.
Fliplet.Session.logout('dataSource').then(function () {
// here you can redirect (or else) once log out is acknowledged from the server
});
Log out from all passports
If you don’t provide a specific passport type to the logout()
function, the user will be logged out from all its passports. This is useful to code a generic “log out” feature instead of relying on a specific login component.
Fliplet.Session.logout().then(function onSessionDestroyed() {
// user has been logged out
});
Read details about connected accounts
If your app contains a login component (either DataSource, SAML2 or Fliplet) you can use the session to check whether the user is logged in and in and some of the connected account(s) details:
Fliplet.User.getCachedSession().then(function(session) {
if (session && session.entries) {
// the user is logged in;
// check if the user is connected to a dataSource login
if (session.entries.dataSource) {
// user is logged in against a Fliplet dataSource
}
// check if the user is connected to a SAML2 login
if (session.entries.saml2) {
// user is logged in against your company's SAML2
}
// check if the user is connected to a Fliplet login
if (session.entries.flipletLogin) {
// user is logged in with a Fliplet Studio account
}
} else {
// not logged in
}
});
Data for the connected account(s) can also be read and used as necessary:
Example for dataSource login
Fliplet.User.getCachedSession().then(function (session) {
var user = _.get(session, 'entries.dataSource.data');
if (!user) {
return; // user is not logged in
}
// contains all columns found on the connected dataSource entry
console.log(user);
});
Example for SAML2
Fliplet.User.getCachedSession().then(function (session) {
var user = _.get(session, 'entries.saml2.user');
if (!user) {
return; // user is not logged in
}
// contains id, email, firstName, lastName
console.log(user);
});
Example for Fliplet Studio login
Fliplet.User.getCachedSession().then(function (session) {
var user = _.get(session, 'entries.flipletLogin');
if (!user) {
return; // user is not logged in
}
// contains id, email, firstName, lastName, fullName, userRoleId, legacyId
console.log(user);
});
Language (locale)
[Closed beta] This feature is currently in development and it's not available yet to all customers.
Set the current user’s language to a new locale
// Change the language to a new country code
Fliplet.Session.Locale.set('fr');
// Change the language to a new country + territory code
Fliplet.Session.Locale.set('fr-ca');
Get the current user’s language
// Get the primary locale in use by the user for the current app
var locale = Fliplet.Env.get('userLocale');
// Get the list of user's locales. As an example, when the locale has been set to "fr-ca"
// the resulting list may be: ['fr-ca', 'fr', 'en-GB', 'en-US', 'en', 'it']
var locales = Fliplet.Env.get('userLocales');
// Get the locale that has previously been set
// in the user's session with the "set()" method.
// This allows you to check if a language has been explicitly set by the user.
Fliplet.Session.Locale.get().then(function (language) {
// language will be the locale string you previously set, e.g. "fr-ca"
});
Get the list of locales supported by the current app
// Gets the list of locales defined for the current app as set by the Studio user
var locales = Fliplet.App.Locales.get();