javascriptreactjslaunchdarklyfeature-flags

How are users identified in LaunchDarkly?


I'm using LaunchDarkly for feature flag management and I don't know where to get the key of the current user. Here is an example from the LD documentation of their React SDK:

import { withLDProvider } from 'launchdarkly-react-client-sdk';
export default withLDProvider({
  clientSideID: 'your-client-side-id',
  user: {
    "key": "aa0ceb",
    "name": "Grace Hopper",
    "email": "gracehopper@example.com"
  },
  options: { /* ... */ }
})(YourApp);

How do I get this key? What is the difference between the key and clientSideID?


Solution

  • LaunchDarkly docs is a bit confusing on various keys needed to use this tool.

    SDK key

    The clientSideID is the SDK key (client side). You can obtain it from the LaunchDarkly web interface. It is the same for the whole environment in the project, so if you use React SDK or JS SDK it will be the same for every client.

    To get it, go to app.launchdarkly.com/YOUR_PROJECT, click on a search icon in the right upper corner. You will see a prompt with the buttons. You need to click the button Copy SDK key for the current development => Client-side ID. Note that if you have dev environment and prod environment, the keys for them will be different. Alternatively you can go to Account settings -> Projects to see all SDK keys.

    See the details about different kinds of SDK keys

    Access token

    If you don't use the SDK but use the the REST API, you will need an access token. You can obtain it from the LaunchDarkly Web interface by going to Account settings -> Authorization. You can have multiple access tokens.

    User key

    The user key uniquely identifies the user of your application. You are responsible for generating it. According to the documentation, it is not possible to pre-create the users in LaunchDarkly interface. Instead, LaunchDarkly registers the users dynamically when you provide the new user keys.

    The user key is different for different registered users of your application. It's up to you what to use as the user unique key but you are responsible for storing it in your database and always provide the same key for the same user.

    When the user logs in you will need to initialize the LaunchDarkly SDK and provide the corresponding key.

    In React:

    import { withLDProvider } from 'launchdarkly-react-client-sdk';
    export default withLDProvider({
      clientSideID: 'your-client-side-id',
      user: {
        "key": getCurrentUser().id,
        "name": getCurrentUser().name,
        "email": getCurrentUser().email
      },
      options: { /* ... */ }
    })(YourApp);
    

    In pure JavaScript:

    const user = {
        "key": getCurrentUser().id,
        "name": getCurrentUser().name,
        "email": getCurrentUser().email
    };
    const ldclient = LDClient.initialize('YOUR_CLIENT_SIDE_SDK', user, options = {
      allAttributesPrivate: true
    });
    

    Here getCurrentUser() is the function that gets the authenticated user details for your application .

    When the user logs out, make sure that the key of that user is not available. Tracking of anynonymous users is described in the documentation of LaunchDarkly, it's possible to use different strategies for that. The easiest way to manage anonymous users is not to send the user key.