Skip to content

Commit d856108

Browse files
authored
Add update phone number action to swift app (#49)
* Add update phone number action to swift app
1 parent 73aa312 commit d856108

File tree

1 file changed

+74
-34
lines changed

1 file changed

+74
-34
lines changed

AuthSamples/SwiftSample/ViewController.swift

Lines changed: 74 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -156,6 +156,32 @@ final class ViewController: UIViewController, UITextFieldDelegate {
156156
view.endEditing(true)
157157
}
158158

159+
func verify(phoneNumber: String, completion: @escaping (PhoneAuthCredential?, Error?) -> Void) {
160+
if #available(iOS 8.0, *) {
161+
PhoneAuthProvider.provider().verifyPhoneNumber(phoneNumber) { verificationID, error in
162+
guard error == nil else {
163+
completion(nil, error)
164+
return
165+
}
166+
let codeAlertController =
167+
UIAlertController(title: "Enter Code", message: nil, preferredStyle: .alert)
168+
codeAlertController.addTextField { textfield in
169+
textfield.placeholder = "SMS Code"
170+
textfield.keyboardType = UIKeyboardType.numberPad
171+
}
172+
codeAlertController.addAction(UIAlertAction(title: "OK",
173+
style: .default,
174+
handler: { (UIAlertAction) in
175+
let code = codeAlertController.textFields!.first!.text!
176+
let phoneCredential =
177+
PhoneAuthProvider.provider().credential(withVerificationID: verificationID ?? "",
178+
verificationCode: code)
179+
completion(phoneCredential, nil)
180+
}))
181+
self.present(codeAlertController, animated: true, completion: nil)
182+
}
183+
}
184+
}
159185
/// The user's photo URL used by the last network request for its contents.
160186
fileprivate var lastPhotoURL: URL? = nil
161187

@@ -221,6 +247,20 @@ final class ViewController: UIViewController, UITextFieldDelegate {
221247
self.showAlert(title: "Updated Email", message: self.user?.email)
222248
}
223249
}
250+
case .updatePhone:
251+
let phoneNumber = phoneField.text
252+
self.verify(phoneNumber: phoneNumber!, completion: { (phoneAuthCredential, error) in
253+
guard error == nil else {
254+
self.showAlert(title: "Error", message: error!.localizedDescription)
255+
return
256+
}
257+
self.user!.updatePhoneNumber(phoneAuthCredential!, completion: { error in
258+
self.ifNoError(error) {
259+
self.showAlert(title: "Updated Phone Number")
260+
self.updateUserInfo(Auth.auth())
261+
}
262+
})
263+
})
224264
case .updatePassword:
225265
user!.updatePassword(to: passwordField.text!) { error in
226266
self.ifNoError(error) {
@@ -281,32 +321,14 @@ final class ViewController: UIViewController, UITextFieldDelegate {
281321
completion(EmailAuthProvider.credential(withEmail: emailField.text!,
282322
password: passwordField.text!))
283323
case .phone:
284-
if #available(iOS 8.0, *) {
285-
let phoneNumber = phoneField.text
286-
PhoneAuthProvider.provider().verifyPhoneNumber(phoneNumber!) { verificationID, error in
287-
guard error == nil else {
288-
self.showAlert(title: "Error", message: error!.localizedDescription)
289-
return
290-
}
291-
292-
let codeAlertController =
293-
UIAlertController(title: "Enter Code", message: nil, preferredStyle: .alert)
294-
codeAlertController.addTextField { (textfield) in
295-
textfield.placeholder = "SMS Codde"
296-
textfield.keyboardType = UIKeyboardType.numberPad
297-
}
298-
codeAlertController.addAction(UIAlertAction(title: "OK",
299-
style: .default,
300-
handler: { (UIAlertAction) in
301-
let code = codeAlertController.textFields!.first!.text!
302-
let phoneCredential =
303-
PhoneAuthProvider.provider().credential(withVerificationID: verificationID ?? "",
304-
verificationCode: code)
305-
completion(phoneCredential)
306-
}))
307-
self.present(codeAlertController, animated: true, completion: nil)
324+
let phoneNumber = phoneField.text
325+
self.verify(phoneNumber: phoneNumber!, completion: { (phoneAuthCredential, error) in
326+
guard error == nil else {
327+
self.showAlert(title: "Error", message: error!.localizedDescription)
328+
return
308329
}
309-
}
330+
completion(phoneAuthCredential!)
331+
})
310332
}
311333
}
312334

@@ -356,7 +378,7 @@ final class ViewController: UIViewController, UITextFieldDelegate {
356378
action.requiresPassword
357379
passwordInputLabel.alpha = isPasswordEnabled ? 1.0 : 0.6
358380
passwordField.isEnabled = isPasswordEnabled
359-
phoneField.isEnabled = credentialType.requiresPhone
381+
phoneField.isEnabled = credentialType.requiresPhone || action.requiresPhoneNumber
360382
}
361383

362384
fileprivate func showAlert(title: String, message: String? = "") {
@@ -499,14 +521,17 @@ fileprivate protocol Action {
499521
/// The text description for the particular action.
500522
var text: String { get }
501523

502-
/// Whether or not the action requires credential.
524+
/// Whether or not the action requires a credential.
503525
var requiresCredential : Bool { get }
504526

505-
/// Whether or not the action requires email.
527+
/// Whether or not the action requires an email.
506528
var requiresEmail: Bool { get }
507529

508-
/// Whether or not the credential requires password.
530+
/// Whether or not the credential requires a password.
509531
var requiresPassword: Bool { get }
532+
533+
/// Whether or not the credential requires a phone number.
534+
var requiresPhoneNumber: Bool { get }
510535
}
511536

512537
/// The list of all possible actions the operator can take on the Auth object.
@@ -545,13 +570,17 @@ fileprivate enum AuthAction: Int, Action {
545570
var requiresPassword : Bool {
546571
return self == .createUser
547572
}
573+
574+
var requiresPhoneNumber: Bool {
575+
return false
576+
}
548577
}
549578

550579
/// The list of all possible actions the operator can take on the User object.
551580
fileprivate enum UserAction: Int, Action {
552581

553-
case updateEmail, updatePassword, reload, reauthenticate, getToken, linkWithCredential,
554-
deleteAccount
582+
case updateEmail, updatePhone, updatePassword, reload, reauthenticate, getToken,
583+
linkWithCredential, deleteAccount
555584

556585
/// Total number of user actions.
557586
static var count: Int {
@@ -562,6 +591,12 @@ fileprivate enum UserAction: Int, Action {
562591
switch self {
563592
case .updateEmail:
564593
return "Update Email ⬇️"
594+
case .updatePhone:
595+
if #available(iOS 8.0, *) {
596+
return "Update Phone ⬇️"
597+
} else {
598+
return "-"
599+
}
565600
case .updatePassword:
566601
return "Update Password ⬇️"
567602
case .reload:
@@ -588,6 +623,11 @@ fileprivate enum UserAction: Int, Action {
588623
var requiresPassword : Bool {
589624
return self == .updatePassword
590625
}
626+
627+
var requiresPhoneNumber : Bool {
628+
return self == .updatePhone
629+
}
630+
591631
}
592632

593633
/// The list of all possible credential types the operator can use to sign in or link.
@@ -616,17 +656,17 @@ fileprivate enum CredentialType: Int {
616656
}
617657
}
618658

619-
/// Whether or not the credential requires email.
659+
/// Whether or not the credential requires an email.
620660
var requiresEmail : Bool {
621661
return self == .password
622662
}
623663

624-
/// Whether or not the credential requires password.
664+
/// Whether or not the credential requires a password.
625665
var requiresPassword : Bool {
626666
return self == .password
627667
}
628668

629-
/// Whether or not the credential requires phone.
669+
/// Whether or not the credential requires a phone number.
630670
var requiresPhone : Bool {
631671
return self == .phone
632672
}

0 commit comments

Comments
 (0)