aboutsummaryrefslogtreecommitdiffhomepage
path: root/AuthSamples/Sample
diff options
context:
space:
mode:
authorGravatar Zsika Phillip <protocol86@users.noreply.github.com>2017-09-04 17:37:46 -0700
committerGravatar GitHub <noreply@github.com>2017-09-04 17:37:46 -0700
commit85e81371801f9cd3e94a1505ce3b2f4f66086b08 (patch)
tree220fdf17ff9aed20e865bf019dd7dad805458fab /AuthSamples/Sample
parent540e21c0cc3206a8e911554227cc1f081ef40dda (diff)
Adds app verification alternative (#228)
* Adds app verification alternative
Diffstat (limited to 'AuthSamples/Sample')
-rw-r--r--AuthSamples/Sample/MainViewController.m119
1 files changed, 89 insertions, 30 deletions
diff --git a/AuthSamples/Sample/MainViewController.m b/AuthSamples/Sample/MainViewController.m
index bb8b07d..fb9bf67 100644
--- a/AuthSamples/Sample/MainViewController.m
+++ b/AuthSamples/Sample/MainViewController.m
@@ -37,6 +37,12 @@
#import "UserInfoViewController.h"
#import "UserTableViewCell.h"
+
+/*! @typedef textInputCompletionBlock
+ @brief The type of callback used to report text input prompt results.
+ */
+typedef void (^textInputCompletionBlock)(NSString *_Nullable userInput);
+
/** @var kTokenGetButtonText
@brief The text of the "Get Token" button.
*/
@@ -506,6 +512,11 @@ static NSString *const kPhoneAuthSectionTitle = @"Phone Auth";
*/
static NSString *const kPhoneNumberSignInTitle = @"Sign in With Phone Number";
+/** @var kPhoneNumberSignInTitle
+ @brief The title for button to sign in with phone number using reCAPTCHA.
+ */
+static NSString *const kPhoneNumberSignInReCaptchaTitle = @"Sign in With Phone Number (reCAPTCHA)";
+
/** @typedef showEmailPasswordDialogCompletion
@brief The type of block which gets called to complete the Email/Password dialog flow.
*/
@@ -644,6 +655,8 @@ typedef enum {
action:^{ [weakSelf presentSettings]; }]
]],
[StaticContentTableViewSection sectionWithTitle:kPhoneAuthSectionTitle cells:@[
+ [StaticContentTableViewCell cellWithTitle:kPhoneNumberSignInReCaptchaTitle
+ action:^{ [weakSelf signInWithPhoneNumberRecaptcha]; }],
[StaticContentTableViewCell cellWithTitle:kPhoneNumberSignInTitle
action:^{ [weakSelf signInWithPhoneNumber]; }],
[StaticContentTableViewCell cellWithTitle:kUpdatePhoneNumber
@@ -2399,14 +2412,9 @@ static NSDictionary<NSString *, NSString *> *parseURL(NSString *urlString) {
@brief Allows sign in with phone number.
*/
- (void)signInWithPhoneNumber {
- [self showTextInputPromptWithMessage:@"Phone #:"
- keyboardType:UIKeyboardTypePhonePad
- completionBlock:^(BOOL userPressedOK, NSString *_Nullable phoneNumber) {
- if (!userPressedOK || !phoneNumber.length) {
- return;
- }
+ [self commonPhoneNumberInputWithTitle:@"Phone #" Completion:^(NSString *_Nullable phone) {
[self showSpinner:^{
- [[AppManager phoneAuthProvider] verifyPhoneNumber:phoneNumber
+ [[AppManager phoneAuthProvider] verifyPhoneNumber:phone
completion:^(NSString *_Nullable verificationID,
NSError *_Nullable error) {
[self hideSpinner:^{
@@ -2417,29 +2425,10 @@ static NSDictionary<NSString *, NSString *> *parseURL(NSString *urlString) {
}
[self logSuccess:@"Code sent"];
- [self showTextInputPromptWithMessage:@"Verification code:"
- keyboardType:UIKeyboardTypeNumberPad
- completionBlock:^(BOOL userPressedOK,
- NSString *_Nullable verificationCode) {
- if (!userPressedOK || !verificationCode.length) {
- return;
- }
- [self showSpinner:^{
- FIRAuthCredential *credential =
- [[AppManager phoneAuthProvider] credentialWithVerificationID:verificationID
- verificationCode:verificationCode];
- [[AppManager auth] signInWithCredential:credential
- completion:^(FIRUser *_Nullable user,
- NSError *_Nullable error) {
- [self hideSpinner:^{
- if (error) {
- [self logFailure:@"failed to verify phone number" error:error];
- [self showMessagePrompt:error.localizedDescription];
- return;
- }
- }];
- }];
- }];
+ [self commonPhoneNumberInputWithTitle:@"Code"
+ Completion:^(NSString *_Nullable verificationCode) {
+ [self commontPhoneVerificationWithVerificationID:verificationID
+ verificationCode:verificationCode];
}];
}];
}];
@@ -2447,6 +2436,76 @@ static NSDictionary<NSString *, NSString *> *parseURL(NSString *urlString) {
}];
}
+/** @fn signInWithPhoneNumberRecaptcha
+ @brief Allows sign in with phone number using reCAPTCHA
+ */
+- (void)signInWithPhoneNumberRecaptcha {
+ [self commonPhoneNumberInputWithTitle:@"Phone #" Completion:^(NSString *_Nullable phone) {
+ [self showSpinner:^{
+ [[AppManager phoneAuthProvider] verifyPhoneNumber:phone
+ UIDelegate:nil
+ completion:^(NSString *_Nullable verificationID,
+ NSError *_Nullable error) {
+ [self hideSpinner:^{
+ if (error) {
+ [self logFailure:@"failed to send verification code" error:error];
+ [self showMessagePrompt:error.localizedDescription];
+ return;
+ }
+ [self logSuccess:@"Code sent"];
+
+ [self commonPhoneNumberInputWithTitle:@"Code"
+ Completion:^(NSString *_Nullable verificationCode) {
+ [self commontPhoneVerificationWithVerificationID:verificationID
+ verificationCode:verificationCode];
+ }];
+ }];
+ }];
+ }];
+ }];
+}
+
+/** @fn commonPhoneNumberInputWithLabel:Completion
+ @brief Allows user input into a text field.
+ @param title of the promt.
+ */
+- (void)commonPhoneNumberInputWithTitle:(NSString *)title
+ Completion:(textInputCompletionBlock)completion {
+ [self showTextInputPromptWithMessage:title
+ keyboardType:UIKeyboardTypePhonePad
+ completionBlock:^(BOOL userPressedOK, NSString *_Nullable phoneNumber) {
+ if (!userPressedOK || !phoneNumber.length) {
+ return;
+ }
+ completion(phoneNumber);
+ }];
+}
+
+/** @fn commonPhoneNumberInputWithLabel:Completion
+ @brief Finishes the phone number verification flow.
+ @param verificationID The verificationID from the backend.
+ @param verificationCode The verificationCode from the SMS message.
+ */
+- (void)commontPhoneVerificationWithVerificationID:(NSString *)verificationID
+ verificationCode:(NSString *)verificationCode {
+ [self showSpinner:^{
+ FIRAuthCredential *credential =
+ [[AppManager phoneAuthProvider] credentialWithVerificationID:verificationID
+ verificationCode:verificationCode];
+ [[AppManager auth] signInWithCredential:credential
+ completion:^(FIRUser *_Nullable user,
+ NSError *_Nullable error) {
+ [self hideSpinner:^{
+ if (error) {
+ [self logFailure:@"failed to verify phone number" error:error];
+ [self showMessagePrompt:error.localizedDescription];
+ return;
+ }
+ }];
+ }];
+ }];
+}
+
/** @fn updatePhoneNumber
@brief Allows adding a verified phone number to the currently signed user.
*/