2017-02-17 42 views
15

W mojej aplikacji na iOS potrzebuję, aby moi użytkownicy mogli odzyskać/zresetować swoje hasła. Używam Drupal iOS SDK do zarządzania loginem użytkownika. Wszystko działa, ale staram się dowiedzieć, jak opublikować adres e-mail użytkownika w punkcie końcowym usługi, aby wywołać wiadomość e-mail odzyskiwania hasła. E.g. użytkownik wprowadza wiadomość e-mail do UITextField i stuka przycisk wysyłania. Jednak nie ma na to żadnej dokumentacji?iOS - Password odzyskiwania hasła e-mail od Drupal

Kod jest następujący - Po prostu nie jestem pewien, jaką metodę powinienem wprowadzić do mojego sendButton? DIOSUser? DIOSSession?

DIOSUser.m

+ (void)userSendPasswordRecoveryEmailWithEmailAddress: (NSString*)email 

               success:(void (^)(AFHTTPRequestOperation *operation, id responseObject)) success 
               failure:(void (^)(AFHTTPRequestOperation *operation, NSError *error)) failure { 

    NSString *path = [NSString stringWithFormat:@"user/request_new_password/%@", email]; 
    NSLog(@"This is the input email %@", email); 

    [[DIOSSession sharedSession] sendRequestWithPath:path method:@"POST" params:nil success:success failure:failure]; 
} 

ViewController.m

- (void)viewDidLoad { 
    [super viewDidLoad]; 

    self.forgotField.returnKeyType = UIReturnKeyDone; 
    [self.forgotField setDelegate:self]; 

    // Do any additional setup after loading the view from its nib. 

    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(dismissKeyboard)]; 
    [self.view addGestureRecognizer:tap]; 
} 

- (IBAction)return:(id)sender { 

    [self dismissViewControllerAnimated:YES completion:nil]; 

} 
- (IBAction)sendButton:(id)sender { 

    UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Retrieving Password" 
                message:@"We're helping you retrieve your password! Please check your email in a few minutes for a rescue link." 
                delegate:self 
              cancelButtonTitle:@"OK" 
              otherButtonTitles:nil]; 
    [alert show]; 

} 

dziennika błędu:

2017-07-12 22:29:34.264669-0700 myApp[4523:1331335] 
----- DIOS Failure ----- 
Status code: 404 
URL: http://url.com/endpoint01/user/request_new_password/[email protected] 
----- Response ----- 

----- Error ----- 
Request failed: not found (404) 
+0

Czy próbowali sugestia wspomniano w https://drupal.stackexchange.com/questions/215281/password-reset-causing-404 – Ellen

+0

Spróbuj moją odpowiedź poniżej – Dhiru

Odpowiedz

0

Skończyłem, wykonując tę ​​czynność za pomocą poniższego kodu - wysyłanie postu, aby ktokolwiek inny uznał to za użyteczne! Dwa nieco różne alternatywy w zależności od swojej struktury bazy danych:

- (IBAction)sendButton:(id)sender { 

    [[DIOSSession sharedSession] getCSRFTokenWithSuccess:^(AFHTTPRequestOperation *operation, id responseObject) { 
     NSString *csrfToken = [NSString stringWithUTF8String:[responseObject bytes]]; 

    NSString *email = self.forgotField.text; 

    NSString *urlString2 = [NSString stringWithFormat:@"http://myapp.com/endpoint01/user/request_new_password?name=%@", 
         email]; 
    NSDictionary *jsonBodyDict = @{@"name":email}; 
    NSData *jsonBodyData = [NSJSONSerialization dataWithJSONObject:jsonBodyDict options:kNilOptions error:nil]; 


    NSMutableURLRequest *request = [NSMutableURLRequest new]; 
    request.HTTPMethod = @"POST"; 

    // for alternative 1: 
    [request setURL:[NSURL URLWithString:urlString2]]; 
    [request setValue:@"application/json" forHTTPHeaderField:@"Content-Type"]; 
    [request setValue:@"application/json" forHTTPHeaderField:@"Accept"]; 

    [request setHTTPBody:jsonBodyData]; 

    // for alternative 2: 
    [request setURL:[NSURL URLWithString:urlString2]]; 
     [request addValue:csrfToken forHTTPHeaderField:@"X-CSRF-Token"]; 

    NSURLSessionConfiguration *config = [NSURLSessionConfiguration defaultSessionConfiguration]; 
    NSURLSession *session = [NSURLSession sessionWithConfiguration:config 
                  delegate:nil 
                delegateQueue:[NSOperationQueue mainQueue]]; 
    NSURLSessionDataTask *task = [session dataTaskWithRequest:request 
              completionHandler:^(NSData * _Nullable data, 
                   NSURLResponse * _Nullable response, 
                   NSError * _Nullable error) { 
               NSLog(@"Yay, done! Check for errors in response!"); 

               NSHTTPURLResponse *asHTTPResponse = (NSHTTPURLResponse *) response; 
               NSLog(@"The response is: %@", asHTTPResponse); 
               // set a breakpoint on the last NSLog and investigate the response in the debugger 

               // if you get data, you can inspect that, too. If it's JSON, do one of these: 
               NSDictionary *forJSONObject = [NSJSONSerialization JSONObjectWithData:data 
                               options:kNilOptions 
                               error:nil]; 
               // or 
               NSArray *forJSONArray = [NSJSONSerialization JSONObjectWithData:data 
                             options:kNilOptions 
                              error:nil]; 

              }]; 
    [task resume]; 

     UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Retrieving Password" 
                 message:@"We're helping you retrieve your password! Please check your email in a few minutes for a rescue link." 
                 delegate:self 
               cancelButtonTitle:@"OK" 
               otherButtonTitles:nil]; 
     [alert show]; 

    } failure:^(AFHTTPRequestOperation *operation, NSError *error) { 

    }]; 

    } 
5

Należy trad Wykonaj następujące czynności, zakładając, że forgotField przyjmuje ID e-mail jako dane wejściowe i masz poprawną weryfikację, aby sprawdzić poprawny adres e-mail.

- (IBAction)sendButton:(id)sender { 

     [DIOSUser userSendPasswordRecoveryEmailWithEmailAddress:self.forgotField.text 
success:^(AFHTTPRequestOperation *operation, id responseObject) failure:^(AFHTTPRequestOperation *operation , NSError *error)){ 

     if(!error){ 
        UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Retrieving Password" 
                 message:@"We're helping you retrieve your password! Please check your email in a few minutes for a rescue link." 
                 delegate:self 
               cancelButtonTitle:@"OK" 
               otherButtonTitles:nil]; 
       [alert show]; 
     } 

    }]; 

} 

Znajdź dokumentacji here

Cheers.

+0

Jesteś niesamowity - chociaż po wdrożeniu powyższego, konsola wyśle ​​mi błąd "Żądanie nie powiodło się - nie znaleziono 404 - URL: http: // mywebsite/myendpoint/resetpassword/recentemailaddress". Jakiś pomysł, dlaczego to może być? – Brittany

+0

@Brittany Wygląda na to, że SDK, którego używasz, jest przestarzałe, spróbuj [waterwheel] (https://github.com/acquia/waterwheel.swift) – iphonic

+0

Mimo to wszystkie pozostałe metody DIOSUser działają? To po prostu URL, którego nie ma w tym przypadku? Jak powinna wyglądać ta ścieżka? – Brittany

0

resetowania można wysłać żądanie za pomocą poniższego kodu:

 - (IBAction)sendButton:(id)sender { 

    [DIOSUser userSendPasswordRecoveryEmailWithEmailAddress:self.txtForgotPassword.text 
                    success:^(AFHTTPRequestOperation *operation, id responseObject) { 
    // Success Block 
    UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Retrieving Password" message:@"We have send you reset link to your email Please check your email." delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil]; 
     [alert show]; 


     }failure:^(AFHTTPRequestOperation *operation , NSError *error){ 

    // Failure Block 
       if(!error){ 
     // error.description 
     UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Oopss" message: error.description delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil]; 
     [alert show]; 

      } 
       }]; 
    } 

Mam nadzieję, że to pomoże.