0

Do you know any syncronous way to get IsAuthenticated status for currentRequest in client side via javascript?

I can get current user via client model, but it will be asynchronous.

this.authenticationPending = true;
this.isAuthenticated = false;

ExecuteOrDelayUntilScriptLoaded(Function.createDelegate(this, function() {
   var context = new SP.ClientContext.get_current();
   this.website = context.get_web();
   this.currentUser = website.get_currentUser();
   context.load(currentUser);
   context.executeQueryAsync(
      Function.createDelegate(this, function ()
          {
             this.authenticationPending = false;
             this.isAuthenticated = this.currentUser.get_id() > 0;
          }),
          Function.createDelegate(this, function ()
          {
             this.authenticationPending = false;
             this.isAuthenticated = false;
          }));
}), "sp.js");

After that I can get status, but I need to get status synchronous.

3

I'm assuming you're on a standard SharePoint page -- if so, you can use the _spPageContextInfo object's userId and userLoginName properties.

They come along for the ride when the page is rendered.

From my browser dev tools:

>_spPageContextInfo.userId
1337
>_spPageContextInfo.userLoginName
"somebody@somecompany.onmicrosoft.com"

If you're dealing with an anonymous user those two properties will both be undefined so you can make your checks accordingly.

2
  • The second way is using spsecuritytrimmedcontrol to surround client script block to initialize javascript variable with authenticated status. – asolovyov Dec 6 '14 at 20:47
  • Heads up that if the page is currently anonymous but you have NTLM setup and the user's browser can authenticate, the userId attribute will get populated when you call _spPageContextInfo.userId. This may lead to unexpected/undesired behavior. – Lane Goolsby Apr 20 '15 at 15:25

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.