title: How can I check if user is already Logged in using aws cognito user pool anywhere in project?
body: <p>I am doing SSO(Single Sign On) in my application. I am making use of AWS cognito for it and user pool and Identity pool for users.</p>
  
  <p>I have setup user pool, identity pool and using hosted UI. </p>
  
  <p>When I am using Custom UI for login and calling below cognito function:</p>
  
  <pre><code>        cognitoUser.authenticateUser(authenticationDetails, {
            onSuccess: function (result) {
              var accessToken = result.getAccessToken().getJwtToken();
              AWS.config.region = config.region;
              AWS.config.credentials = new AWS.CognitoIdentityCredentials({
                  IdentityPoolId : config.IdentityPoolId, // your identity pool id here
                  Logins : {
                      'cognito-idp.&lt;region&gt;.amazonaws.com/&lt;pool-id&gt;': result.getIdToken().getJwtToken()
                  }
              });
  
              //refreshes credentials using AWS.CognitoIdentity.getCredentialsForIdentity()
              AWS.config.credentials.refresh((error) =&gt; {
              console.log("in refresh");
                  if (error) {
                      console.error(error);
                  } else {
                      console.log('Successfully logged!');
                  }
              });
          },
  
          onFailure: function(err) {
              console.log("in failure");
              alert(err.message || JSON.stringify(err));
          },
  
          mfaRequired: function(codeDeliveryDetails) {
              // MFA is required to complete user authentication.
              // Get the code from user and call
              console.log("in mfa required");
              cognitoUser.sendMFACode(mfaCode, this)
          }
        });
  </code></pre>
  
  <p>This works fine but I am not able to redirect user to routes on successful and failure.</p>
  
  <p>I want to use hosted UI.</p>
  
  <p>Now I am able to successfully authenticate user from hosted UI and redirect to URL provided in app client settings callback URL. And getting id_token, access_token in url. I am able to decode it and get username from that.</p>
  
  <p>But when I do:</p>
  
  <pre><code>var AmazonCognitoIdentity = require('amazon-cognito-identity-js');
  var userPool = new AmazonCognitoIdentity.CognitoUserPool(config.poolData);
  console.log("cog user: ", userPool.getCurrentUser());
  </code></pre>
  
  <p>I get null.</p>
  
  <p>Is there any way to get current logged in user So that I can identify if user is logged in or not and do next operations accordingly.</p>