簡體   English   中英

AWS Amplify Auth.updateUserAttributes() 清除 localStorage/sessionStorage

[英]AWS Amplify Auth.updateUserAttributes() clear localStorage/sessionStorage

我正在編寫一個使用 AWS Cognito 作為身份驗證服務的基於 Web 的應用程序。 我使用“aws-amplify”來實現客戶端應用程序。

我正在使用 Auth.updateUserAttributes() 來更新 Cognito 上用戶的自定義屬性。 但是我發現調用這個 function 會清除所有與 Cognito 相關的項,包括 localStorage 中存儲的 idToken、refreshToken 和 accessToken。 因此,web 應用程序的行為類似於注銷。

這是關於Auth的配置的代碼

Amplify.configure({
  Auth: {
    userPoolId: process.env.REACT_APP_AWS_COGNITO_USER_POOL_ID, 
    region: process.env.REACT_APP_AWS_COGNITO_REGION,
    userPoolWebClientId: process.env.REACT_APP_AWS_COGNITO_APP_CLIENT_ID,
    storage: window.localStorage, 
    authenticationFlowType: 'CUSTOM_AUTH', 
  },
});

以及我為更新用戶屬性而編寫的代碼。 (我遵循了放大文檔https://docs.amplify.aws/lib/auth/manageusers/q/platform/js/#managing-user-attributes中的示例代碼)

let user = await Auth.currentAuthenticatedUser(); 
console.log(JSON.stringify(localStorage)); // it outputs the localstorage with idToken,
                                           // refreshToken, accessToken and other items
                                           // start with 'CognitoIdentityServiceProvider'
const result = await Auth.updateUserAttributes(user, {
    'custom:attributes_1': '123456789',
}); 
console.log(result); // OUTPUT: SUCCESS
console.log(JSON.stringify(localStorage)); // Only 'amplify-signin-with-hostedUI'. 
                                           // idToken, refreshToken, accessToken and
                                           // other items were gone. No key, no value.

在最后一行之后,我無法再與 web 頁面交互。 如果我刷新了 web 頁面,我發現我已經退出,必須重新登錄。 如果我將 Auth 的存儲從 localStorage 更改為 sessionStorage,它仍然是一樣的。

以下是我的問題:

  1. 這種行為正常嗎? Auth.updateUserAttributes() 是否會導致強制退出?
  2. 如果是真的,有什么辦法可以避免強制退出?
  3. 如果不是,我的代碼或配置有什么問題? 還是我應該為 Cognito 服務做一些特定的配置?

非常感謝!

好吧,我在閱讀 aws-amplify 的源代碼后想通了。

在 Auth.userUpdateAttributes 的調用之后,amplify 最終會調用 CognitoUser.refreshSession(refreshToken, callback, clientMetadata) ( https://github.com/aws-amplify/amplify-js/blob/f28918b1ca1111f98c231c8ed6ed6bccacezon9ad9eto-identitypackages/ma -js/src/CognitoUser.js#L1446 )。 在這個 function 中,放大向 Coginito 發送一個“InitiateAuth”請求。 如果發生“NotAuthorizedException”錯誤,則放大調用 clearCachedUser() 從 localStorage 中刪除我在問題中提到的所有內容。

Chrome瀏覽器的網絡工作監視器發生並報告“NotAuthorizedException”錯誤。 我認為它是在類似注銷行為之后生成的。 但是,它被觸發了,因為沒有將 deviceKey 傳遞給請求的參數。

所以整個故事是:

  1. 我在 Cognito 中設置了記住設備選項;
  2. 我使用“CUSTOM_AUTH”作為身份驗證流類型;
  3. 當用戶成功登錄到我的應用程序時,由於“CUSTOM_AUTH”身份驗證流類型,Cognito 沒有向客戶端提供 deviceKey。
  4. 當 Auth.userUpdateAttributes() 被調用時,CognitoUser.refreshSession() 被調用在它后面。 當它發送一個請求要求 Cognito 刷新令牌時,它沒有將 deviceKey 附加到 Cognito。 Cognito 以“NotAuthorizedException”錯誤拒絕了請求。 CognitoUser.refreshSession() 處理了錯誤並調用 clearCachedUser() 從 localStorage 中刪除存儲的令牌和其他信息。

我的最終解決方案是關閉記住設備選項,因為我必須根據應用程序的功能要求使用“CUSTOM_AUTH”作為身份驗證流類型。

根據https://aws.amazon.com/premiumsupport/knowledge-center/cognito-user-pool-remembered-devices/ ,記憶設備 function 僅在身份驗證流類型設置為“USER_SRP_AUTH”時有效。

暫無
暫無

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

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