我将与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"
}
答案 0 :(得分:1)
您必须在根文件夹的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
答案 1 :(得分:1)
您修改后的fetchCurrentAuthSession碰到了catch块,但是您没有做任何事情来显示错误。 如果您添加类似 print(“内部捕获:(错误)”) 在catch内,您应该看到错误,很可能是您再次调用Amplify.configure()。 您的awsconfiguration.json和amplifyconfiguration.json文件中包含什么(在发布之前屏蔽掉poolId,appClientID和区域)?
答案 2 :(得分:1)
这是与较旧版本的Amplify CLI相关的常见错误。
1。使用npm install -g @aws-amplify/cli
在终端中对其进行更新。
2。完成后,请通过amplify remove auth
从项目目录中删除Amplify Auth。
3。输入amplify push
以配置后端中的更改。
4。现在,您可以成功地将auth添加回您的项目amplify add auth
5。输入amplify push
以配置后端中的更改。
6。现在运行您的项目,看看会发生什么;)