Search code examples
iosswiftamazon-web-servicesaws-sdkfacebook-sdk-4.0

ios swift AWS cognito and Facebook Authentication


I am trying to use Facebook credentials to login on to AWS. I have set up a federate identity using Facebook as my social identity provider on Amazon Cognito but for some reason it does not authenticate. The Docs on Amazon are not up to date so I am not sure what to do.

Below is some swift code from the ios app:

let token = FBSDKAccessToken.currentAccessToken().tokenString

    let provider = MyProvider(tokens: [AWSIdentityProviderFacebook: token])


    let credentialsProvider = AWSCognitoCredentialsProvider(regionType: region, identityPoolId: poolId, identityProviderManager: provider)


    let configuration = AWSServiceConfiguration(region: region, credentialsProvider:credentialsProvider)

    AWSServiceManager.defaultServiceManager().defaultServiceConfiguration = configuration

    let id = credentialsProvider.getIdentityId()

Here is the MyProvider class:

import Foundation
import AWSCognitoIdentityProvider

class MyProvider:NSObject, AWSIdentityProviderManager{
     var tokens : [NSString : NSString]?
     init(tokens: [NSString : NSString]) 
     {
         self.tokens = tokens
     }
    @objc func logins() -> AWSTask {

        return AWSTask(result: tokens)
    }
}

I am able to get a token from Facebook and login but when I pass this token to Cognito I get an error like "NotAuthorizedException" "Token is not from a supported provider of this identity pool." The pool ID and region I pass to AWSCognitoCredentialsProvider match to the ones on Amazon Cognito, the method I defined in MyProvider gets called. If anyone has got it to work please share what you have did that got it working. I will appreciate any advice and help!


Solution

  • Solved my problem somehow. All I did was deleted my identity pool and Facebook app and created new ones. It is now working fine.