title: How to determine in Keycloak JS adapter whether user is logged in with SSO?
body: <p>To introduce my current setup, I have 3 components:</p>
  
  <ul>
  <li>Website with some static (CMS) and dynamic parts, a couple of Single Page Applications, everything works on domain <code>spa.com</code>. </li>
  <li>REST API, which provides any dynamic functionality (user profile) and is hosted on <code>api.com</code> and requires SSO tokens to work.</li>
  <li>SSO server is hosted under <code>sso.com</code> domain</li>
  </ul>
  
  <p>I'd like to determine in my SPA whether user has an active SSO session, so when I'm logged in but I do not have tokens, I can:</p>
  
  <ul>
  <li>do a redirection dance to get tokens</li>
  <li>do not allow user to use registration form</li>
  </ul>
  
  <p>I'm using Keycloak JS adapter, which uses iframe mechanism and checks for <code>KEYCLOAK_SESSION</code> cookie and communicates with host window via some flags:</p>
  
  <pre><code>var cookie = getCookie('KEYCLOAK_SESSION');
  if (cookie) {
      data.loggedIn = true;
      data.session = cookie;
  }
  </code></pre>
  
  <p>The problem is that I can not rely on this cookie to check if user is logged in because the cookie has 30 day expiration date, which is way more than an actual Keycloak session lasts, let's say 15 minutes.</p>
  
  <p>This cookie is being set when user gets authenticated and is not refreshed on each token update. I do not understand then it's purpose and the purpose of this flag.</p>
  
  <p>I'm also thinking about relying on local storage and keeping refresh &amp; access token there to determine if user is logged in, either by checking timeout or trying to refresh access token. BUT.. this also is not reliable, because user might have already authenticated using some other application.</p>
  
  <p>To add more context to the problem... one of my SPA applications is a registration wizard, which uses REST API to register user and receives <code>login_hint</code> in return. This <code>login_hint</code> can be used as a wildcard in SSO redirect to pass the challenge and log in immediately. </p>
  
  <p>However, if some other user is already logged in, we get in return session of this other user, not the one that should be logged in because of used <code>login_hint</code>.</p>