AWS Amplify + React Authentication Issue (Not getting JWT value after Auth.signIn() on Sign)

Viewed 13

hope you all are well. I am working on a React project with a serverless backend in AWS Amplify. Facing an issue with the authentication which is blocking me to use admin action queries. The problem is the following:

I am getting "Uncaught (in promise) No current user" or "Uncaught (in promise) The user is not authenticated" after using the Auth.SignIn() method. I need to get the JWT value from Auth.currentAuthenticatedUser() and sign in correctly in order to perform administrative tasks (which require authentication).

The code for this is:

 try {
      const user = await Auth.signIn(username, password);
      console.log(user); //prints the CognitoUser object.
      console.log(Auth.currentAuthenticatedUser()); //throws any of the two errors mentioned above.
    } catch (error) {
      console.log(error);
    }

When I implemented this previously, I was able to get the JWT token with the following method after authentication. That function is:

export const getJWT = async () => {
 try{
 const userCurrentSession = await Auth.currentSession();
  const accessToken = userCurrentSession.getAccessToken();
  const JWTvalue = accessToken.getJwtToken();
  return JWTvalue;
}catch(error){console.log(error)}
};

I would be highly obliged if someone helps in resolving this problem. Thanks in advance.

0 Answers
Related