Error initializing appsync client. invalidAuthConfiguration("AWSCognitoUserPoolsAuthProvider cannot be nil.")

611 views Asked by At

I am using AWS amplify to build the backend of my ios application. I am having an issue initializing appsync client. I have added Auth to the backend and have signed in but I am getting the following error in the console:

CONSOLE:

Error initializing appsync client. invalidAuthConfiguration("AWSCognitoUserPoolsAuthProvider cannot be nil.")

In AppDelegate:

// CONFIGURE AWSAppSync
        do {
            // You can choose your database location if you wish, or use the default
            let cacheConfiguration = try AWSAppSyncCacheConfiguration()
            
            // AppSync configuration & client initialization
            let appSyncConfig = try AWSAppSyncClientConfiguration(appSyncServiceConfig: AWSAppSyncServiceConfig(), cacheConfiguration: cacheConfiguration)
            
            appSyncClient = try AWSAppSyncClient(appSyncConfig: appSyncConfig)
            print("appSyncClient initialise success \n")
        } catch {
            print("Error initializing appsync client. \(error)")
        }

I am just guessing here but 'appSyncConfig' variable should be:

let appSyncConfig = try AWSAppSyncClientConfiguration(appSyncServiceConfig: AWSAppSyncServiceConfig.init(), cacheConfiguration: cacheConfiguration, userPoolsAuthProvider: AWSCognitoUserPoolsAuthProvider?)

But how do i get AWSCognitoUserPoolsAuthProvider????

1

There are 1 answers

0
Mohamad Ghaith Alzin On

In configuring the AWS AppSync you are missing a step which is adding the class to get the tokens from Cognito User Pools. Of course, here I assume that you have chosen Cognito User Pool as your default authorization type!

Please let me know if you have any more concerns about to setup amplify GraphQL or whatever!

import UIKit
import AWSAppSync
import AWSMobileClient

class MyCognitoUserPoolsAuthProvider : AWSCognitoUserPoolsAuthProviderAsync {
    func getLatestAuthToken(_ callback: @escaping (String?, Error?) -> Void) {
        AWSMobileClient.default().getTokens { (tokens, error) in
            if error != nil {
                callback(nil, error)
            } else {
                callback(tokens?.idToken?.tokenString, nil)
            }
        }
    }
}
@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate, UISplitViewControllerDelegate {
    var window: UIWindow?
   
    var appSyncClient: AWSAppSyncClient?
    
    func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
        //// Override point for customization after application launch.
        self.initializeAppSync()
        return true
    }
    
    func initializeAppSync() {
        do {
            // You can choose the directory in which AppSync stores its persistent cache databases
            let cacheConfiguration = try AWSAppSyncCacheConfiguration()
            
            // Initialize the AWS AppSync configuration
            let appSyncConfig = try AWSAppSyncClientConfiguration(appSyncServiceConfig:
                                                                    AWSAppSyncServiceConfig(),
                                                                  userPoolsAuthProvider: MyCognitoUserPoolsAuthProvider(),
                                                                  cacheConfiguration: cacheConfiguration)
            
            // Initialize the AWS AppSync client
            appSyncClient = try AWSAppSyncClient(appSyncConfig: appSyncConfig)
            print("appSyncClient initialise success.")
        } catch {
            print("Error initializing appsync client. \(error)")
        }
    }
    
}