aboutsummaryrefslogtreecommitdiffhomepage
path: root/AuthSamples/Sample/MainViewController.m
diff options
context:
space:
mode:
authorGravatar Xiangtian Dai <xiangtian@google.com>2017-07-28 17:43:33 -0700
committerGravatar GitHub <noreply@github.com>2017-07-28 17:43:33 -0700
commit925a1cc0a27a3afb17b862a0740b56e56c3ae669 (patch)
treeda6935aa77f50ae17e0b6f60425c8bd0f1c142e6 /AuthSamples/Sample/MainViewController.m
parent0abd3444344ea134b0593fcaaf428e82cd190e12 (diff)
Merge from private/auth-master (#159)
* Adds passing state and continue URL in OOBCodes actions. (#82) * Adds actionCodeSettings class (#83) * Adds FIRActionCodeSettings class * Removes iOSAppStoreId to reflect update spec. * Adds “reset password in app” to sample app (#84) * Adds “reset password in app” to sample app * Addresses comments * Addresses comments * Adds @param documentation for actionCodeSettings (#85) Adds @param documentation for actionCodeSettings in FIRGetOOBConfirmationCodeRequest. * Adds request configuration class Adds a class that will comprise of all configurations needed to make a request to the Firebase Auth backend; including APIKey and Language code in the future. * Addresses comments. * Adds Setting Language Add Setting Language Feature to Firebase Auth. - Only adds support for two languages (English and Spanish) in this change. (Also supports two languages in test), more to come after this change is accepted. - For now sample app tests language setting only when using “reset password in app” option, the email sent will be in the current app language. Additional tests to come after change is accepted. * Amends code for unit tests Will add more comprehensive testing when code accepted. * Ecodes strings files in UTF-8 * Addresses comments * Addresses comments * Remove comments in .strings * Adds remaining TC languages to Firebase Auth. * Add line break to .strings files * remove language code english fallback Remove the fallback that sends “en” as the language code to the backend if no language code is specified. * addresses comment * Fixes FIRSecureToken Crash Integrates FIRSecureTokenService and FIRSecureTokenRequest with FIRAuthRequestConfiguration. * Addresses comment * Add auth language input Adds Auth language input to the Sample App in preparation for manual testing. * removes unnecessary comment * address comments * Add app language option Allow user to set the current app language code as the auth language code. This is in preparation for manual testing before release. * Addresses Comment * Adds missing continue URI Adds missing continue URL to Firebear error handling. * Remove unused line Removes unused line from FIRPhoneAuthProvider.m * Moves the new public header to the right place. * Fixes broken macOS build. * Removes trailing spaces. * Addresses review comments.
Diffstat (limited to 'AuthSamples/Sample/MainViewController.m')
-rw-r--r--AuthSamples/Sample/MainViewController.m117
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.
*/