Forum Discussion

sriray's avatar
sriray
Contributor
12 years ago

How do I get the currently logged-in user details like avatar, id and Name using Javascript?

Hi,

 

We are having a JS based widget that would need to display basic user details like the avatar, name and a unique id. Is this information available via cookies or can we make REST API call to get this data?

 

Thank you,

Srikanth

  • There's a JavaScript object available called LITHIUM.CommunityJsonObject which has many details related to the current user and the page that they're on. For example:

     

    LITHIUM.CommunityJsonObject = {
      "User" : {
        "login" : "AdamN"
      },
      "Community" : {
        "id" : "lithium",
        "title" : "Lithosphere Community",
        "shortTitle" : "Lithosphere"
      },
      "Activity" : {
        "Results" : [ ]
      },
      "WebTracking" : {
        "path" : "Community:Lithosphere/Board:ask a question/Message:How do I get the currently logged-in user details like avatar, id and Name using Javascript?",
        "Activities" : { }
      },
      "CoreNode" : {
        "id" : "public-qa",
        "title" : "ask a question",
        "shortTitle" : "ask a question",
        "ancestors" : [
          {
            "id" : "lithium",
            "title" : "Lithosphere Community",
            "shortTitle" : "Lithosphere",
            "nodeType" : "Community"
          }
        ],
        "nodeType" : "Board"
      }
    };

     

    If this object doesn't have all the details that you need, you could always write out your own JavaScript object populated using a combination of FreeMarker and our REST API.

     

    I hope this helps!

     

  • AdamN's avatar
    AdamN
    Khoros Oracle

    There's a JavaScript object available called LITHIUM.CommunityJsonObject which has many details related to the current user and the page that they're on. For example:

     

    LITHIUM.CommunityJsonObject = {
      "User" : {
        "login" : "AdamN"
      },
      "Community" : {
        "id" : "lithium",
        "title" : "Lithosphere Community",
        "shortTitle" : "Lithosphere"
      },
      "Activity" : {
        "Results" : [ ]
      },
      "WebTracking" : {
        "path" : "Community:Lithosphere/Board:ask a question/Message:How do I get the currently logged-in user details like avatar, id and Name using Javascript?",
        "Activities" : { }
      },
      "CoreNode" : {
        "id" : "public-qa",
        "title" : "ask a question",
        "shortTitle" : "ask a question",
        "ancestors" : [
          {
            "id" : "lithium",
            "title" : "Lithosphere Community",
            "shortTitle" : "Lithosphere",
            "nodeType" : "Community"
          }
        ],
        "nodeType" : "Board"
      }
    };

     

    If this object doesn't have all the details that you need, you could always write out your own JavaScript object populated using a combination of FreeMarker and our REST API.

     

    I hope this helps!

     

    • sriray's avatar
      sriray
      Contributor
      Thank you Adam! The user field in this JSON object doesn't have all the fields we need... so, we will get the logged in user id from this object and then make a REST API call to fetch more data about the user.

      Thank you,
      Srikanth
      • AdamN's avatar
        AdamN
        Khoros Oracle
        The LITHIUM.CommunityJsonObject is essentially the front-end equivalent of the back-end FreeMarker context objects. It contains many of the common context object key/value pairs that you could then access from JavaScript code.

        It differs in that you can only use the details that have been explicitly exposed in this object. You can't have your pick of all the various FreeMarker context object values directly, as you would in a custom component or endpoint.

        That said, and as I mentioned above, you can expose any of these values you'd like in your own custom JavaScript variable. You could create a custom component and use Freemarker to create the necessary JavaScript code.