如何使用 AWS Amplify 检查当前用户会话?

How to check current user session using AWS Amplify?

我正在关注 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。 解决这个问题的正确方法是什么?这是他们文档的 link 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"
}

您必须在根文件夹的 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
}

最重要的是,

you must check whether the user's email is confirmed ?.

如果电子邮件未被确认,则 isSignedIn 将始终为 false。您可以在 userPool

中检查特定的 emailId

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

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

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

2.Once 已完成,通过 amplify remove auth.

从项目目录中删除 Amplify Auth

3.Enter amplify push 在后端配置更改。

4.Now 您可以成功将 auth 添加回您的项目 amplify add auth

5.Enter amplify push 在后端配置更改。

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