Skip to content

Commit b369374

Browse files
committed
Firebase Phone Auth. Update error messages.
Change-Id: I117c6d40cd0aae6abe51730f20e13abf00c0dd2c
1 parent ecb974b commit b369374

File tree

7 files changed

+71
-41
lines changed

7 files changed

+71
-41
lines changed

FirebasePhoneAuthUI/FUIPhoneAuth.m

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -121,6 +121,42 @@ - (void)callbackWithCredential:(nullable FIRAuthCredential *)credential
121121
}
122122
}
123123

124+
+ (UIAlertController *)alertControllerForError:(NSError *)error
125+
actionHandler:(nullable FUIAuthAlertActionHandler)actionHandler {
126+
NSString *message;
127+
if (error.code == FIRAuthErrorCodeInvalidPhoneNumber) {
128+
message = FUIPhoneAuthLocalizedString(kPAStr_IncorrectPhoneMessage);
129+
} else if (error.code == FIRAuthErrorCodeInvalidVerificationCode) {
130+
message = FUIPhoneAuthLocalizedString(kPAStr_IncorrectCodeMessage);
131+
} else if (error.code == FIRAuthErrorCodeTooManyRequests) {
132+
message = FUIPhoneAuthLocalizedString(kPAStr_TooManyCodesSent);
133+
} else if (error.code == FIRAuthErrorCodeQuotaExceeded) {
134+
message = FUIPhoneAuthLocalizedString(kPAStr_MessageQuotaExceeded);
135+
} else if (error.code == FIRAuthErrorCodeSessionExpired) {
136+
message = FUIPhoneAuthLocalizedString(kPAStr_MessageExpired);
137+
} else if ((error.code >= FIRAuthErrorCodeMissingPhoneNumber
138+
&& error.code <= FIRAuthErrorCodeAppNotVerified)
139+
|| error.code >= FIRAuthErrorCodeInternalError) {
140+
message = FUIPhoneAuthLocalizedString(kPAStr_InternalErrorMessage);
141+
} else {
142+
message = error.localizedDescription;
143+
}
144+
UIAlertController *alertController =
145+
[UIAlertController alertControllerWithTitle:nil
146+
message:message
147+
preferredStyle:UIAlertControllerStyleAlert];
148+
UIAlertAction *okAction =
149+
[UIAlertAction actionWithTitle:FUIPhoneAuthLocalizedString(kPAStr_Done)
150+
style:UIAlertActionStyleDefault
151+
handler:^(UIAlertAction *_Nonnull action) {
152+
if (actionHandler) {
153+
actionHandler();
154+
}
155+
}];
156+
[alertController addAction:okAction];
157+
return alertController;
158+
}
159+
124160
@end
125161

126162
NS_ASSUME_NONNULL_END

FirebasePhoneAuthUI/FUIPhoneAuthStrings.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,9 @@ extern NSString *const kPAStr_Back;
3939
extern NSString *const kPAStr_IncorrectPhoneTitle;
4040
extern NSString *const kPAStr_IncorrectPhoneMessage;
4141
extern NSString *const kPAStr_InternalErrorMessage;
42+
extern NSString *const kPAStr_TooManyCodesSent;
43+
extern NSString *const kPAStr_MessageQuotaExceeded;
44+
extern NSString *const kPAStr_MessageExpired;
4245

4346
#ifdef __cplusplus
4447
extern "C" {

FirebasePhoneAuthUI/FUIPhoneAuthStrings.m

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,9 @@
4141
NSString *const kPAStr_IncorrectPhoneTitle = @"IncorrectPhoneTitle";
4242
NSString *const kPAStr_IncorrectPhoneMessage = @"IncorrectPhoneMessage";
4343
NSString *const kPAStr_InternalErrorMessage = @"InternalErrorMessage";
44+
NSString *const kPAStr_TooManyCodesSent = @"TooManyCodesSent";
45+
NSString *const kPAStr_MessageQuotaExceeded = @"MessageQuotaExceeded";
46+
NSString *const kPAStr_MessageExpired = @"MessageExpired";
4447

4548
/** @var kPhoneAuthProviderTableName
4649
@brief The name of the strings table to search for localized strings.

FirebasePhoneAuthUI/FUIPhoneAuth_Internal.h

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
#import "FUIPhoneAuth.h"
1818

1919
#import <FirebasePhoneAuthUI/FirebasePhoneAuthUI.h>
20+
#import "FUIAuthBaseViewController.h"
2021

2122
NS_ASSUME_NONNULL_BEGIN
2223

@@ -33,6 +34,14 @@ NS_ASSUME_NONNULL_BEGIN
3334
error:(nullable NSError *)error
3435
result:(nullable FIRAuthResultCallback)result;
3536

37+
/** @fn alertControllerForError:actionHandler:
38+
@brief Creates alert controller for specified phone auth error.
39+
@param error The error which should be shown in alert.
40+
@param actionHandler The handler of alert action button, if any.
41+
*/
42+
+ (UIAlertController *)alertControllerForError:(NSError *)error
43+
actionHandler:(nullable FUIAuthAlertActionHandler)actionHandler;
44+
3645
@end
3746

3847
NS_ASSUME_NONNULL_END

FirebasePhoneAuthUI/FUIPhoneEntryViewController.m

Lines changed: 4 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -143,22 +143,10 @@ - (void)onNext:(NSString *)phoneNumber {
143143
self.navigationItem.rightBarButtonItem.enabled = YES;
144144

145145
if (error) {
146-
if (error.code == FIRAuthErrorCodeInvalidPhoneNumber) {
147-
NSString *title = FUIPhoneAuthLocalizedString(kPAStr_IncorrectPhoneTitle);
148-
NSString *message = FUIPhoneAuthLocalizedString(kPAStr_IncorrectPhoneMessage);
149-
[[self class] showAlertWithTitle:title
150-
message:message
151-
actionTitle:FUIPhoneAuthLocalizedString(kPAStr_Done)
152-
presentingViewController:self];
153-
} if (error.code >= FIRAuthErrorCodeInternalError) {
154-
NSString *message = FUIPhoneAuthLocalizedString(kPAStr_InternalErrorMessage);
155-
[[self class] showAlertWithTitle:nil
156-
message:message
157-
actionTitle:FUIPhoneAuthLocalizedString(kPAStr_Done)
158-
presentingViewController:self];
159-
} else {
160-
[self showAlertWithMessage:error.localizedDescription];
161-
}
146+
UIAlertController *alertController = [FUIPhoneAuth alertControllerForError:error
147+
actionHandler:nil];
148+
[self presentViewController:alertController animated:YES completion:nil];
149+
162150
FUIPhoneAuth *delegate = [self.authUI providerWithID:FIRPhoneAuthProviderID];
163151
[delegate callbackWithCredential:nil error:error result:nil];
164152
return;

FirebasePhoneAuthUI/FUIPhoneVerificationViewController.m

Lines changed: 4 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -176,28 +176,10 @@ - (void)onNext:(NSString *)verificationCode {
176176
if (!error || error.code == FUIAuthErrorCodeUserCancelledSignIn) {
177177
[self.navigationController dismissViewControllerAnimated:YES completion:nil];
178178
} else {
179-
NSString *title;
180-
NSString *message;
181-
if (error.code >= FIRAuthErrorCodeMissingPhoneNumber
182-
&& error.code <= FIRAuthErrorCodeInvalidVerificationID) {
183-
title = FUIPhoneAuthLocalizedString(kPAStr_IncorrectCodeTitle);
184-
message = FUIPhoneAuthLocalizedString(kPAStr_IncorrectCodeMessage);
185-
} if (error.code >= FIRAuthErrorCodeInternalError) {
186-
message = FUIPhoneAuthLocalizedString(kPAStr_InternalErrorMessage);
187-
} else {
188-
message = error.localizedDescription;
189-
}
190-
UIAlertController *alertController =
191-
[UIAlertController alertControllerWithTitle:title
192-
message:message
193-
preferredStyle:UIAlertControllerStyleAlert];
194-
UIAlertAction *okAction =
195-
[UIAlertAction actionWithTitle:FUIPhoneAuthLocalizedString(kPAStr_Done)
196-
style:UIAlertActionStyleDefault
197-
handler:^(UIAlertAction *_Nonnull action) {
198-
[_codeField clearCodeInput];
199-
}];
200-
[alertController addAction:okAction];
179+
UIAlertController *alertController = [FUIPhoneAuth alertControllerForError:error
180+
actionHandler:^{
181+
[_codeField clearCodeInput];
182+
}];
201183
[self presentViewController:alertController animated:YES completion:nil];
202184
}
203185
}];

FirebasePhoneAuthUI/Strings/en.lproj/FirebasePhoneAuthUI.strings

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@
4747
"IncorrectPhoneTitle" = "Invalid phone number";
4848

4949
/* The body message of alert shown when user entered invalid phone number format. */
50-
"IncorrectPhoneMessage" = "Try entering your phone number again.";
50+
"IncorrectPhoneMessage" = "Enter a valid phone number.";
5151

5252
/* The body message of alert shown when user tapped resend verification code button. */
5353
"ResendCodeResult" = "Code was sent to %@";
@@ -56,7 +56,16 @@
5656
"IncorrectCodeTitle" = "Incorrect verification code";
5757

5858
/* The body message of alert shown when user entered invalid verification code. */
59-
"IncorrectCodeMessage" = "Try entering your verification code again.";
59+
"IncorrectCodeMessage" = "Wrong code. Try again.";
6060

6161
/* The body message of alert shown when internal server error appeared. */
62-
"InternalErrorMessage" = "Your request couldn't be completed. Try again later.";
62+
"InternalErrorMessage" = "Something went wrong. Please try again.";
63+
64+
/* The body message of alert shown when the user has tried to send too many SMS messages. */
65+
"TooManyCodesSent" = "This phone number has been used too many times.";
66+
67+
/* The body message of alert shown when Firebase project has tried to send too many SMS messages for its price tier. */
68+
"MessageQuotaExceeded" = "There was a problem verifying your phone number.";
69+
70+
/* The body message of alert shown when the SMS confirmation code has expired, so the user should send a new one. */
71+
"MessageExpired" = "This code is no longer valid.";

0 commit comments

Comments
 (0)