Amplify ios signin with custom flow

380 views Asked by At

I am trying to implement a custom signin flow using amplify ios library and cognito. The flow is based on this passwordless implementation https://github.com/mobilequickie/amplify-passwordless-sms-auth/tree/68152489152e1fc4c3185f4e5e3383639bdc8285, it works great on web, but I can't make it work on ios, I get the following error:

-------Sign In response---------
failure(AuthError: Incorrect username or password.
Recovery suggestion: Check whether the given values are correct and the user is authorized to perform the operation.)

Please find below the relevant code:

 public init(_ secureService: SecureServiceProtocol) {
        self.secureService = secureService
        self.token = secureService.get(tokenKey)
        self.authModel = secureService.get(authKey, type: AuthModel.self)
        
        do {
            let url = Bundle.main.url(forResource: "amplifyconfiguration", withExtension: "json")!
            let configuration = try AmplifyConfiguration(configurationFile: url)
            try Amplify.add(plugin: AWSCognitoAuthPlugin())
            try Amplify.configure(configuration)
            if authModel != nil {
                self.retrieveAuthData { _ in }
            }
        } catch {
            L.log(type: .error, message: error.localizedDescription)
            print(error)
            print(error.asAFError)
        }
    }

public func accessWith(_ phone: String, callback: @escaping AuthResultCallback) {     
    print(phone)
    Amplify.Auth.signIn(username: phone) { result in
      print("-------Sign In response---------")
      print(result)
    }
  }

configuration

{
  "auth": {
    "plugins": {
      "awsCognitoAuthPlugin": {
        "IdentityManager": {
          "Default": {}
        },
        "CredentialsProvider": {
          "CognitoIdentity": {
            "Default": {}
          }
        },
        "CognitoUserPool": {
          "Default": {
            "Region": "eu-west-2",
            "PoolId": "eu-west-2xxxxxx",
            "AppClientId": "5vmjioxxxxxxxxxx"
          }
        }
      },
      "Auth": {
        "Default": {
          "authenticationFlowType": "CUSTOM_AUTH"
        }
      }
    }
  }
}
1

There are 1 answers

0
Munib Hamza On

I have been facing the same issue and found this

The root cause for our issue was that the iOS Amplify library always sends an initial ChallengeName of SRP_A to the Cognito signIn call. However, the example "Define Auth Challenge trigger" is explicitly coded to fail any authentication calls where the ChallengeName is not CUSTOM_CHALLENGE.

So you need to port that same behavior with these lambdas. Because the Define lambda looks for the CUSTOM_CHALLENGE ChallengeName and fails requests that have a different ChallngeName, the logic is incompatible with the iOS Amplify libraries as-is, since they initially send SRP_A.

I was able to work around this by modifying the Define Auth Challenge lambda to respond with the CUSTOM_CHALLENGE name instead of failing outright, and that seems to have fixed up the iOS side.

You can use the lambda's from here