Cannot Authenticate User for Aws Appsync with Swift Sdk

How to authenicate AWS Appsync in iOS (Swift)

AWS SDK for iOS - AppSync solves your use-case. You can check out the documentation here: https://docs.aws.amazon.com/appsync/latest/devguide/building-a-client-app-ios.html. You can check out the source code here: https://github.com/awslabs/aws-mobile-appsync-sdk-ios. There is a starter app which will help you onboard quickly: https://github.com/aws-samples/aws-mobile-appsync-events-starter-ios.

import UIKit
import AWSAppSync

@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {

var window: UIWindow?
var appSyncClient: AWSAppSyncClient?

func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
// Set up Amazon Cognito credentials
let credentialsProvider = AWSCognitoCredentialsProvider(regionType: CognitoIdentityRegion,
identityPoolId: CognitoIdentityPoolId)
// You can choose your database location, accessible by the SDK
let databaseURL = URL(fileURLWithPath:NSTemporaryDirectory()).appendingPathComponent(database_name)

do {
// Initialize the AWS AppSync configuration
let appSyncConfig = try AWSAppSyncClientConfiguration(url: AppSyncEndpointURL,
serviceRegion: AppSyncRegion,
credentialsProvider: credentialsProvider,
databaseURL:databaseURL)
// Initialize the AWS AppSync client
appSyncClient = try AWSAppSyncClient(appSyncConfig: appSyncConfig)
// Set id as the cache key for objects
appSyncClient?.apolloClient?.cacheKeyForObject = { $0["id"] }
} catch {
print("Error initializing appsync client. \(error)")
}
return true
}

// ... other intercept methods
}

Appsync return 401 errors when connecting with cognito

Ok the issue was: If you're using the code above, you need to set your appsync to authenticate via IAM (not Cognito). This will also require changes to your resolvers since the parameters passed to the identity object are different for IAM vs Cognito.

This is confusing because you're using Cognito (both a user pool and a federated identity user pool), but do NOT chose Cognito.

AWS Appsync - unable to find User Pool Client ID

The User Pool Client ID is available from the Amazon Cognito User Pools console in the App Clients section.

Sample Image

You should create an App Client if it doesn't already exist. Make sure to uncheck the "Generate client secret" box.

Sample Image



Related Topics



Leave a reply



Submit