我将跟随AWS放大器文档,并给出用于检查当前auth会话的示例代码是
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。解决这个问题的正确方法是什么?这是指向他们的docs 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"
}发布于 2020-06-04 22:46:36
这是一个常见的错误,与更早版本的放大器CLI相关联。
1.在终端中使用npm install -g @aws-amplify/cli进行更新。
2.完成后,通过amplify remove auth从项目目录中移除扩增Auth。
3.输入amplify push以配置后端中的更改。
4.现在您可以成功地将auth添加回您的项目amplify add auth
5.输入amplify push以配置后端中的更改。
6.现在运行您的项目,看看会发生什么;)
发布于 2020-06-04 07:32:19
您必须在根文件夹中的AmplifyPlugin中导入AppDelegate.swift包。
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将永远是错误的。您可以在emailId中检查特定的userPool
发布于 2020-06-04 12:36:21
修改后的fetchCurrentAuthSession击中了catch块,但是您没有做任何事情来显示错误。如果在catch中添加了诸如print("Inside catch:( error )")之类的内容,那么您应该会看到这个错误,这很可能是您再次调用Amplify.configure()。您的awsconfiguration.json和amplifyconfiguration.json文件中有哪些内容(在发布之前屏蔽您的poolId、appClientID和区域)?
https://stackoverflow.com/questions/62141372
复制相似问题