User signing in using Amazon Cognito

446 Views Asked by At

I am using Amazon Cognito User Pools. I am trying to authenticate a user. First he/she will have to enter the phone number and password, there'll be a SMS sent to authenticate the user, upon Authenticating the user is expected to Sign in by giving the phonenumber and password.

1.) I want to popup the User registration Screen if the user is not registered with the app

2.) If the app has gone to the background I want the user to proceed using the app without having to login again. (At the moment the user requires to sign in all the time when they go to the background)

3.) If the user has registered but not authenticated the SMS validation then I want to redirect the user to the confirmation page

I have been stuck in this for nearly a week now. Can someone help me out.

In the app Delegate I have the following code. - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions

..

        AWSServiceConfiguration *serviceConfiguration = [[AWSServiceConfiguration alloc] initWithRegion:AWSRegionUSEast1 credentialsProvider:nil];



        //create a pool

        AWSCognitoIdentityUserPoolConfiguration *configuration = [[AWSCognitoIdentityUserPoolConfiguration alloc] initWithClientId:@"XXX" clientSecret:@"XXX" poolId:@"us-east-1_XXX"];

        [AWSCognitoIdentityUserPool registerCognitoIdentityUserPoolWithConfiguration:serviceConfiguration userPoolConfiguration:configuration forKey:@"UserPool"];

        //AWSCognitoIdentityUserPool *pool = [AWSCognitoIdentityUserPool CognitoIdentityUserPoolForKey:@"UserPool"];





        [AWSLogger defaultLogger].logLevel = AWSLogLevelVerbose;





        AWSCognitoIdentityUserPool *pool =[AWSCognitoIdentityUserPool CognitoIdentityUserPoolForKey:@"UserPool"];



        pool.delegate = self;

}



//set up password authentication ui to retrieve username and password from the user

-(id<AWSCognitoIdentityPasswordAuthentication>) startPasswordAuthentication {

//    

    if(!self.navController){

        self.navController = [[UIForViewController getStoryboard] instantiateViewControllerWithIdentifier:@"signupSegueID"];

    }

//    if(!self.signInViewController){

//        self.signInViewController = self.navigationController.viewControllers[0];

//    }



    dispatch_async(dispatch_get_main_queue(), ^{

        //rewind to login screen



        //display login screen if it isn't already visibile

        if(!(self.navController.isViewLoaded && self.navController.view.window))

        {

            [self.window.rootViewController presentViewController:self.navController animated:YES completion:nil];

        }

    });

    return nil;


}

Please note that startPasswordAuthentication is never executed unless I add the following code in the APPDELEGATES - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions

[[self.user getDetails] continueWithSuccessBlock:^id _Nullable(AWSTask<AWSCognitoIdentityUserGetDetailsResponse *> * _Nonnull task) {
    if (task.error) {
        //
        NSLog(@"Error ");
        [[[UIAlertView alloc] initWithTitle:task.error.userInfo[@"__type"]
                                    message:task.error.userInfo[@"message"]
                                   delegate:self
                          cancelButtonTitle:@"Ok"
                          otherButtonTitles:nil] show];
        return  nil;
    }
    AWSCognitoIdentityUserGetDetailsResponse *response = task.result;



    for (AWSCognitoIdentityUserAttributeType *attribute in response.userAttributes) {
        //print the user attributes
        NSLog(@"Attribute: %@ Value: %@", attribute.name, attribute.value);
    }
    return nil;
}];
1

There are 1 best solutions below

0
On

1) Cognito doesn't currently expose an API to check if a username exists already. You could work around this by calling a username specific API and acting based on the exception thrown back. If you're thinking more locally, you can check the session based on the username to see if someone is already signed in.

2) The RefreshTokens API is used to get a new access token once the old one has expired. Use the refresh token you get back in authenticating to facilitate this.

3) Being registered doesn't give you access. On user registration, you get no token, but are required to log in afterwards. This is already handled.