nwales
nwales

Reputation: 3561

how to resend email verification parse.com

I am trying to trigger a resend of the user verification email. I read a few posts suggesting one simply set the email address to the same value and save the user object - so I tried that like so:

PFUser.currentUser().email = String(PFUser.currentUser().email)
PFUser.currentUser().saveInBackgroundWithBlock {}

This is not working. It's never firing off the verification email. Is there a better way? What might be going on. I can confirm non nil user and success in the save block.

Upvotes: 2

Views: 1916

Answers (2)

codeetcetera
codeetcetera

Reputation: 3203

In my case I'm copying a lowercase string version of the email as the user's username they login with. So the actual capitalization of the email address doesn't matter. Since the domain of an email address is always read case insensitive I'm flipping the first character of it and saving. This works without a second save and without a possible limbo state.

// Flip the capitlization of the first letter of the email

// Get the domain separator
NSRange atRange = [user.email rangeOfString:@"@"];

// If we couldn't find it we need to back out
if (atRange.location == NSNotFound) {
    completion(NO, [NSError errorWithDomain:@"com.myapp.error"
                                       code:0
                                   userInfo:@{NSLocalizedDescriptionKey : NSLocalizedString(@"User Has Invalid Email Message", @"User Has Invalid Email Message") } ]);
    return;
}

// Get the flip range and character
NSRange flipRange = NSMakeRange(atRange.location + 1, 1);
NSString *flipChar = [user.email substringWithRange:flipRange];

// Flip the capitlization
flipChar = ([[NSCharacterSet uppercaseLetterCharacterSet] characterIsMember:[flipChar characterAtIndex:0]]) ?
[flipChar lowercaseString] : [flipChar uppercaseString];

// Updated and save
user.email = [user.email stringByReplacingCharactersInRange:flipRange withString:flipChar];
[user saveInBackgroundWithBlock:^(BOOL succeeded, NSError * _Nullable error) {
    if (succeeded) {
        // Handle Success
    }
    else{
        // Handle failure
    }
}];

If you need to keep capitalization you can always fix it in a cloud code hook.

Upvotes: 7

rickerbh
rickerbh

Reputation: 9913

You've found one of the roughest edges in Parse's handy email verification behaviour :-) Resending email verifications used to work by resetting the email to the users current address, saving, and the verification would be sent. Currently, this behaviour is not working.

Verification emails are only sent now if the email address changes on the server, which is terrible behaviour. It means you need to set the email address to something else, save, when that's successful change it back to the users original email, and save again. The trick is that the first change and save, will send a verification email.

What I've done, and it's horrible, but it's the only way I've found around this that doesn't send out random verification emails to temporary/invalid addresses, is to empty the users email, save, set it again, and save. This results in a middle state where you're screwed if the setting/saving of the correct address fails, but it's the only workaround I've found. Code below.

let user = PFUser.currentUser()
let email = user.email
user.email = ""
user.saveInBackgroundWithBlock { result, error in
  if let e = error {
    // Handle the error
    return
  }
  user.email = email
  user.saveInBackgroundWithBlock {result, error in
    if let e = error {
      // If you have an error here you're screwed, as your user now has a blank email address
      return
    }
  }
}

A way around having the user having an irretrievably broken email address could be to have a "oldEmail" address field on the user object, and you set that to their email, and then their email to "", and then save, so if the first save succeeds and the second one fails you can try to recover by setting email = oldEmail.

Upvotes: 8

Related Questions