swiftaws-amplifyaws-amplify-cli

Error: Extra trailing closure passed in call


import Amplify

class Auth {
    var email: String?
    var password: String?

    // Login
    func login(email: String, password: String, completion: @escaping (String?) -> Void) {
        Amplify.Auth.signIn(username: email, password: password) { result in // Error: Extra trailing closure passed in call
            switch result {
            case .success:
                completion("Success")
            case .failure(let error):
                completion("Login failed: \(error.errorDescription)")
            }
        }
    }

    // Forgot Password
    func forgotPassword(email: String, completion: @escaping (String?) -> Void) {
        Amplify.Auth.resetPassword(for: email) { result in //Error: Extra trailing closure passed in call
            switch result {
            case .success:
                completion("Password reset code sent to \(email).")
            case .failure(let error):
                completion("Password reset failed: \(error.errorDescription)")
            }
        }
    }

Error:

Extra trailing closure passed in call.

I’m working on an iOS app using Amplify and AWSCognitoAuthPlugin for authentication. I'm using UIKit for development and have created an authentication class to handle login, forgot password, verify email, and resend verification code. However, I’m encountering an error when trying to perform authentication methods like signIn, resetPassword, and others.

enter image description here

I tried to use "AWSCognitoIdentityProviderService" but I want to use Amplify completely for my IOS App


Solution

  • It looks like it was rewritten to use async/await. I found this sample code using the function. It does not take a closure, but instead returns a signInResult.

    func signIn(username: String, password: String) async {
        do {
            let signInResult = try await Amplify.Auth.signIn(
                username: username, 
                password: password
            )
            if signInResult.isSignedIn {
                print("Sign in succeeded")
            }
        } catch let error as AuthError {
            print("Sign in failed \(error)")
        } catch {
            print("Unexpected error: \(error)")
        }
    }