【问题标题】:How to check current user session using AWS Amplify?如何使用 AWS Amplify 检查当前用户会话?
【发布时间】:2020-09-20 07:03:25
【问题描述】:

我正在关注 AWS Amplify 文档,用于检查当前身份验证会话的示例代码是

 func fetchCurrentAuthSession() {
    _ = Amplify.Auth.fetchAuthSession { (result) in
        switch result {
        case .success(let session):
            print("Is user signed in - \(session.isSignedIn)")
        case .failure(let error):
            print("Fetch session failed with error \(error)")
        }
    }
}

在 viewDidLoad 中调用此函数后,我收到此错误 线程 1:致命错误:未配置身份验证类别。在使用类别上的任何方法之前调用 Amplify.configure()。 所以我然后将代码更改为这个

      func fetchCurrentAuthSession() {
            do {
                try Amplify.configure()
            _ = Amplify.Auth.fetchAuthSession { (result) in
                switch result {
                case .success(let session):
                    print("Is user signed in - \(session.isSignedIn)")
                case .failure(let error):
                    print("Fetch session failed with error \(error)")
                }
            }
        }catch{

        }

    }

它运行没有错误,但 authSession 没有打印出来。 解决此问题的正确方法是什么?这是他们文档的链接https://docs.amplify.aws/lib/auth/getting-started/q/platform/ios#check-the-current-auth-session

这是我的 awsconfiguration.json

{
    "UserAgent": "aws-amplify/cli",
    "Version": "0.1.0",
    "IdentityManager": {
        "Default": {}
    },
    "CredentialsProvider": {
        "CognitoIdentity": {
            "Default": {
                "PoolId": "removed",
                "Region": "removed"
            }
        }
    },
    "CognitoUserPool": {
        "Default": {
            "PoolId": "removed",
            "AppClientId": "removed",
            "AppClientSecret": "removed",
            "Region": "removed"
        }
    },
    "FacebookSignIn": {
        "AppId": "removed",
        "Permissions": "public_profile"
    },
    "Auth": {
        "Default": {
            "authenticationFlowType": "USER_SRP_AUTH"
        }
    }
}

这是我的 amplifyconfiguration.json

{
    "UserAgent": "aws-amplify-cli/2.0",
    "Version": "1.0"
}

【问题讨论】:

    标签: swift aws-amplify


    【解决方案1】:

    这是与旧版本 Amplify CLI 相关的常见错误。

    1.在终端中使用 npm install -g @aws-amplify/cli 更新它。

    2.完成后,通过amplify remove auth从项目目录中删除 Amplify Auth。

    3.输入amplify push 以在您的后端配置更改。

    4.现在您可以成功将身份验证添加回您的项目amplify add auth

    5.输入amplify push 以在您的后端配置更改。

    6.现在运行你的项目,看看会发生什么;)

    【讨论】:

      【解决方案2】:

      您修改后的 fetchCurrentAuthSession 正在命中您的 catch 块,但您没有做任何事情来显示错误。 如果你添加类似的东西 print("内部捕获:(错误)") 在 catch 中,您应该会看到错误,可能是您再次调用 Amplify.configure()。 您的 awsconfiguration.json 和 amplifyconfiguration.json 文件中有什么(在发布之前屏蔽掉您的 poolId、appClientID 和区域)?

      【讨论】:

      • 我已经编辑了我的问题以显示我的 awsconfiguration.json 和 amplifyconfiguration.json 文件。
      【解决方案3】:

      您必须在根文件夹的 AppDelegate.swift 中导入 Amplify 和 AmplifyPlugin 包。

      import UIKit
      import Amplify
      import AmplifyPlugins
      

      然后添加如下函数

         func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
          // Override point for customization after application launch.
           do {
                  try Amplify.add(plugin: AWSCognitoAuthPlugin())
                  try Amplify.configure()
                  print("Amplify configured with auth plugin")
              } catch {
                  print("An error occurred setting up Amplify: \(error)")
              }
          return true
      }
      

      最重要的是,

      您必须检查用户的电子邮件是否已确认?

      如果未确认电子邮件,则 isSignedIn 将始终为 false。您可以在 userPool 中检查特定的 emailId

      【讨论】:

      • 我忘了提到当我跟随文档时,我已经将该代码添加到我的应用程序委托中。我还没有注册用户,我只是想看看 fetchCurrentAuthSession 函数打印 false,因为没有用户登录。
      猜你喜欢
      • 1970-01-01
      • 2018-07-21
      • 1970-01-01
      • 2019-05-06
      • 2019-07-09
      • 2021-05-04
      • 1970-01-01
      • 2021-08-19
      • 2021-02-10
      相关资源
      最近更新 更多