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.
AWSServiceConfiguration *serviceConfiguration = [[AWSServiceConfiguration alloc] initWithRegion:AWSRegionUSEast1 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"];
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;
}];
Confirm Signup
Confirm a users' sign up with the confirmation code using SecureAuthentication's confirmSignUpForUser:confirmationCode:forceAliasCreation:withBlock:
method.
AWSCognitoIdentityUserPool *pool = [AWSCognitoIdentityUserPool CognitoIdentityUserPoolForKey:@"UserPool"];
AWSCognitoIdentityUser *user = [pool getUser:@"username"];
[[SecureAuthentication sharedInstance] confirmSignUpForUser:user confirmationCode:"code" forceAliasCreation:YES withBlock:^id _Nullable(AWSTask<AWSCognitoIdentityUserConfirmSignUpResponse *> * _Nonnull task) {
if(task.error){
[[[UIAlertView alloc] initWithTitle:task.error.userInfo[@"__type"]
message:task.error.userInfo[@"message"]
delegate:nil
cancelButtonTitle:@"Ok"
otherButtonTitles:nil] show];
} else {
//return to signin screen
[self.navigationController popToRootViewControllerAnimated:YES];
}
return nil;
}];
Sign in a User
Use SecureAuthentication's signInPool:username:password:withBlock:
method to get a session username and password.
AWSCognitoIdentityUserPool *pool = [AWSCognitoIdentityUserPool CognitoIdentityUserPoolForKey:@"UserPool"];
[[SecureAuthentication sharedInstance] signInPool:pool username:@"username" password:@"password" withBlock:^id _Nullable(AWSTask<AWSCognitoIdentityUserSession*> * _Nonnull task) {
dispatch_async(dispatch_get_main_queue(), ^{
NSError *error = task.error;
if(error){
[[[UIAlertView alloc] initWithTitle:error.userInfo[@"__type"]
message:error.userInfo[@"message"]
delegate:nil
cancelButtonTitle:nil
otherButtonTitles:@"Retry", nil] show];
} else {
//signIn successful
}
});
return nil;
}];
Signout a user
Use SecureAuthentication's signout method to clear all tokens.
[[SecureAuthentication sharedInstance] signout:user];
Last updated
Was this helpful?