Users will only have a user ID if they’ve had to log in using one of Fliplet’s login components. For apps where no login is required it’s not possible to uniquely identify the user.

If a user has logged in using one of Fliplet’s login components then a user ID will have been generated for them. The user ID is a unique number automatically generated by Fliplet linked to the login account. To get the user ID you first need to get the data associated with the user and the current session.

Fliplet.User.getCachedSession()
  .then(function (session) {
    // Returns the session information
    var user = session.entries.dataSource;
  });

Note that if a user has logged in with a SAML2 Login or Fliplet Login component then the code changes slightly to be the following:

Fliplet.User.getCachedSession()
  .then(function (session) {
    // Returns the session information for a SAML2 Login user 
    var user = session.entries.saml2;
  });

Fliplet.User.getCachedSession()
  .then(function (session) {
    // Returns the session information for a Fliplet Login user
    var user = session.entries.flipletLogin;
  });

Now that we have the user session data you can access the ID and assign it to a variable (we’ve called our variable userId). You can then use userId to look up more information about a user in a data source.

Fliplet.User.getCachedSession()
  .then(function (session) {
    // returns the session information
    var user = session.entries.dataSource;

    // Checks if a user is logged in
    if (!user) {
      return Promise.reject('User is logged out.');
    }

    var userId = user.id;
    var userData = user.data;
  });