gpt4 book ai didi

swift - 将来自 Cognito 的用户信息与 AWS Amplify GraphQL 相关联

转载 作者:行者123 更新时间:2023-12-04 12:59:11 32 4
gpt4 key购买 nike

我在 xcode 11.4,Swift 4 上。目标是:

  • 在 Cognito 用户池中注册一个新用户,然后使用 Amplify GraphQL 保存关联的用户记录。
  • 使用 Cognito 用户池登录后,CRUD 用户的记录。

  • 问题是我不知道如何将 Cognito 与 Amplify GraphQL 相关联。例如,在 Google Firebase 身份验证和 Firestore 中,我会得到一个唯一的用户 ID UID注册后,我会在 Firestore中创建一个关联的用户记录。用这样的键 UID .然后在用户登录/身份验证时,我可以得到这个 UID从 firebase auth 并在 firestore 中找到相关记录。

    目前使用 AWS 堆栈,我在 schema.graphql 中创建了一个用户模型作为:
    type User @model @auth(rules: [{ allow: owner, ownerField: "id", operations: [create, update, delete]}]){
    id: ID!
    firstName : String
    lastName : String
    handle : String
    email : String!
    }

    这样只有经过身份验证的用户才能创建、更新和删除。接下来在 SignUpController 的某个地方我创建一个新用户:
    AWSMobileClient.default().signUp( username: email
    , password: password
    , userAttributes: ["email": email]) { (signUpResult, error) in
    if let signUpResult = signUpResult {

    switch(signUpResult.signUpConfirmationState) {
    case .confirmed:
    self.showAlert(msg: "You already have an account. Please go back and press log in")
    case .unconfirmed:
    break
    case .unknown:
    self.showAlert(msg: "Network error")
    }
    } else if let error = error { ... }

    然后用代码确认用户:
    AWSMobileClient.default().confirmSignUp(username: email, confirmationCode: code) { (signUpResult, error) in
    if let signUpResult = signUpResult {
    switch(signUpResult.signUpConfirmationState) {
    case .confirmed:
    // This is where I need to create an associated user account
    break
    case .unconfirmed:
    self.showAlert(title: "Error", msg: "User is not confirmed and needs verification via \(signUpResult.codeDeliveryDetails!.deliveryMedium) sent at \(signUpResult.codeDeliveryDetails!.destination!)")
    case .unknown:
    self.showAlert(title: "Error", msg: "Network error")
    }
    } else { //if let error = error {
    self.showAlert(title: "Error", msg: "Network error")
    }

    现在我在 case .confirmed 中的解决方案是立即登录,然后获取用户的 client token通过:
    class CognitoPoolProvider : AWSCognitoUserPoolsAuthProviderAsync {

    /// this token may not be what you want ...
    func getLatestAuthToken(_ callback: @escaping (String?, Error?) -> Void) {

    AWSMobileClient.default().getTokens { (token, error) in
    if let error = error {
    callback(nil,error)
    }
    callback(token?.accessToken?.tokenString, error)
    }
    }
    }

    结果证明这是错误的解决方案,因为用户的客户端 token 一直在变化。

    总的来说,这是一个标准的 hello-world 问题,应该有 AWS 提供的标准开箱即用解决方案。我搜索了文档和 github,但找不到满意的答案。

    最佳答案

    正确的方法是不要相信客户端从 Cognito 创建关联用户信息,您必须在服务器端进行。

    您应该为 Cognito 创建一个新的 Lambda Post Confirmation Trigger 并对其进行编码以创建关联账户。您可以使用 event.userName或创建自定义属性 uuid 类型喜欢 custom:id链接您的关联帐户。

    引用:https://docs.aws.amazon.com/cognito/latest/developerguide/user-pool-lambda-post-confirmation.html

    关于swift - 将来自 Cognito 的用户信息与 AWS Amplify GraphQL 相关联,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61310638/

    32 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com