4.2.1 Using user pools with AWS Mobile SDK
Here are the details about registering, confirming, and authenticating users using standard AWS Mobile SDK.
Creating an AWSCognitoIdentityUserPool Object
The following procedure describes how to create an AWSCognitoIdentityUserPool object to interact with.
1. Set up your service config
AWSServiceConfiguration *serviceConfiguration = [[AWSServiceConfiguration alloc] initWithRegion:AWSRegionUSEast1 credentialsProvider:nil];
let serviceConfiguration : AWSServiceConfiguration = AWSServiceConfiguration(region: .USEast1, credentialsProvider: nil)
2. Create a user pool configuration
//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 pool.signUp:password:userAttributes:validationData
to sign up a user.
AWSCognitoIdentityUserAttributeType * phone = [AWSCognitoIdentityUserAttributeType new];
phone.name = @"phone_number";
//phone number must be prefixed by country code
phone.value = @"+15555555555";
AWSCognitoIdentityUserAttributeType * email = [AWSCognitoIdentityUserAttributeType new];
email.name = @"email";
email.value = @"email@mydomain.com";
AWSCognitoIdentityUserPool *pool = [AWSCognitoIdentityUserPool CognitoIdentityUserPoolForKey:@"UserPool"];
//sign up the user
[[pool signUp:@"username" password:@"password" userAttributes:@[email,phone] validationData:nil] continueWithBlock:^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){
//need to confirm user using user.confirmUser:
}
}});
return nil;
}];
let pool : AWSCognitoIdentityUserPool = AWSCognitoIdentityUserPool(forKey: "UserPool")
let user : AWSCognitoIdentityUser = pool.getUser("username")
user.confirmSignUp("code", forceAliasCreation: true).continueWith { (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 okAction = UIAlertAction(title: "Ok", style: .default, handler: nil)
alertController.addAction(okAction)
self.present(alertController, animated: true, completion: nil)
} else {
let _ = strongSelf.navigationController?.popToRootViewController(animated: true)
}
})
Confirm Signup
Confirm a users' sign up with the confirmation code using user.confirmSignUp:forceAliasCreation:withBlock:
method
AWSCognitoIdentityUserPool *pool = [AWSCognitoIdentityUserPool CognitoIdentityUserPoolForKey:@"UserPool"];
AWSCognitoIdentityUser *user = [pool getUser:@"username"];
[user confirmSignUp:"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;
}];
let pool : AWSCognitoIdentityUserPool = AWSCognitoIdentityUserPool(forKey: "UserPool")
let user : AWSCognitoIdentityUser = pool.getUser("username")
user.confirmSignUp("code", forceAliasCreation: true).continueWith { (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 okAction = UIAlertAction(title: "Ok", style: .default, handler: nil)
alertController.addAction(okAction)
self.present(alertController, animated: true, completion: nil)
} else {
let _ = strongSelf.navigationController?.popToRootViewController(animated: true)
}
})
Sign in a User
Use getSession:password:validationData:scopes:
method to get a session username and password.
[[user getSession:@"username" password:@"password" validationData:nil scopes:nil] continueWithSuccessBlock:^id _Nullable(AWSTask<AWSCognitoIdentityUserSession *> * _Nonnull task) {
//success, task.result has user session
return nil;
}];
user.getSession("username", password: "password", validationData:nil, scopes:nil.continueWith { (task) -> Any? in
//success, task.result has user session
return nil;
})
Previous4.2 Authentication Using AWS Cognito Service WrapperNext4.2.2 Using user pools with Bayun AWSS3 wrapper 'SecureAuthentication'
Last updated