Getting Authentication Error while configuring AWS DynamoDB in mobile application

Viewed 423

So, I am attempting to use Amazon's DynamoDB Object Mapper to map code into a preexisting database table that already includes a primary key and specific attributes. I am developing an IOS application.

As I attempt to use the Object Mapper, I am receiving the following error:

The request failed. Error: Error Domain=com.amazonaws.AWSCognitoIdentityErrorDomain Code=0 "(null)" UserInfo={__type=com.amazon.coral.service#MissingAuthenticationTokenException, message=Request is missing Authentication Token}

This error is quite odd to me because whenever a user signs into my application using their email address, the user shows up correctly in both the user pool and in the identity pool. This seems to tell me that there should be no issue with the pool ID, identity pool ID, and app client ID that I have linked between Amazon Cognito and my application.

Here is the code I have used to configure the user pool and identity pool (which is configured in the app delegate):

    func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
    // Override point for customization after application launch.

    // Setup Service Configuration
    let serviceConfiguration = AWSServiceConfiguration(region: .USEast1, credentialsProvider: nil)
    AWSServiceManager.default().defaultServiceConfiguration = serviceConfiguration

    // Link app to AWS user pool
    let userPoolConfiguration = AWSCognitoIdentityUserPoolConfiguration(clientId: CLIENT_ID, clientSecret: nil, poolId: USER_POOL_ID)
    AWSCognitoIdentityUserPool.register(with: serviceConfiguration, userPoolConfiguration: userPoolConfiguration, forKey: USER_POOL_ID)
    pool = AWSCognitoIdentityUserPool(forKey: USER_POOL_ID)

    // Configure the identity pool to connect to all AWS resources
    credentialsProvider = AWSCognitoCredentialsProvider(regionType: .USEast1, identityPoolId: IDENTITY_POOL_ID, identityProviderManager: pool)
    let configuration = AWSServiceConfiguration(region: .USEast1, credentialsProvider: credentialsProvider)
    AWSServiceManager.default().defaultServiceConfiguration = configuration

    return true
}

I am receiving the error when I attempt to use the save function of the DynamoDBObjectMapper below:

    @IBAction func login(_ sender: Any) {

    guard let confirmationCodeValue = confirmationCode.text, !confirmationCodeValue.isEmpty else {
        let alert = UIAlertController(title: "Confirmation code missing.",
                                      message: "Please enter a valid confirmation code.",
                                      preferredStyle: .alert)
        alert.addAction(UIAlertAction(title: "OK", style: .default, handler: nil))
        self.present(alert, animated: true, completion:nil)
        return
    }

    self.user?.confirmSignUp(confirmationCodeValue, forceAliasCreation: true).continueWith(block: {[weak self] (task: AWSTask) -> AnyObject? in
        guard let strongSelf = self else { return nil }
        DispatchQueue.main.async(execute: {
            if let error = task.error as NSError? {
                let alert = UIAlertController(title: "Error",
                                              message:error.userInfo["message"] as? String,
                                              preferredStyle: .alert)
                alert.addAction(UIAlertAction(title: "OK", style: .default, handler: nil))
                strongSelf.present(alert, animated: true, completion:nil)
            }
            else {
                let dynamoDBObjectMapper = AWSDynamoDBObjectMapper.default()
                let artistInformation = ArtistInformation()

                artistInformation?.UserID = self?.userID
                artistInformation?.ArtistName = UserDefaults.standard.string(forKey: "ArtistName")
                artistInformation?.ProfileType = UserDefaults.standard.string(forKey: "ProfileType")
                artistInformation?.CityYouRep = UserDefaults.standard.string(forKey: "CityYouRep")
                artistInformation?.Genre = UserDefaults.standard.string(forKey: "Genre")

                dynamoDBObjectMapper.save(artistInformation!).continueWith(block: { (task: AWSTask<AnyObject>!) -> Any? in
                    if let error = task.error as NSError? {
                        print("The request failed. Error: \(error)")
                    } else {
                        let alert = UIAlertController(title: "Registration Complete",
                                                      message: "Registration was successful.",
                                                      preferredStyle: .alert)

                        alert.addAction(UIAlertAction(title: "OK", style: .default, handler: {(action) in
                            let _ = self?.navigationController?.popToRootViewController(animated: true)
                        }))
                        strongSelf.present(alert, animated: true, completion: nil)
                    }
                    return nil
                })

To avoid confusion, the "ArtistInformation" class is one that is made to link to the existing table in DynamoDB:

class ArtistInformation: AWSDynamoDBObjectModel, AWSDynamoDBModeling {

var UserID: String?
var ArtistName: String?
var ProfileType: String?
var CityYouRep: String?
var Genre: String?

class func dynamoDBTableName() -> String {
    return "Artist_Information"
}

class func hashKeyAttribute() -> String {
    return "userID"
}

}

What seems to be the cause of this error? Please let me know if you need any more information.

0 Answers
Related