[英]I cannot get the AWS Cognito credentials of a user (swiftUI)
我嘗試了幾種不同的方法,在這一點上我很困惑。 我只是希望能夠訪問用戶的 email 以在視圖中顯示它。 然而,我無法成功地呈現,更不用說檢索這些信息了。 這是我嘗試過的兩段代碼:
func getUsername() -> String? {
if(self.isAuth) {
return AWSMobileClient.default().username
} else {
return nil
}
}
和
func getUserEmail() -> String {
var returnValue = String()
AWSMobileClient.default().getUserAttributes { (attributes, error) in
if(error != nil){
print("ERROR: \(String(describing: error))")
}else{
if let attributesDict = attributes{
//print(attributesDict["email"])
self.name = attributesDict["name"]!
returnValue = attributesDict["name"]!
}
}
}
print("return value: \(returnValue)")
return returnValue
}
有誰知道為什么這不起作用?
登錄后試試這個:
AWSMobileClient.default().getTokens { (tokens, error) in
if let error = error {
print("error \(error)")
} else if let tokens = tokens {
let claims = tokens.idToken?.claims
print("claims \(claims)")
print("email? \(claims?["email"] as? String ?? "No email")")
}
}
我嘗試使用AWSMobileClient getUserAttributes
獲取用戶屬性但沒有成功。 還嘗試使用AWSCognitoIdentityPool getDetails
沒有成功。 可能是 AWS 移動客戶端的錯誤,但我們仍然可以從 id 令牌中獲取屬性,如上所示。
如果您使用的是托管 UI,請記住為您的托管 UI 提供正確的范圍,例如:
let hostedUIOptions = HostedUIOptions(scopes: ["openid", "email", "profile"], identityProvider: "Google")
這是因為它是一個異步 function 所以會返回但晚於 function 實際以該值結束的時間。 我發現這樣做的唯一方法是放置一個 while 循環,然后使用 if 條件。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.