4.2.2 Using user pools with Bayun AWSS3 wrapper 'SecureAuthentication'
Here are the details about registering, confirming, and authenticating users using AWSS3 wrapper. User Registration, SignUp Confirmation, SignIn, SignOut needs to be done with SecureAuthentication instance.
Creating an AWSCognitoIdentityUserPool Object
The following procedure describes how to create an AWSCognitoIdentityUserPool object to interact with.
1. Set up your service config
There is no change in setting up Service Config and is same as using standard AWS Mobile SDK.
let serviceConfiguration : AWSServiceConfiguration = AWSServiceConfiguration(region: .USEast1, credentialsProvider: nil)
2. Create a user pool configuration.
There is no change in creating user pool configuration and is same as using standard AWS Mobile SDK.
//create a pool
AWSCognitoIdentityUserPoolConfiguration *configuration = [[AWSCognitoIdentityUserPoolConfiguration alloc] initWithClientId:@"CLIENT_ID" clientSecret:@"CLIENT_SECRET" poolId:@"USER_POOL_ID"];
[AWSCognitoIdentityUserPool registerCognitoIdentityUserPoolWithConfiguration:serviceConfiguration userPoolConfiguration:configuration forKey:@"UserPool"];
AWSCognitoIdentityUserPool *pool = [AWSCognitoIdentityUserPool CognitoIdentityUserPoolForKey:@"UserPool"];
let configuration : AWSCognitoIdentityUserPoolConfiguration = AWSCognitoIdentityUserPoolConfiguration(clientId: "CLIENT_ID", clientSecret: "CLIENT_SECRET", poolId: "USER_POOL_ID")
AWSCognitoIdentityUserPool.register(with: serviceConfiguration, userPoolConfiguration: configuration, forKey: "UserPool")
let pool : AWSCognitoIdentityUserPool = AWSCognitoIdentityUserPool(forKey: "UserPool")
Register a User
Use SecureAuthentication's signUp:username:password:userAttributes:userAttributes:validationData:withBlock: method to register a new user instead of relying on standard AWS Mobile SDK's signUp method.
AWSCognitoIdentityUserAttributeType * email = [AWSCognitoIdentityUserAttributeType new];
email.name = @"email";
email.value = @"email@mydomain.com";
AWSCognitoIdentityUserPool *pool = [AWSCognitoIdentityUserPool CognitoIdentityUserPoolForKey:@"UserPool"];
BOOL registerBayunWithPwd = true;
//sign up the user
[[SecureAuthentication sharedInstance] signUp:pool username:@"username" password:@"password" userAttributes:@[email] validationData:nil registerBayunWithPwd:registerBayunWithPwd withBlock:^id_Nullable(AWSTask<AWSCognitoIdentityUserPoolSignUpResponse *> * _Nonnull task) {
dispatch_async(dispatch_get_main_queue(), ^{
if(task.error){
[[[UIAlertView alloc] initWithTitle:task.error.userInfo[@"__type"]
message:task.error.userInfo[@"message"]
delegate:self
cancelButtonTitle:@"Ok"
otherButtonTitles:nil] show];
} else {
AWSCognitoIdentityUserPoolSignUpResponse * response = task.result;
if(!response.userConfirmed){
// handle the case where user has to confirm his identity
}
}});
return nil;
}];
let pool : AWSCognitoIdentityUserPool = AWSCognitoIdentityUserPool(forKey: "UserPool")
let email = AWSCognitoIdentityUserAttributeType()
email?.name = "email"
email?.value = "email@mydomain.com"
let registerBayunWithPwd = true
SecureAuthentication.sharedInstance().signUp(pool, username: "username", password: "password", userAttributes: [email!], validationData: nil, registerBayunWithPwd: registerBayunWithPwd){ (task) -> Any? in
DispatchQueue.main.async(execute: {
if let error = task.error as? NSError {
let alertController = UIAlertController(title: error.userInfo["__type"] as? String,
message: error.userInfo["message"] as? String,
preferredStyle: .alert)
let retryAction = UIAlertAction(title: "Retry", style: .default, handler: nil)
alertController.addAction(retryAction)
self?.present(alertController, animated: true, completion: nil)
} else if let result = task.result {
if (result.user.confirmedStatus != AWSCognitoIdentityUserStatus.confirmed) {
// handle the case where user has to confirm his identity
}
}
})
}
Confirm Signup
Confirm a users' sign up with the confirmation code using SecureAuthentication's confirmSignUpForUser:confirmationCode:forceAliasCreation:withBlock: method.