Search code examples
iosswiftamazon-cognitocapacitor

AWSCognito Auth `getSession` does not fire callback in Swift


I'm somewhat new to Swift but I'm trying to adapt the AWSCognitoAuth to a Swift plugin for Capacitor. I've created a repo here: https://github.com/Kikketer/CapacitorTest

One of the biggest issues is that the callback is not being fired inside the iOS project:

cognitoAuth.getSession(self.bridge.viewController) { (session, err) in
// this is never called after I click the "sign in" button in Cognito
    if(err != nil) {
        NSLog(err.debugDescription)
        call.reject(err.debugDescription)
    } else {
        call.resolve([
            "accessToken": session?.accessToken?.tokenString ?? ""])
    }
}

Through all the setup I have described in the readme, this is the hightlight:

  1. I setup Cognito User pools, added a user, and created an app page for it
  2. I brought in the latest AWSCognito pod (2.9 as of this time)
  3. I wrote a plugin for Capacitor to bridge the gap, but in theory Capacitor has nothing much to do with this as it seems to be purely a Swift and native implementation issue: https://github.com/Kikketer/CapacitorTest/blob/master/ios/App/App/CognitoPlugin.swift#L12
  4. When I click the "sign in" button in the app, Cognito launches in the "special iOS browser" like it should but after I click the sign in button in that browser, it just kind of hangs there.
  5. The callback is never fired unless I click "Done" (which in that case it's an error, no actual token is returned).

I'm not seeing any obvious errors in any of the logs, so I'm not sure where to go next with this.

I realize there's quite a bit of setup to repeat this issue, but any help would be appreciated.

EDIT: I've done a little closer investigation and I'm getting a 405 response but no actual error message is displaying on the page:

The Server responded with a status of 405
https://[mydomain].auth.us-east-2.amazoncognito.com/login?response_type=code&client_id=[myclientid]&state=[a big guid]&redirect_uri=com.testthing.myapp://signin&scope=email%20openid&code_challenge=[a long code]&code_challenge_method=S256

Solution

  • So I figured it out after I was thinking about that 405 (method not allowed) error. What was happening is the POST was being sent to my application but my application wasn't handling a POST (because Capacitor handles GETs by default).

    It was a simple change to get this to work fully, updated the AppDelegate.swift file to handle the open url properly:

    func application(_ app: UIApplication, open url: URL, options: [UIApplication.OpenURLOptionsKey : Any] = [:]) -> Bool {
        // TODO parse the url and only launch cognito auth for ://signin and ://signout
        return AWSCognitoAuth(forKey: "AWSCognito").application(app, open: url, options: options)
    
        // This was here before
        //return CAPBridge.handleOpenUrl(url, options)
      }
    

    Hope this helps anyone else trying their hand at Cognito + Swift.