| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305 |
- /*
- * Copyright 2019 Google
- *
- * Licensed under the Apache License, Version 2.0 (the "License");
- * you may not use this file except in compliance with the License.
- * You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
- #import "MainViewController+User.h"
- #import "AppManager.h"
- NS_ASSUME_NONNULL_BEGIN
- @implementation MainViewController (User)
- - (StaticContentTableViewSection *)userSection {
- __weak typeof(self) weakSelf = self;
- return [StaticContentTableViewSection sectionWithTitle:@"User" cells:@[
- [StaticContentTableViewCell cellWithTitle:@"Set Display Name"
- action:^{ [weakSelf setDisplayName]; }],
- [StaticContentTableViewCell cellWithTitle:@"Set Photo URL"
- action:^{ [weakSelf setPhotoURL]; }],
- [StaticContentTableViewCell cellWithTitle:@"Update Email"
- action:^{ [weakSelf updateEmail]; }],
- [StaticContentTableViewCell cellWithTitle:@"Update Password"
- action:^{ [weakSelf updatePassword]; }],
- [StaticContentTableViewCell cellWithTitle:@"Update Phone Number"
- action:^{ [weakSelf updatePhoneNumber]; }],
- [StaticContentTableViewCell cellWithTitle:@"Get Provider IDs for Email"
- action:^{ [weakSelf getProvidersForEmail]; }],
- [StaticContentTableViewCell cellWithTitle:@"Get Sign-in methods for Email"
- action:^{ [weakSelf getAllSignInMethodsForEmail]; }],
- [StaticContentTableViewCell cellWithTitle:@"Reload User"
- action:^{ [weakSelf reloadUser]; }],
- [StaticContentTableViewCell cellWithTitle:@"Delete User"
- action:^{ [weakSelf deleteAccount]; }],
- [StaticContentTableViewCell cellWithTitle:@"Verify before update email"
- action:^{ [weakSelf verifyBeforeUpdateEmail]; }],
- ]];
- }
- - (void)setDisplayName {
- [self showTextInputPromptWithMessage:@"Display Name:"
- completionBlock:^(BOOL userPressedOK, NSString *_Nullable userInput) {
- if (!userPressedOK || !userInput.length) {
- return;
- }
- [self showSpinner:^{
- FIRUserProfileChangeRequest *changeRequest = [[self user] profileChangeRequest];
- changeRequest.displayName = userInput;
- [changeRequest commitChangesWithCompletion:^(NSError *_Nullable error) {
- [self hideSpinner:^{
- if (error) {
- [self logFailure:@"set display name failed" error:error];
- } else {
- [FIRAuth.auth.currentUser getIDTokenResultWithCompletion:^(FIRAuthTokenResult *_Nullable tokenResult,
- NSError *_Nullable error) {
- [self logSuccess:@"set display name succeeded."];
- }];
- }
- [self showTypicalUIForUserUpdateResultsWithTitle:@"Set Display Name" error:error];
- }];
- }];
- }];
- }];
- }
- - (void)setPhotoURL {
- [self showTextInputPromptWithMessage:@"Photo URL:"
- completionBlock:^(BOOL userPressedOK, NSString *_Nullable userInput) {
- if (!userPressedOK || !userInput.length) {
- return;
- }
- [self showSpinner:^{
- FIRUserProfileChangeRequest *changeRequest = [[self user] profileChangeRequest];
- changeRequest.photoURL = [NSURL URLWithString:userInput];
- [changeRequest commitChangesWithCompletion:^(NSError *_Nullable error) {
- if (error) {
- [self logFailure:@"set photo URL failed" error:error];
- } else {
- [self logSuccess:@"set Photo URL succeeded."];
- }
- [self hideSpinner:^{
- [self showTypicalUIForUserUpdateResultsWithTitle:@"Set Photo URL" error:error];
- }];
- }];
- }];
- }];
- }
- - (void)reloadUser {
- [self showSpinner:^() {
- [[self user] reloadWithCompletion:^(NSError *_Nullable error) {
- if (error) {
- [self logFailure:@"reload user failed" error:error];
- } else {
- [self logSuccess:@"reload user succeeded."];
- }
- [self hideSpinner:^() {
- [self showTypicalUIForUserUpdateResultsWithTitle:@"Reload User" error:error];
- }];
- }];
- }];
- }
- - (void)getProvidersForEmail {
- [self showTextInputPromptWithMessage:@"Email:"
- completionBlock:^(BOOL userPressedOK, NSString *_Nullable userInput) {
- if (!userPressedOK || !userInput.length) {
- return;
- }
- [self showSpinner:^{
- [[AppManager auth] fetchProvidersForEmail:userInput
- completion:^(NSArray<NSString *> *_Nullable providers,
- NSError *_Nullable error) {
- if (error) {
- [self logFailure:@"get providers for email failed" error:error];
- } else {
- [self logSuccess:@"get providers for email succeeded."];
- }
- [self hideSpinner:^{
- if (error) {
- [self showMessagePrompt:error.localizedDescription];
- return;
- }
- [self showMessagePrompt:[providers componentsJoinedByString:@", "]];
- }];
- }];
- }];
- }];
- }
- - (void)getAllSignInMethodsForEmail {
- [self showTextInputPromptWithMessage:@"Email:"
- completionBlock:^(BOOL userPressedOK, NSString *_Nullable userInput) {
- if (!userPressedOK || !userInput.length) {
- return;
- }
- [self showSpinner:^{
- [[AppManager auth] fetchSignInMethodsForEmail:userInput
- completion:^(NSArray<NSString *> *_Nullable signInMethods,
- NSError *_Nullable error) {
- if (error) {
- [self logFailure:@"get sign-in methods for email failed" error:error];
- } else {
- [self logSuccess:@"get sign-in methods for email succeeded."];
- }
- [self hideSpinner:^{
- if (error) {
- [self showMessagePrompt:error.localizedDescription];
- return;
- }
- [self showMessagePrompt:[signInMethods componentsJoinedByString:@", "]];
- }];
- }];
- }];
- }];
- }
- - (void)updateEmail {
- [self showTextInputPromptWithMessage:@"Email Address:"
- completionBlock:^(BOOL userPressedOK, NSString *_Nullable userInput) {
- if (!userPressedOK || !userInput.length) {
- return;
- }
- [self showSpinner:^{
- [[self user] updateEmail:userInput completion:^(NSError *_Nullable error) {
- if (error) {
- [self logFailure:@"update email failed" error:error];
- } else {
- [self logSuccess:@"update email succeeded."];
- }
- [self hideSpinner:^{
- [self showTypicalUIForUserUpdateResultsWithTitle:@"Update Email" error:error];
- }];
- }];
- }];
- }];
- }
- - (void)verifyBeforeUpdateEmail {
- [self showTextInputPromptWithMessage:@"Email Address:"
- completionBlock:^(BOOL userPressedOK, NSString *_Nullable userInput) {
- if (!userPressedOK || !userInput.length) {
- return;
- }
- [self showSpinner:^{
- [[self user] sendEmailVerificationBeforeUpdatingEmail:userInput
- actionCodeSettings:[self actionCodeSettings]
- completion:^(NSError *_Nullable error) {
- if (error) {
- [self logFailure:@"verify before update email failed." error:error];
- } else {
- [self logSuccess:@"verify before update email succeeded."];
- }
- [self hideSpinner:^{
- [self showTypicalUIForUserUpdateResultsWithTitle:@"Update Email" error:error];
- }];
- }];
- }];
- }];
- }
- - (void)updatePassword {
- [self showTextInputPromptWithMessage:@"New Password:"
- completionBlock:^(BOOL userPressedOK, NSString *_Nullable userInput) {
- if (!userPressedOK) {
- return;
- }
- [self showSpinner:^{
- [[self user] updatePassword:userInput completion:^(NSError *_Nullable error) {
- if (error) {
- [self logFailure:@"update password failed" error:error];
- } else {
- [self logSuccess:@"update password succeeded."];
- }
- [self hideSpinner:^{
- [self showTypicalUIForUserUpdateResultsWithTitle:@"Update Password" error:error];
- }];
- }];
- }];
- }];
- }
- - (void)deleteAccount {
- FIRUser *user = [self user];
- [user deleteWithCompletion:^(NSError *_Nullable error) {
- if (error) {
- [self logFailure:@"delete account failed" error:error];
- }
- [self showTypicalUIForUserUpdateResultsWithTitle:@"Delete User" error:error];
- }];
- }
- - (void)updatePhoneNumber:(NSString *_Nullable)phoneNumber
- completion:(nullable TestAutomationCallback)completion {
- [self showSpinner:^{
- [[AppManager phoneAuthProvider] verifyPhoneNumber:phoneNumber
- UIDelegate:nil
- completion:^(NSString *_Nullable verificationID,
- NSError *_Nullable error) {
- if (error) {
- [self logFailure:@"failed to send verification code" error:error];
- [self showMessagePrompt:error.localizedDescription];
- if (completion) {
- completion(error);
- }
- return;
- }
- [self logSuccess:@"Code sent"];
- [self showTextInputPromptWithMessage:@"Verification code:"
- keyboardType:UIKeyboardTypeNumberPad
- completionBlock:^(BOOL userPressedOK,
- NSString *_Nullable verificationCode) {
- if (!userPressedOK || !verificationCode.length) {
- return;
- }
- [self showSpinner:^{
- FIRPhoneAuthCredential *credential =
- [[AppManager phoneAuthProvider] credentialWithVerificationID:verificationID
- verificationCode:verificationCode];
- [[self user] updatePhoneNumberCredential:credential
- completion:^(NSError *_Nullable error) {
- if (error) {
- [self logFailure:@"update phone number failed" error:error];
- [self showMessagePrompt:error.localizedDescription];
- if (completion) {
- completion(error);
- }
- } else {
- [self logSuccess:@"update phone number succeeded."];
- if (completion) {
- completion(nil);
- }
- }
- }];
- }];
- }];
- }];
- }];
- }
- - (void)updatePhoneNumber {
- [self showTextInputPromptWithMessage:@"Update Phone #:"
- keyboardType:UIKeyboardTypePhonePad
- completionBlock:^(BOOL userPressedOK, NSString *_Nullable phoneNumber) {
- if (!userPressedOK || !phoneNumber.length) {
- return;
- }
- [self updatePhoneNumber:phoneNumber completion:nil];
- }];
- }
- @end
- NS_ASSUME_NONNULL_END
|