iOS Password Reset

Ok, so I’m finally getting an iOS app to interact with DF services. Thought I’d start sharing a little bit to contribute to the community. My app has typical Registration/Login. On the Login screen I have username(email) and email UITextFields and a login button. I added a ‘Forgot Password’ button.

PASSWORD RESET:

Using the iOS sample app pattern (very well written, BTW - learning a lot) I did the following:

RESTEngine.h

  • (void)resetPassword:(NSString *)email success:(SuccessBlock)successBlock failure:(FailureBlock)failureBlock;

RESTEngine.m

  • (void)resetPassword:(NSString *)email success:(SuccessBlock)successBlock failure:(FailureBlock)failureBlock {

    id requestBody = @{@“email”: email};

      [self callApiWithPath:[Routing userWithResourceName:@"password?reset=true"] method:@"POST" queryParams:nil body:requestBody headerParams:self.headerParams success:successBlock failure:failureBlock];
    

}

In my LoginVC.m I connected an IBAction to my Password Reset button and added some field validation on the username UITextField. When field is validated, this is the code with FAIL/SUCCESS alerts I got to work:

else {

        [[RESTEngine sharedEngine]resetPassword:_fldUsername.text success:^(NSDictionary *response) {
            
            LOGI(@"Password reset success!");
            dispatch_async(dispatch_get_main_queue(),^ (void){
                UIAlertView *message=[[UIAlertView alloc]initWithTitle:NSLocalizedString(@"Success!", nil) message:[NSString stringWithFormat:NSLocalizedString(@"We sent a password reset link to %@.  Check your email and follow the password reset instructions.", nil),_fldUsername.text] delegate:nil cancelButtonTitle:NSLocalizedString(@"Ok", nil) otherButtonTitles: nil];
                [message show];
            });
            
        } failure:^(NSError *error) {
            LOGI(@"Error logging in user: %@",error);
            dispatch_async(dispatch_get_main_queue(),^ (void){
                UIAlertView *message= [[UIAlertView alloc]initWithTitle:@"" message:error.errorMessage delegate:nil cancelButtonTitle:NSLocalizedString(@"Ok", nil) otherButtonTitles: nil];
                [message show];
            });
        }];
        
    }

User receives a link just as planned.

Couple of notes:

  1. Make sure you set up the User Role to your Mobile App Service, unless that is you have set up a dashboard or other service you want them to access. If they don’t have a Role or Service assigned, as soon as they reset their password they get logged into the DSP.

  2. TIP: If testing, make sure you log out of your DSP console before clicking on the email link, or else you will end up initializing your ADMIN session again and not seeing the proper user work flow on password reset.

Hope this helps - I’m a self-taught old man for coding/iOS, so be nice and offer alternatives if the code is not hipster optimized…

1 Like