diff options
Diffstat (limited to 'AuthSamples/Sample/MainViewController.m')
-rw-r--r-- | AuthSamples/Sample/MainViewController.m | 117 |
1 files changed, 113 insertions, 4 deletions
diff --git a/AuthSamples/Sample/MainViewController.m b/AuthSamples/Sample/MainViewController.m index 6171da2..8ee1b39 100644 --- a/AuthSamples/Sample/MainViewController.m +++ b/AuthSamples/Sample/MainViewController.m @@ -18,6 +18,7 @@ #import <objc/runtime.h> +#import "AuthCredentials.h" #import "FIRAdditionalUserInfo.h" #import "FirebaseCommunity/FIRApp.h" #import "FirebaseCommunity/FIRAppInternal.h" @@ -55,10 +56,11 @@ static NSString *const kTokenRefreshedAlertTitle = @"Token"; */ static NSString *const kTokenRefreshErrorAlertTitle = @"Get Token Error"; -/** @var kSettingsButtonText - @brief The text of the "Settings" button. +/** @var kSettingsButtonTextDesription + @brief The description for the text of the "Settings" button. */ -static NSString *const kSettingsButtonText = @"[Sample App Settings]"; +static NSString *const kSettingsButtonTextDesription = + @"The button text to open sample app settings"; /** @var kUserInfoButtonText @brief The text of the "Show User Info" button. @@ -265,6 +267,11 @@ static NSString *const kRequestPasswordReset = @"Send Password Reset Email"; */ static NSString *const kResetPassword = @"Reset Password"; +/** @var kResetPasswordInApp + @brief The text of the "Password Reset" button. + */ +static NSString *const kResetPasswordInApp = @"Reset Password In app"; + /** @var kCheckActionCode @brief The text of the "Check action code" button. */ @@ -465,6 +472,11 @@ static NSString *const kSafariFacebookSignOutMessagePrompt = @"This automated te static NSString *const kUnlinkAccountMessagePrompt = @"Sign into gmail with an email address " "that has not been linked to this sample application before. Delete account if necessary."; +/** @var kPasswordResetAction + @brief The value for password reset mode in the action code URL. + */ +static NSString *const kPasswordResetAction = @"PASSWORD_RESET"; + // Declared extern in .h file. NSString *const kCreateUserAccessibilityID = @"CreateUserAccessibilityID"; @@ -584,7 +596,8 @@ typedef void (^FIRTokenCallback)(NSString *_Nullable token, NSError *_Nullable e }], ]], [StaticContentTableViewSection sectionWithTitle:kSectionTitleSettings cells:@[ - [StaticContentTableViewCell cellWithTitle:kSettingsButtonText + [StaticContentTableViewCell cellWithTitle:NSLocalizedString(@"SETTINGSKEY", + kSettingsButtonTextDesription) action:^{ [weakSelf presentSettings]; }] ]], [StaticContentTableViewSection sectionWithTitle:kPhoneAuthSectionTitle cells:@[ @@ -636,6 +649,8 @@ typedef void (^FIRTokenCallback)(NSString *_Nullable token, NSError *_Nullable e action:^{ [weakSelf requestVerifyEmail]; }], [StaticContentTableViewCell cellWithTitle:kRequestPasswordReset action:^{ [weakSelf requestPasswordReset]; }], + [StaticContentTableViewCell cellWithTitle:kResetPasswordInApp + action:^{ [weakSelf requestPasswordResetInApp]; }], [StaticContentTableViewCell cellWithTitle:kResetPassword action:^{ [weakSelf resetPassword]; }], [StaticContentTableViewCell cellWithTitle:kCheckActionCode @@ -747,6 +762,68 @@ typedef void (^FIRTokenCallback)(NSString *_Nullable token, NSError *_Nullable e [self updateUserInfo]; } +/** @fn parseURL + @brief Parses an incoming URL into all available query items. + @param urlString The url to be parsed. + @return A dictionary of available query items in the target URL. + */ +static NSDictionary<NSString *, NSString *> *parseURL(NSString *urlString) { + NSString *linkURL = [NSURLComponents componentsWithString:urlString].query; + NSArray<NSString *> *URLComponents = [linkURL componentsSeparatedByString:@"&"]; + NSMutableDictionary<NSString *, NSString *> *queryItems = + [[NSMutableDictionary alloc] initWithCapacity:URLComponents.count]; + for (NSString *component in URLComponents) { + NSRange equalRange = [component rangeOfString:@"="]; + if (equalRange.location != NSNotFound) { + NSString *queryItemKey = + [[component substringToIndex:equalRange.location] stringByRemovingPercentEncoding]; + NSString *queryItemValue = + [[component substringFromIndex:equalRange.location + 1] stringByRemovingPercentEncoding]; + if (queryItemKey && queryItemValue) { + queryItems[queryItemKey] = queryItemValue; + } + } + } + return queryItems; +} + +#pragma mark public methods + +- (BOOL)handleIncomingLinkWithURL:(NSURL *)URL { + // Parse the query portion of the incoming URL. + NSDictionary<NSString *, NSString *> *queryItems = + parseURL([NSURLComponents componentsWithString:URL.absoluteString].query); + + // Check that all necessary query items are available. + if (!queryItems[@"oobCode"] || + !queryItems[@"mode"]) { + return NO; + } + // Handle Password Reset action. + if ([queryItems[@"mode"] isEqualToString:kPasswordResetAction]) { + [self showTextInputPromptWithMessage:@"New Password:" + completionBlock:^(BOOL userPressedOK, NSString *_Nullable newPassword) { + if (!userPressedOK || !newPassword.length) { + return; + } + [[FIRAuth auth] confirmPasswordResetWithCode:queryItems[@"oobCode"] + newPassword:newPassword + completion:^(NSError *_Nullable error) { + [self hideSpinner:^{ + if (error) { + [self logFailure:@"Password reset failed" error:error]; + [self showMessagePrompt:error.localizedDescription]; + return; + } + [self logSuccess:@"Password reset succeeded."]; + [self showMessagePrompt:@"Password reset succeeded."]; + }]; + }]; + }]; + } + return YES; +} + #pragma mark - Actions /** @fn signInWithProvider:provider: @@ -1953,6 +2030,38 @@ typedef void (^FIRTokenCallback)(NSString *_Nullable token, NSError *_Nullable e }]; } +/** @fn requestPasswordResetInApp + @brief Requests a "password reset" email be sent and handled in the app. + */ +- (void)requestPasswordResetInApp { + [self showTextInputPromptWithMessage:@"Email:" + completionBlock:^(BOOL userPressedOK, NSString *_Nullable userInput) { + if (!userPressedOK || !userInput.length) { + return; + } + [self showSpinner:^{ + FIRActionCodeSettings *actionCodeSettings = [[FIRActionCodeSettings alloc] init]; + [actionCodeSettings setIOSBundleID:[NSBundle mainBundle].bundleIdentifier]; + actionCodeSettings.URL = [NSURL URLWithString:KCONTINUE_URL]; + actionCodeSettings.handleCodeInApp = YES; + + [[FIRAuth auth] sendPasswordResetWithEmail:userInput + actionCodeSettings:actionCodeSettings + completion:^(NSError *_Nullable error) { + [self hideSpinner:^{ + if (error) { + [self logFailure:@"request password reset failed" error:error]; + [self showMessagePrompt:error.localizedDescription]; + return; + } + [self logSuccess:@"request password reset succeeded."]; + [self showMessagePrompt:@"Sent"]; + }]; + }]; + }]; + }]; +} + /** @fn resetPassword @brief Performs a password reset operation. */ |