簡體   English   中英

AWS Amplify + React 身份驗證問題(在 Auth.signIn() 登錄后未獲得 JWT 值)

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

希望你們都好。 我正在 AWS Amplify 中使用無服務器后端開發一個 React 項目。 面臨身份驗證問題,阻止我使用管理員操作查詢。 問題如下:

使用 Auth.SignIn() 方法后,我收到“未捕獲(承諾)沒有當前用戶”或“未捕獲(承諾)用戶未通過身份驗證”。 我需要從 Auth.currentAuthenticatedUser() 獲取 JWT 值並正確登錄以執行管理任務(需要身份驗證)。

代碼是:

 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);
    }

當我之前實現這個時,我能夠在身份驗證后使用以下方法獲取 JWT 令牌。 function 是:

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)}
};

如果有人幫助解決這個問題,我將非常感激。 提前致謝。

const user = await Auth.currentAuthenticatedUser(); user.signInUserSession.getAccessToken().getJwtToken();

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM