I'm trying to get my Cognito login to work. The problem is that it's not working and I'm not getting error messages from AWS, or XCode. I've implemented it according to the tutorial and the AWS sample code (maybe wrongly?). I've tried to understand how the code works by adding a couple of NSlog's within the AWS cognito functions so that I know whether they get excecuted, but they do not show up in my console either. Why are these function not being run without even sending an error? Is there something obvious that I'm forgetting?
Here's the essential parts of my code
// loginviewcontroller.h
@import AWSCognitoIdentityProvider;
@interface LoginViewController : UIViewController <AWSCognitoIdentityPasswordAuthentication>
@property (nonatomic, strong) NSString * usernameText;
@end
loginviewcontroller.m file:
// loginviewcontroller.m
@property (nonatomic, strong) AWSTaskCompletionSource<AWSCognitoIdentityPasswordAuthenticationDetails*> *passwordAuthenticationCompletion;
- (IBAction)signInPressed:(UIButton *)sender {
self.passwordAuthenticationCompletion.result = [[AWSCognitoIdentityPasswordAuthenticationDetails alloc] initWithUsername:self.userName.text password:self.password.text];
NSLog(@"button pressed");};
-(void) getPasswordAuthenticationDetails: (AWSCognitoIdentityPasswordAuthenticationInput *) authenticationInput passwordAuthenticationCompletionSource: (AWSTaskCompletionSource<AWSCognitoIdentityPasswordAuthenticationDetails *> *) passwordAuthenticationCompletionSource {
//keep a handle to the completion, you'll need it continue once you get the inputs from the end user
self.passwordAuthenticationCompletion = passwordAuthenticationCompletionSource;}
-(void) didCompletePasswordAuthenticationStepWithError:(NSError*) error {
NSLog(@"didCompletePasswordAuthenticationStepWithError");
dispatch_async(dispatch_get_main_queue(), ^{
//present error to end user
if(error){
NSLog(@"Error");
[[[UIAlertView alloc] initWithTitle:error.userInfo[@"__type"]
message:error.userInfo[@"message"]
delegate:nil
cancelButtonTitle:nil
otherButtonTitles:@"Ok", nil] show];
}else{
NSLog(@"Success");
//dismiss view controller
[self dismissViewControllerAnimated:YES completion:nil];
}
});
appdelegate.h
//appdelegate.h
@import AWSCognitoIdentityProvider;
@interface AppDelegate : UIResponder <UIApplicationDelegate, AWSCognitoIdentityInteractiveAuthenticationDelegate>
@property(nonatomic,strong) LoginViewController* LoginViewController;
appdelegate.m:
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
//setup AWS service config
AWSServiceConfiguration *serviceConfiguration = [[AWSServiceConfiguration alloc] initWithRegion:AWSRegionUSEast1 credentialsProvider:nil];
//create a pool
AWSCognitoIdentityUserPoolConfiguration *configuration = [[AWSCognitoIdentityUserPoolConfiguration alloc] initWithClientId:@"xxxxxx" clientSecret:@"xxxxxxx" poolId:@"us-east-1_xxxxxx"];
[AWSCognitoIdentityUserPool registerCognitoIdentityUserPoolWithConfiguration:serviceConfiguration userPoolConfiguration:configuration forKey:@"us-east-1_xxxxx"];
AWSCognitoIdentityUserPool *pool = [AWSCognitoIdentityUserPool CognitoIdentityUserPoolForKey:@"us-east-1_xxxxxx"];
pool.delegate = self;
return YES;
}
-(id<AWSCognitoIdentityPasswordAuthentication>) startPasswordAuthentication{
//implement code to instantiate and display login UI here
//return something that implements the AWSCognitoIdentityPasswordAuthentication protocol
NSLog(@"startpasswordauth AWS!");
return self.LoginViewController;
}
Also I did not understand this property line that's in the AWS github sample. The notation of *xxx I haven't see before. Here's the line:
@property (nonatomic, strong) AWSTaskCompletionSource<AWSCognitoIdentityPasswordAuthenticationDetails*> *passwordAuthenticationCompletion;
It's not mentioned in the tutorial, but without it
self.passwordAuthenticationCompletion.result = [[AWSCognitoIdentityPasswordAuthenticationDetails alloc] initWithUsername:self.userName.text password:self.password.text];
errors that the attribute is not found.
I have also tried this but delegate methods are not working.
Secondly, I tried with this code:
[AWSServiceManager.defaultServiceManager.defaultServiceConfiguration.credentialsProvider invalidateCachedTemporaryCredentials];
AWSCognitoIdentityUserPool *pool = [AWSCognitoIdentityUserPool CognitoIdentityUserPoolForKey:@"User"];
AppDelegate *delegate = (AppDelegate*)[[UIApplication sharedApplication] delegate];
self.user = [delegate.pool currentUser];
[[ self.user getSession:_userName.text password:_txtPassword.text validationData:nil ] continueWithSuccessBlock:^id _Nullable(AWSTask<AWSCognitoIdentityUserSession *> * _Nonnull task) {
//success, task.result has user session
dispatch_async(dispatch_get_main_queue(), ^{
if(task.error || task.isCancelled) {
[[[UIAlertView alloc] initWithTitle:task.error.userInfo[@"__type"]
message:task.error.userInfo[@"message"]
delegate:self
cancelButtonTitle:@"Ok"
otherButtonTitles:nil] show];
}else {
AWSCognitoIdentityUserSession *session = (AWSCognitoIdentityUserSession *) task.result;
NSString *tokenStr = [session.idToken tokenString];
[[NSUserDefaults standardUserDefaults]setObject:tokenStr forKey:@"token"];
[[NSUserDefaults standardUserDefaults]synchronize];
[self performSelectorOnMainThread:@selector(pushToDashbard) withObject:nil waitUntilDone:YES];
}});
return nil;
}]
If I am passing the right credentials then this is giving the token, with wrong credentials giving no error response.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.