MainViewController.m 148 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719
  1. /*
  2. * Copyright 2017 Google
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. #import "MainViewController.h"
  17. #import <objc/runtime.h>
  18. #import <GameKit/GameKit.h>
  19. #import <FirebaseCore/FIRApp.h>
  20. #import <FirebaseCore/FIRAppInternal.h>
  21. #import <FirebaseCore/FIRAppAssociationRegistration.h>
  22. #import "AppManager.h"
  23. #import "AuthCredentials.h"
  24. #import "FIRAdditionalUserInfo.h"
  25. #import "FIRGameCenterAuthProvider.h"
  26. #import "FIROAuthProvider.h"
  27. #import "FIRPhoneAuthCredential.h"
  28. #import "FIRPhoneAuthProvider.h"
  29. #import "FIRAuthTokenResult.h"
  30. #import "FirebaseAuth.h"
  31. #import "CustomTokenDataEntryViewController.h"
  32. #import "FacebookAuthProvider.h"
  33. #import "GoogleAuthProvider.h"
  34. #import "SettingsViewController.h"
  35. #import "StaticContentTableViewManager.h"
  36. #import "UIViewController+Alerts.h"
  37. #import "UserInfoViewController.h"
  38. #import "UserTableViewCell.h"
  39. #import "FIRAuth_Internal.h"
  40. #import "FIRAuthAPNSToken.h"
  41. #import "FIRAuthAPNSTokenManager.h"
  42. #import "FIRAuthAppCredential.h"
  43. #import "FIRAuthAppCredentialManager.h"
  44. #import "FIRAuthBackend.h"
  45. #import "FIRVerifyClientRequest.h"
  46. #import "FIRVerifyClientResponse.h"
  47. #import "FIRSendVerificationCodeRequest.h"
  48. NS_ASSUME_NONNULL_BEGIN
  49. /** @typedef textInputCompletionBlock
  50. @brief The type of callback used to report text input prompt results.
  51. */
  52. typedef void (^textInputCompletionBlock)(NSString *_Nullable userInput);
  53. /** @typedef testAutomationCallback
  54. @brief The type of callback used when automatically testing an API.
  55. */
  56. typedef void (^testAutomationCallback)(NSError *_Nullable error);
  57. /** @var kTokenGetButtonText
  58. @brief The text of the "Get Token" button.
  59. */
  60. static NSString *const kTokenGetButtonText = @"Get Token";
  61. /** @var kGetTokenResultButtonText
  62. @brief The text of the "Get Token Result" button.
  63. */
  64. static NSString *const kGetTokenResultButtonText = @"Get Token Result";
  65. /** @var kGetTokenResultForceButtonText
  66. @brief The text of the "Force Token Result" button.
  67. */
  68. static NSString *const kGetTokenResultForceButtonText = @"Force Token Result";
  69. /** @var kTokenRefreshButtonText
  70. @brief The text of the "Refresh Token" button.
  71. */
  72. static NSString *const kTokenRefreshButtonText = @"Force Refresh Token";
  73. /** @var kTokenRefreshedAlertTitle
  74. @brief The title of the "Token Refreshed" alert.
  75. */
  76. static NSString *const kTokenRefreshedAlertTitle = @"Token";
  77. /** @var kTokenRefreshErrorAlertTitle
  78. @brief The title of the "Token Refresh error" alert.
  79. */
  80. static NSString *const kTokenRefreshErrorAlertTitle = @"Get Token Error";
  81. /** @var kSettingsButtonTextDesription
  82. @brief The description for the text of the "Settings" button.
  83. */
  84. static NSString *const kSettingsButtonTextDesription =
  85. @"The button text to open sample app settings";
  86. /** @var kUserInfoButtonText
  87. @brief The text of the "Show User Info" button.
  88. */
  89. static NSString *const kUserInfoButtonText = @"[Show User Info]";
  90. /** @var kSetImageURLText
  91. @brief The text of the "Set Photo url" button.
  92. */
  93. static NSString *const kSetPhotoURLText = @"Set Photo url";
  94. /** @var kSignInGoogleButtonText
  95. @brief The text of the "Google SignIn" button.
  96. */
  97. static NSString *const kSignInGoogleButtonText = @"Sign in with Google";
  98. /** @var kSignInGoogleProviderButtonText
  99. @brief The text of the Google provider version of "Sign In With Provider" button.
  100. */
  101. static NSString *const kSignInGoogleProviderButtonText = @"Sign in with provider:(Google)";
  102. /** @var kSignInWithEmailLink
  103. @brief The text of the "Sign in with Email Link" button.
  104. */
  105. static NSString *const kSignInWithEmailLink = @"Sign in with Email Link";
  106. /** @var kVerifyEmailLinkAccount
  107. @brief The text of the "Verify Email-link Account" button.
  108. */
  109. static NSString *const kVerifyEmailLinkAccount = @"Verify Email-link Account";
  110. /** @var kSendEmailSignInLink
  111. @brief The text of the "Send Email SignIn link" button
  112. */
  113. static NSString *const kSendEmailSignInLink = @"Send Email Sign in Link";
  114. /** @var kSignInAndRetrieveGoogleButtonText
  115. @brief The text of the "Sign in with Google and retrieve data" button.
  116. */
  117. static NSString *const kSignInGoogleAndRetrieveDataButtonText =
  118. @"Sign in with Google and retrieve data";
  119. /** @var kSignInGoogleHeadfulLite
  120. @brief The text of the "Sign in with Google (headful lite)" button.
  121. */
  122. static NSString *const kSignInGoogleHeadfulLite = @"Sign in with Google (headful lite)";
  123. /** @var kSignInMicrosoftHeadfulLite
  124. @brief The text of the "Sign in with Microsoft (headful lite)" button.
  125. */
  126. static NSString *const kSignInMicrosoftHeadfulLite = @"Sign in with Microsoft (headful lite)";
  127. /** @var kSignInFacebookButtonText
  128. @brief The text of the "Facebook SignIn" button.
  129. */
  130. static NSString *const kSignInFacebookButtonText = @"Sign in with Facebook";
  131. /** @var kSignInFacebookAndRetrieveDataButtonText
  132. @brief The text of the "Facebook SignIn and retrieve data" button.
  133. */
  134. static NSString *const kSignInFacebookAndRetrieveDataButtonText =
  135. @"Sign in with Facebook and retrieve data";
  136. /** @var kSignInEmailPasswordButtonText
  137. @brief The text of the "Email/Password SignIn" button.
  138. */
  139. static NSString *const kSignInEmailPasswordButtonText = @"Sign in with Email/Password";
  140. /** @var kSignInEmailPasswordAuthDataResultButtonText
  141. @brief The text of the "Email/Password SignIn (AuthDataResult)" button.
  142. */
  143. static NSString *const kSignInEmailPasswordAuthDataResultButtonText =
  144. @"Sign in with Email/Password (Deprecated)";
  145. /** @var kSignInWithCustomTokenButtonText
  146. @brief The text of the "Sign In (BYOAuth)" button.
  147. */
  148. static NSString *const kSignInWithCustomTokenButtonText = @"Sign In (BYOAuth)";
  149. /** @var kSignInWithCustomAuthResultTokenButtonText
  150. @brief The text of the "Sign In with Custom Token (Auth Result)" button.
  151. */
  152. static NSString *const kSignInWithCustomAuthResultTokenButtonText = @"Sign In with Custom Token"
  153. " (Deprecated)";
  154. /** @var kSignInAnonymouslyButtonText
  155. @brief The text of the "Sign In Anonymously" button.
  156. */
  157. static NSString *const kSignInAnonymouslyButtonText = @"Sign In Anonymously";
  158. /** @var kSignInAnonymouslyWithAuthResultButtonText
  159. @brief The text of the "Sign In Anonymously (AuthDataResult)" button.
  160. */
  161. static NSString *const kSignInAnonymouslyWithAuthResultButtonText =
  162. @"Sign In Anonymously (Deprecated)";
  163. /** @var kSignedInAlertTitle
  164. @brief The text of the "Sign In Succeeded" alert.
  165. */
  166. static NSString *const kSignedInAlertTitle = @"Signed In";
  167. /** @var kSignInErrorAlertTitle
  168. @brief The text of the "Sign In Encountered an Error" alert.
  169. */
  170. static NSString *const kSignInErrorAlertTitle = @"Sign-In Error";
  171. /** @var kSignOutButtonText
  172. @brief The text of the "Sign Out" button.
  173. */
  174. static NSString *const kSignOutButtonText = @"Sign Out";
  175. /** @var kDeleteAccountText
  176. @brief The text of the "Delete Account" button.
  177. */
  178. static NSString *const kDeleteUserText = @"Delete Account";
  179. /** @var kReauthenticateGoogleText
  180. @brief The text of the "Reathenticate Google" button.
  181. */
  182. static NSString *const kReauthenticateGoogleText = @"Reauthenticate Google";
  183. /** @var kReauthenticateGoogleAndRetrieveDataText
  184. @brief The text of the "Reathenticate Google and retrieve data" button.
  185. */
  186. static NSString *const kReauthenticateGoogleAndRetrieveDataText =
  187. @"Reauthenticate Google and retrieve data";
  188. /** @var kReauthenticateFBText
  189. @brief The text of the "Reathenticate Facebook" button.
  190. */
  191. static NSString *const kReauthenticateFBText = @"Reauthenticate FB";
  192. /** @var kReauthenticateFBAndRetrieveDataText
  193. @brief The text of the "Reathenticate Facebook and retrieve data" button.
  194. */
  195. static NSString *const kReauthenticateFBAndRetrieveDataText =
  196. @"Reauthenticate FB and retrieve data";
  197. /** @var kReAuthenticateEmail
  198. @brief The text of the "Reathenticate Email" button.
  199. */
  200. static NSString *const kReauthenticateEmailText = @"Reauthenticate Email/Password";
  201. /** @var kOKButtonText
  202. @brief The text of the "OK" button for the Sign In result dialogs.
  203. */
  204. static NSString *const kOKButtonText = @"OK";
  205. /** @var kSetDisplayNameTitle
  206. @brief The title of the "Set Display Name" error dialog.
  207. */
  208. static NSString *const kSetDisplayNameTitle = @"Set Display Name";
  209. /** @var kUpdateEmailText
  210. @brief The title of the "Update Email" button.
  211. */
  212. static NSString *const kUpdateEmailText = @"Update Email";
  213. /** @var kUpdatePasswordText
  214. @brief The title of the "Update Password" button.
  215. */
  216. static NSString *const kUpdatePasswordText = @"Update Password";
  217. /** @var kUpdatePhoneNumber
  218. @brief The title of the "Update Photo" button.
  219. */
  220. static NSString *const kUpdatePhoneNumber = @"Update Phone Number";
  221. /** @var kLinkPhoneNumber
  222. @brief The title of the "Link phone" button.
  223. */
  224. static NSString *const kLinkPhoneNumber = @"Link Phone Number";
  225. /** @var kUnlinkPhone
  226. @brief The title of the "Unlink Phone" button for unlinking phone auth provider.
  227. */
  228. static NSString *const kUnlinkPhoneNumber = @"Unlink Phone Number";
  229. /** @var kReloadText
  230. @brief The title of the "Reload User" button.
  231. */
  232. static NSString *const kReloadText = @"Reload User";
  233. /** @var kLinkWithGoogleText
  234. @brief The title of the "Link with Google" button.
  235. */
  236. static NSString *const kLinkWithGoogleText = @"Link with Google";
  237. /** @var kLinkWithGoogleAndRetrieveDataText
  238. @brief The title of the "Link with Google and retrieve data" button.
  239. */
  240. static NSString *const kLinkWithGoogleAndRetrieveDataText = @"Link with Google and retrieve data";
  241. /** @var kLinkWithFacebookText
  242. @brief The title of the "Link with Facebook Account" button.
  243. */
  244. static NSString *const kLinkWithFacebookText = @"Link with Facebook";
  245. /** @var kLinkWithFacebookAndRetrieveDataText
  246. @brief The title of the "Link with Facebook and retrieve data" button.
  247. */
  248. static NSString *const kLinkWithFacebookAndRetrieveDataText =
  249. @"Link with Facebook and retrieve data";
  250. /** @var kLinkWithEmailPasswordText
  251. @brief The title of the "Link with Email/Password Account" button.
  252. */
  253. static NSString *const kLinkWithEmailPasswordText = @"Link with Email/Password";
  254. /** @var kUnlinkTitle
  255. @brief The text of the "Unlink from Provider" error Dialog.
  256. */
  257. static NSString *const kUnlinkTitle = @"Unlink from Provider";
  258. /** @var kUnlinkFromGoogle
  259. @brief The text of the "Unlink from Google" button.
  260. */
  261. static NSString *const kUnlinkFromGoogle = @"Unlink from Google";
  262. /** @var kUnlinkFromFacebook
  263. @brief The text of the "Unlink from Facebook" button.
  264. */
  265. static NSString *const kUnlinkFromFacebook = @"Unlink from Facebook";
  266. /** @var kUnlinkFromEmailPassword
  267. @brief The text of the "Unlink from Google" button.
  268. */
  269. static NSString *const kUnlinkFromEmailPassword = @"Unlink from Email/Password";
  270. /** @var kGetProvidersForEmail
  271. @brief The text of the "Get Provider IDs for Email" button.
  272. */
  273. static NSString *const kGetProvidersForEmail = @"Get Provider IDs for Email";
  274. /** @var kGetAllSignInMethodsForEmail
  275. @brief The text of the "Get sign-in methods for Email" button.
  276. */
  277. static NSString *const kGetAllSignInMethodsForEmail = @"Get Sign-in methods for Email";
  278. /** @var kActionCodeTypeDescription
  279. @brief The description of the "Action Type" entry.
  280. */
  281. static NSString *const kActionCodeTypeDescription = @"Action Type";
  282. /** @var kContinueURLDescription
  283. @brief The description of the "Continue URL" entry.
  284. */
  285. static NSString *const kContinueURLDescription = @"Continue URL";
  286. /** @var kRequestVerifyEmail
  287. @brief The text of the "Request Verify Email Link" button.
  288. */
  289. static NSString *const kRequestVerifyEmail = @"Request Verify Email Link";
  290. /** @var kRequestPasswordReset
  291. @brief The text of the "Email Password Reset" button.
  292. */
  293. static NSString *const kRequestPasswordReset = @"Send Password Reset Email";
  294. /** @var kResetPassword
  295. @brief The text of the "Password Reset" button.
  296. */
  297. static NSString *const kResetPassword = @"Reset Password";
  298. /** @var kCheckActionCode
  299. @brief The text of the "Check action code" button.
  300. */
  301. static NSString *const kCheckActionCode = @"Check action code";
  302. /** @var kApplyActionCode
  303. @brief The text of the "Apply action code" button.
  304. */
  305. static NSString *const kApplyActionCode = @"Apply action code";
  306. /** @var kVerifyPasswordResetCode
  307. @brief The text of the "Verify password reset code" button.
  308. */
  309. static NSString *const kVerifyPasswordResetCode = @"Verify password reset code";
  310. /** @var kSectionTitleSettings
  311. @brief The text for the title of the "Settings" section.
  312. */
  313. static NSString *const kSectionTitleSettings = @"SETTINGS";
  314. /** @var kSectionTitleSignIn
  315. @brief The text for the title of the "Sign-In" section.
  316. */
  317. static NSString *const kSectionTitleSignIn = @"SIGN-IN";
  318. /** @var kSectionTitleReauthenticate
  319. @brief The text for the title of the "Reauthenticate" section.
  320. */
  321. static NSString *const kSectionTitleReauthenticate = @"REAUTHENTICATE";
  322. /** @var kSectionTitleTokenActions
  323. @brief The text for the title of the "Token Actions" section.
  324. */
  325. static NSString *const kSectionTitleTokenActions = @"TOKEN ACTIONS";
  326. /** @var kSectionTitleEditUser
  327. @brief The text for the title of the "Edit User" section.
  328. */
  329. static NSString *const kSectionTitleEditUser = @"EDIT USER";
  330. /** @var kSectionTitleLinkUnlinkAccount
  331. @brief The text for the title of the "Link/Unlink account" section.
  332. */
  333. static NSString *const kSectionTitleLinkUnlinkAccounts = @"LINK/UNLINK ACCOUNT";
  334. /** @var kSectionTitleUserActions
  335. @brief The text for the title of the "User Actions" section.
  336. */
  337. static NSString *const kSectionTitleUserActions = @"USER ACTIONS";
  338. /** @var kSectionTitleOOBAction
  339. @brief The text for the title of the "OOB Actions" section.
  340. */
  341. static NSString *const kSectionTitleOOBActions = @"OOB ACTIONS";
  342. /** @var kSectionTitleUserDetails
  343. @brief The text for the title of the "User Details" section.
  344. */
  345. static NSString *const kSectionTitleUserDetails = @"SIGNED-IN USER DETAILS";
  346. /** @var kSectionTitleListeners
  347. @brief The title for the table view section dedicated to auth state did change listeners.
  348. */
  349. static NSString *const kSectionTitleListeners = @"Listeners";
  350. /** @var kAddAuthStateListenerTitle
  351. @brief The title for the table view row which adds a block to the auth state did change
  352. listeners.
  353. */
  354. static NSString *const kAddAuthStateListenerTitle = @"Add Auth State Change Listener";
  355. /** @var kRemoveAuthStateListenerTitle
  356. @brief The title for the table view row which removes a block to the auth state did change
  357. listeners.
  358. */
  359. static NSString *const kRemoveAuthStateListenerTitle = @"Remove Last Auth State Change Listener";
  360. /** @var kAddIDTokenListenerTitle
  361. @brief The title for the table view row which adds a block to the ID token did change
  362. listeners.
  363. */
  364. static NSString *const kAddIDTokenListenerTitle = @"Add ID Token Change Listener";
  365. /** @var kRemoveIDTokenListenerTitle
  366. @brief The title for the table view row which removes a block to the ID token did change
  367. listeners.
  368. */
  369. static NSString *const kRemoveIDTokenListenerTitle = @"Remove Last ID Token Change Listener";
  370. /** @var kSectionTitleApp
  371. @brief The text for the title of the "App" section.
  372. */
  373. static NSString *const kSectionTitleApp = @"APP";
  374. /** @var kSwitchToInMemoryUserTitle
  375. @brief The text of the "Switch to in memory user" button.
  376. */
  377. static NSString *const kSwitchToInMemoryUserTitle = @"Switch to in memory user";
  378. /** @var kCreateUserTitle
  379. @brief The text of the "Create User" button.
  380. */
  381. static NSString *const kCreateUserTitle = @"Create User";
  382. /** @var kCreateUserAuthDataResultTitle
  383. @brief The text of the "Create User (AuthDataResult)" button.
  384. */
  385. static NSString *const kCreateUserAuthDataResultTitle = @"Create User (AuthDataResult)";
  386. /** @var kDeleteAppTitle
  387. @brief The text of the "Delete App" button.
  388. */
  389. static NSString *const kDeleteAppTitle = @"Delete App";
  390. /** @var kTimeAuthInitTitle
  391. @brief The text of the "Time Auth Initialization" button.
  392. */
  393. static NSString *const kTimeAuthInitTitle = @"Time Auth Initialization";
  394. /** @var kSectionTitleManualTests
  395. @brief The section title for automated manual tests.
  396. */
  397. static NSString *const kSectionTitleManualTests = @"Automated (Manual) Tests";
  398. /** @var kAutoBYOAuthTitle
  399. @brief The button title for automated BYOAuth operation.
  400. */
  401. static NSString *const kAutoBYOAuthTitle = @"BYOAuth";
  402. /** @var kAutoSignInGoogle
  403. @brief The button title for automated Google sign-in operation.
  404. */
  405. static NSString *const kAutoSignInGoogle = @"Sign In With Google";
  406. /** @var kAutoSignInFacebook
  407. @brief The button title for automated Facebook sign-in operation.
  408. */
  409. static NSString *const kAutoSignInFacebook = @"Sign In With Facebook";
  410. /** @var kAutoSignUpEmailPassword
  411. @brief The button title for automated sign-up with email/password.
  412. */
  413. static NSString *const kAutoSignUpEmailPassword = @"Sign Up With Email/Password";
  414. /** @var kAutoSignInAnonymously
  415. @brief The button title for automated sign-in anonymously.
  416. */
  417. static NSString *const kAutoSignInAnonymously = @"Sign In Anonymously";
  418. /** @var kAutoAccountLinking
  419. @brief The button title for automated account linking.
  420. */
  421. static NSString *const kAutoAccountLinking = @"Link with Google";
  422. /** @var kAutoPhoneNumberSignIn
  423. @brief The button title for automated Phone Number sign-in.
  424. */
  425. static NSString *const kAutoPhoneNumberSignIn = @"Sign in With Phone Number";
  426. /** @var kGitHubSignInButtonText
  427. @brief The button title for signing in with github.
  428. */
  429. static NSString *const kGitHubSignInButtonText = @"Sign In with GitHub";
  430. /** @var kExpiredCustomTokenUrl
  431. @brief The url for obtaining a valid custom token string used to test BYOAuth.
  432. */
  433. static NSString *const kCustomTokenUrl = @"https://fb-sa-1211.appspot.com/token";
  434. /** @var kCustomTokenUrl
  435. @brief The url for obtaining an expired custom token string used to test BYOAuth.
  436. */
  437. static NSString *const kExpiredCustomTokenUrl = @"https://fb-sa-1211.appspot.com/expired_token";
  438. /** @var kFakeDisplayPhotoUrl
  439. @brief The url for obtaining a display displayPhoto used for testing.
  440. */
  441. static NSString *const kFakeDisplayPhotoUrl =
  442. @"https://www.gstatic.com/images/branding/product/1x/play_apps_48dp.png";
  443. /** @var kFakeDisplayName
  444. @brief Fake display name for testing.
  445. */
  446. static NSString *const kFakeDisplayName = @"John GoogleSpeed";
  447. /** @var kFakeEmail
  448. @brief Fake email for testing.
  449. */
  450. static NSString *const kFakeEmail =@"firemail@example.com";
  451. /** @var kFakePassword
  452. @brief Fake password for testing.
  453. */
  454. static NSString *const kFakePassword =@"fakePassword";
  455. /** @var kInvalidCustomToken
  456. @brief The custom token string for testing BYOAuth.
  457. */
  458. static NSString *const kInvalidCustomToken = @"invalid custom token.";
  459. /** @var kSafariGoogleSignOutMessagePrompt
  460. @brief The message text informing user to sign-out from Google on safari before continuing.
  461. */
  462. static NSString *const kSafariGoogleSignOutMessagePrompt = @"This automated test assumes that no "
  463. "Google account is signed in on Safari, if your are not prompted for a password, sign out on "
  464. "Safari and rerun the test.";
  465. /** @var kSafariFacebookSignOutMessagePrompt
  466. @brief The message text informing user to sign-out from Facebook on safari before continuing.
  467. */
  468. static NSString *const kSafariFacebookSignOutMessagePrompt = @"This automated test assumes that no "
  469. "Facebook account is signed in on Safari, if your are not prompted for a password, sign out on "
  470. "Safari and rerun the test.";
  471. /** @var kUnlinkAccountMessagePrompt
  472. @brief The message text informing user to use an unlinked account for account linking.
  473. */
  474. static NSString *const kUnlinkAccountMessagePrompt = @"Sign into gmail with an email address "
  475. "that has not been linked to this sample application before. Delete account if necessary.";
  476. /** @var kPasswordResetAction
  477. @brief The value for password reset mode in the action code URL.
  478. */
  479. static NSString *const kPasswordResetAction = @"resetPassword";
  480. /** @var kVerifyEmailAction
  481. @brief The value for verify email mode in the action code URL.
  482. */
  483. static NSString *const kVerifyEmailAction = @"verifyEmail";
  484. // Declared extern in .h file.
  485. NSString *const kCreateUserAccessibilityID = @"CreateUserAccessibilityID";
  486. /** @var kPhoneAuthSectionTitle
  487. @brief The title for the phone auth section of the test app.
  488. */
  489. static NSString *const kPhoneAuthSectionTitle = @"Phone Auth";
  490. /** @var kPhoneNumberSignInTitle
  491. @brief The title for button to sign in with phone number using reCAPTCHA.
  492. */
  493. static NSString *const kPhoneNumberSignInReCaptchaTitle = @"Sign in With Phone Number";
  494. /** @var kVerifyIOSClientTitle
  495. @brief The title for button to verify iOS client.
  496. */
  497. static NSString *const kVerifyIOSClientTitle = @"Verify iOS client";
  498. /** @var kGameCenterAuthSectionTitle
  499. @brief The title for the section of Game Center
  500. */
  501. static NSString *const kGameCenterAuthSectionTitle = @"Game Center";
  502. /** @var kLogInWithSystemGameCenterTitle
  503. @brief The title for the button to log into the Game Center account
  504. */
  505. static NSString *const kLogInWithSystemGameCenterTitle = @"Log In System Game Center";
  506. /** @var kSignInWithGameCenterTitle
  507. @brief The title for the button to sign in with Game Center
  508. */
  509. static NSString *const kSignInWithGameCenterTitle = @"Sign in Game Center";
  510. /** @var kLinkWithGameCenterTitle
  511. @brief The title for the button to link with Game Center
  512. */
  513. static NSString *const kLinkWithGameCenterTitle = @"Link Game Center";
  514. /** @var kUnlinkWithGameCenterTitle
  515. @brief The title for the button to unlink with Game Center
  516. */
  517. static NSString *const kUnlinkWithGameCenterTitle = @"Unlink Game Center";
  518. /** @var kReauthenticateWithGameCenterTitle
  519. @brief The title for the button to reauthenticate with Game Center
  520. */
  521. static NSString *const kReauthenticateWithGameCenterTitle = @"Reauthenticate Game Center";
  522. /** @var kNewOrExistingUserToggleTitle
  523. @brief The title for button to enable new or existing user toggle.
  524. */
  525. static NSString *const kNewOrExistingUserToggleTitle = @"New or Existing User Toggle";
  526. /** @typedef showEmailPasswordDialogCompletion
  527. @brief The type of block which gets called to complete the Email/Password dialog flow.
  528. */
  529. typedef void (^ShowEmailPasswordDialogCompletion)(FIRAuthCredential *credential);
  530. /** @typedef FIRTokenCallback
  531. @brief The type of block which gets called when a token is ready.
  532. */
  533. typedef void (^FIRTokenCallback)(NSString *_Nullable token, NSError *_Nullable error);
  534. /** @brief The request type for OOB action codes.
  535. */
  536. typedef enum {
  537. /** No action code settings. */
  538. ActionCodeRequestTypeEmail,
  539. /** With continue URL but not handled in-app. */
  540. ActionCodeRequestTypeContinue,
  541. /** Handled in-app. */
  542. ActionCodeRequestTypeInApp,
  543. } ActionCodeRequestType;
  544. /** @category FIRAppAssociationRegistration(Deregistration)
  545. @brief The category for the deregistration method.
  546. */
  547. @interface FIRAppAssociationRegistration (Deregistration)
  548. /** @fn deregisteredObjectWithHost:key:
  549. @brief Removes the object that was registered with a particular host and key, if one exists.
  550. @param host The host object.
  551. @param key The key to specify the registered object on the host.
  552. */
  553. + (void)deregisterObjectWithHost:(id)host key:(NSString *)key;
  554. @end
  555. @implementation FIRAppAssociationRegistration (Deregistration)
  556. + (void)deregisterObjectWithHost:(id)host key:(NSString *)key {
  557. @synchronized(self) {
  558. SEL dictKey = @selector(registeredObjectWithHost:key:creationBlock:);
  559. NSMutableDictionary<NSString *, id> *objectsByKey = objc_getAssociatedObject(host, dictKey);
  560. [objectsByKey removeObjectForKey:key];
  561. }
  562. }
  563. @end
  564. @implementation MainViewController {
  565. NSMutableString *_consoleString;
  566. /** @var _authStateDidChangeListeners
  567. @brief An array of handles created during calls to @c FIRAuth.addAuthStateDidChangeListener:
  568. */
  569. NSMutableArray<FIRAuthStateDidChangeListenerHandle> *_authStateDidChangeListeners;
  570. /** @var _IDTokenDidChangeListeners
  571. @brief An array of handles created during calls to @c FIRAuth.addIDTokenDidChangeListener:
  572. */
  573. NSMutableArray<FIRAuthStateDidChangeListenerHandle> *_IDTokenDidChangeListeners;
  574. /** @var _userInMemory
  575. @brief Acts like the "memory" function of a calculator. An operation allows sample app users
  576. to assign this value based on @c FIRAuth.currentUser or clear this value.
  577. */
  578. FIRUser *_userInMemory;
  579. /** @var _useUserInMemory
  580. @brief Instructs the application to use _userInMemory instead of @c FIRAuth.currentUser for
  581. testing operations. This allows us to test if things still work with a user who is not
  582. the @c FIRAuth.currentUser, and also allows us to test those things while
  583. @c FIRAuth.currentUser remains nil (after a sign-out) and also when @c FIRAuth.currentUser
  584. is non-nil (do to a subsequent sign-in.)
  585. */
  586. BOOL _useUserInMemory;
  587. /** @var _newUserToggle
  588. @brief When enabled allows login popup alert determining new or existing user.
  589. */
  590. BOOL _isNewUserToggleOn;
  591. /** @var _actionCodeRequestType
  592. @brief The type for the next action code request.
  593. */
  594. ActionCodeRequestType _actionCodeRequestType;
  595. /** @var _actionCodeContinueURL
  596. @brief The continue URL to be used in the next action code request.
  597. */
  598. NSURL *_actionCodeContinueURL;
  599. /** @var _googleOAuthProvider
  600. @brief OAuth provider instance for Google.
  601. */
  602. FIROAuthProvider *_googleOAuthProvider;
  603. /** @var _microsoftOAuthProvider
  604. @brief OAuth provider instance for Microsoft.
  605. */
  606. FIROAuthProvider *_microsoftOAuthProvider;
  607. }
  608. /** @fn initWithNibName:bundle:
  609. @brief Overridden default initializer.
  610. */
  611. - (id)initWithNibName:(nullable NSString *)nibNameOrNil bundle:(nullable NSBundle *)nibBundleOrNil {
  612. self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
  613. if (self) {
  614. _actionCodeRequestType = ActionCodeRequestTypeInApp;
  615. _actionCodeContinueURL = [NSURL URLWithString:KCONTINUE_URL];
  616. _authStateDidChangeListeners = [NSMutableArray array];
  617. _IDTokenDidChangeListeners = [NSMutableArray array];
  618. _googleOAuthProvider = [FIROAuthProvider providerWithProviderID:FIRGoogleAuthProviderID];
  619. _microsoftOAuthProvider = [FIROAuthProvider providerWithProviderID:@"microsoft.com"];
  620. [[NSNotificationCenter defaultCenter] addObserver:self
  621. selector:@selector(authStateChangedForAuth:)
  622. name:FIRAuthStateDidChangeNotification
  623. object:nil];
  624. self.useStatusBarSpinner = YES;
  625. }
  626. return self;
  627. }
  628. - (void)viewDidLoad {
  629. [super viewDidLoad];
  630. // Give us a circle for the image view:
  631. _userInfoTableViewCell.userInfoProfileURLImageView.layer.cornerRadius =
  632. _userInfoTableViewCell.userInfoProfileURLImageView.frame.size.width / 2.0f;
  633. _userInfoTableViewCell.userInfoProfileURLImageView.layer.masksToBounds = YES;
  634. _userInMemoryInfoTableViewCell.userInfoProfileURLImageView.layer.cornerRadius =
  635. _userInMemoryInfoTableViewCell.userInfoProfileURLImageView.frame.size.width / 2.0f;
  636. _userInMemoryInfoTableViewCell.userInfoProfileURLImageView.layer.masksToBounds = YES;
  637. }
  638. - (void)viewWillAppear:(BOOL)animated {
  639. [super viewWillAppear:animated];
  640. [self updateTable];
  641. [self updateUserInfo];
  642. }
  643. - (void)updateTable {
  644. __weak typeof(self) weakSelf = self;
  645. _tableViewManager.contents =
  646. [StaticContentTableViewContent contentWithSections:@[
  647. [StaticContentTableViewSection sectionWithTitle:kSectionTitleUserDetails cells:@[
  648. [StaticContentTableViewCell cellWithCustomCell:_userInfoTableViewCell action:^{
  649. [weakSelf presentUserInfo];
  650. }],
  651. [StaticContentTableViewCell cellWithCustomCell:_userToUseCell],
  652. [StaticContentTableViewCell cellWithCustomCell:_userInMemoryInfoTableViewCell action:^{
  653. [weakSelf presentUserInMemoryInfo];
  654. }],
  655. ]],
  656. [StaticContentTableViewSection sectionWithTitle:kSectionTitleSettings cells:@[
  657. [StaticContentTableViewCell cellWithTitle:NSLocalizedString(@"SETTINGSKEY",
  658. kSettingsButtonTextDesription)
  659. action:^{ [weakSelf presentSettings]; }],
  660. [StaticContentTableViewCell cellWithTitle:kNewOrExistingUserToggleTitle
  661. value:_isNewUserToggleOn ? @"Enabled" : @"Disabled"
  662. action:^{
  663. _isNewUserToggleOn = !_isNewUserToggleOn;
  664. [self updateTable]; }],
  665. ]],
  666. [StaticContentTableViewSection sectionWithTitle:kGameCenterAuthSectionTitle cells:@[
  667. [StaticContentTableViewCell cellWithTitle:kLogInWithSystemGameCenterTitle
  668. action:^{ [weakSelf logInWithSystemGameCenter]; }],
  669. [StaticContentTableViewCell cellWithTitle:kSignInWithGameCenterTitle
  670. action:^{ [weakSelf signInWithGameCenter]; }],
  671. [StaticContentTableViewCell cellWithTitle:kLinkWithGameCenterTitle
  672. action:^{ [weakSelf linkWithGameCenter]; }],
  673. [StaticContentTableViewCell cellWithTitle:kUnlinkWithGameCenterTitle
  674. action:^{
  675. [weakSelf unlinkFromProvider:FIRGameCenterAuthProviderID completion:nil];
  676. }],
  677. [StaticContentTableViewCell cellWithTitle:kReauthenticateWithGameCenterTitle
  678. action:^{ [weakSelf reauthenticateWithGameCenter]; }],
  679. ]],
  680. [StaticContentTableViewSection sectionWithTitle:kPhoneAuthSectionTitle cells:@[
  681. [StaticContentTableViewCell cellWithTitle:kPhoneNumberSignInReCaptchaTitle
  682. action:^{ [weakSelf signInWithPhoneNumberWithPrompt]; }],
  683. [StaticContentTableViewCell cellWithTitle:kUpdatePhoneNumber
  684. action:^{ [weakSelf updatePhoneNumberWithPrompt]; }],
  685. [StaticContentTableViewCell cellWithTitle:kLinkPhoneNumber
  686. action:^{ [weakSelf linkPhoneNumberWithPrompt]; }],
  687. [StaticContentTableViewCell cellWithTitle:kUnlinkPhoneNumber
  688. action:^{
  689. [weakSelf unlinkFromProvider:FIRPhoneAuthProviderID completion:nil];
  690. }],
  691. [StaticContentTableViewCell cellWithTitle:kVerifyIOSClientTitle
  692. action:^{ [weakSelf verifyIOSClient]; }],
  693. ]],
  694. [StaticContentTableViewSection sectionWithTitle:kSectionTitleSignIn cells:@[
  695. [StaticContentTableViewCell cellWithTitle:kSwitchToInMemoryUserTitle
  696. value:nil
  697. action:^{ [weakSelf updateToSavedUser]; }],
  698. [StaticContentTableViewCell cellWithTitle:kCreateUserTitle
  699. value:nil
  700. action:^{ [weakSelf createUser]; }
  701. accessibilityID:kCreateUserAccessibilityID],
  702. [StaticContentTableViewCell cellWithTitle:kCreateUserAuthDataResultTitle
  703. action:^{ [weakSelf createUserAuthDataResult]; }],
  704. [StaticContentTableViewCell cellWithTitle:kSignInGoogleButtonText
  705. action:^{ [weakSelf signInGoogle]; }],
  706. [StaticContentTableViewCell cellWithTitle:kSignInGoogleProviderButtonText
  707. action:^{ [weakSelf signInGoogleProvider]; }],
  708. [StaticContentTableViewCell cellWithTitle:kSignInWithEmailLink
  709. action:^{ [weakSelf signInWithEmailLink]; }],
  710. [StaticContentTableViewCell cellWithTitle:kVerifyEmailLinkAccount
  711. action:^{ [weakSelf verifyEmailLinkAccount]; }],
  712. [StaticContentTableViewCell cellWithTitle:kSendEmailSignInLink
  713. action:^{ [weakSelf sendEmailSignInLink]; }],
  714. [StaticContentTableViewCell cellWithTitle:kSignInGoogleAndRetrieveDataButtonText
  715. action:^{ [weakSelf signInGoogleAndRetrieveData]; }],
  716. [StaticContentTableViewCell cellWithTitle:kSignInGoogleHeadfulLite
  717. action:^{ [weakSelf signInGoogleHeadfulLite]; }],
  718. [StaticContentTableViewCell cellWithTitle:kSignInMicrosoftHeadfulLite
  719. action:^{ [weakSelf signInMicrosoftHeadfulLite]; }],
  720. [StaticContentTableViewCell cellWithTitle:kSignInFacebookButtonText
  721. action:^{ [weakSelf signInFacebook]; }],
  722. [StaticContentTableViewCell cellWithTitle:kSignInFacebookAndRetrieveDataButtonText
  723. action:^{ [weakSelf signInFacebookAndRetrieveData]; }],
  724. [StaticContentTableViewCell cellWithTitle:kSignInEmailPasswordButtonText
  725. action:^{ [weakSelf signInEmailPassword]; }],
  726. [StaticContentTableViewCell cellWithTitle:kSignInEmailPasswordAuthDataResultButtonText
  727. action:^{ [weakSelf signInEmailPasswordAuthDataResult]; }],
  728. [StaticContentTableViewCell cellWithTitle:kSignInWithCustomTokenButtonText
  729. action:^{ [weakSelf signInWithCustomToken]; }],
  730. [StaticContentTableViewCell cellWithTitle:kSignInWithCustomAuthResultTokenButtonText
  731. action:^{ [weakSelf signInWithCustomTokenAuthResult]; }],
  732. [StaticContentTableViewCell cellWithTitle:kSignInAnonymouslyButtonText
  733. action:^{ [weakSelf signInAnonymously]; }],
  734. [StaticContentTableViewCell cellWithTitle:kSignInAnonymouslyWithAuthResultButtonText
  735. action:^{ [weakSelf signInAnonymouslyAuthDataResult]; }],
  736. [StaticContentTableViewCell cellWithTitle:kGitHubSignInButtonText
  737. action:^{ [weakSelf signInWithGitHub]; }],
  738. [StaticContentTableViewCell cellWithTitle:kSignOutButtonText
  739. action:^{ [weakSelf signOut]; }]
  740. ]],
  741. [StaticContentTableViewSection sectionWithTitle:kSectionTitleUserActions cells:@[
  742. [StaticContentTableViewCell cellWithTitle:kSetDisplayNameTitle
  743. action:^{ [weakSelf setDisplayName]; }],
  744. [StaticContentTableViewCell cellWithTitle:kSetPhotoURLText
  745. action:^{ [weakSelf setPhotoURL]; }],
  746. [StaticContentTableViewCell cellWithTitle:kReloadText
  747. action:^{ [weakSelf reloadUser]; }],
  748. [StaticContentTableViewCell cellWithTitle:kGetProvidersForEmail
  749. action:^{ [weakSelf getProvidersForEmail]; }],
  750. [StaticContentTableViewCell cellWithTitle:kGetAllSignInMethodsForEmail
  751. action:^{ [weakSelf getAllSignInMethodsForEmail]; }],
  752. [StaticContentTableViewCell cellWithTitle:kUpdateEmailText
  753. action:^{ [weakSelf updateEmail]; }],
  754. [StaticContentTableViewCell cellWithTitle:kUpdatePasswordText
  755. action:^{ [weakSelf updatePassword]; }],
  756. [StaticContentTableViewCell cellWithTitle:kDeleteUserText
  757. action:^{ [weakSelf deleteAccount]; }],
  758. ]],
  759. [StaticContentTableViewSection sectionWithTitle:kSectionTitleOOBActions cells:@[
  760. [StaticContentTableViewCell cellWithTitle:kActionCodeTypeDescription
  761. value:[self actionCodeRequestTypeString]
  762. action:^{ [weakSelf toggleActionCodeRequestType]; }],
  763. [StaticContentTableViewCell cellWithTitle:kContinueURLDescription
  764. value:_actionCodeContinueURL.absoluteString ?: @"(nil)"
  765. action:^{ [weakSelf changeActionCodeContinueURL]; }],
  766. [StaticContentTableViewCell cellWithTitle:kRequestVerifyEmail
  767. action:^{ [weakSelf requestVerifyEmail]; }],
  768. [StaticContentTableViewCell cellWithTitle:kRequestPasswordReset
  769. action:^{ [weakSelf requestPasswordReset]; }],
  770. [StaticContentTableViewCell cellWithTitle:kResetPassword
  771. action:^{ [weakSelf resetPassword]; }],
  772. [StaticContentTableViewCell cellWithTitle:kCheckActionCode
  773. action:^{ [weakSelf checkActionCode]; }],
  774. [StaticContentTableViewCell cellWithTitle:kApplyActionCode
  775. action:^{ [weakSelf applyActionCode]; }],
  776. [StaticContentTableViewCell cellWithTitle:kVerifyPasswordResetCode
  777. action:^{ [weakSelf verifyPasswordResetCode]; }],
  778. ]],
  779. [StaticContentTableViewSection sectionWithTitle:kSectionTitleReauthenticate cells:@[
  780. [StaticContentTableViewCell cellWithTitle:kReauthenticateGoogleText
  781. action:^{ [weakSelf reauthenticateGoogle]; }],
  782. [StaticContentTableViewCell
  783. cellWithTitle:kReauthenticateGoogleAndRetrieveDataText
  784. action:^{ [weakSelf reauthenticateGoogleAndRetrieveData]; }],
  785. [StaticContentTableViewCell cellWithTitle:kReauthenticateFBText
  786. action:^{ [weakSelf reauthenticateFB]; }],
  787. [StaticContentTableViewCell cellWithTitle:kReauthenticateFBAndRetrieveDataText
  788. action:^{ [weakSelf reauthenticateFBAndRetrieveData]; }],
  789. [StaticContentTableViewCell cellWithTitle:kReauthenticateEmailText
  790. action:^{ [weakSelf reauthenticateEmailPassword]; }]
  791. ]],
  792. [StaticContentTableViewSection sectionWithTitle:kSectionTitleTokenActions cells:@[
  793. [StaticContentTableViewCell cellWithTitle:kTokenGetButtonText
  794. action:^{ [weakSelf getUserTokenWithForce:NO]; }],
  795. [StaticContentTableViewCell cellWithTitle:kTokenRefreshButtonText
  796. action:^{ [weakSelf getUserTokenWithForce:YES]; }],
  797. [StaticContentTableViewCell cellWithTitle:kGetTokenResultButtonText
  798. action:^{ [weakSelf getUserTokenResultWithForce:NO]; }],
  799. [StaticContentTableViewCell cellWithTitle:kGetTokenResultForceButtonText
  800. action:^{ [weakSelf getUserTokenResultWithForce:YES]; }],
  801. ]],
  802. [StaticContentTableViewSection sectionWithTitle:kSectionTitleLinkUnlinkAccounts cells:@[
  803. [StaticContentTableViewCell cellWithTitle:kLinkWithGoogleText
  804. action:^{ [weakSelf linkWithGoogle]; }],
  805. [StaticContentTableViewCell cellWithTitle:kLinkWithGoogleAndRetrieveDataText
  806. action:^{ [weakSelf linkWithGoogleAndRetrieveData]; }],
  807. [StaticContentTableViewCell cellWithTitle:kLinkWithFacebookText
  808. action:^{ [weakSelf linkWithFacebook]; }],
  809. [StaticContentTableViewCell cellWithTitle:kLinkWithFacebookAndRetrieveDataText
  810. action:^{ [weakSelf linkWithFacebookAndRetrieveData]; }],
  811. [StaticContentTableViewCell cellWithTitle:kLinkWithEmailPasswordText
  812. action:^{ [weakSelf linkWithEmailPassword]; }],
  813. [StaticContentTableViewCell cellWithTitle:kUnlinkFromGoogle
  814. action:^{
  815. [weakSelf unlinkFromProvider:FIRGoogleAuthProviderID completion:nil];
  816. }],
  817. [StaticContentTableViewCell cellWithTitle:kUnlinkFromFacebook
  818. action:^{
  819. [weakSelf unlinkFromProvider:FIRFacebookAuthProviderID completion:nil];
  820. }],
  821. [StaticContentTableViewCell cellWithTitle:kUnlinkFromEmailPassword
  822. action:^{
  823. [weakSelf unlinkFromProvider:FIREmailAuthProviderID completion:nil];
  824. }]
  825. ]],
  826. [StaticContentTableViewSection sectionWithTitle:kSectionTitleApp cells:@[
  827. [StaticContentTableViewCell cellWithTitle:kDeleteAppTitle
  828. action:^{ [weakSelf deleteApp]; }],
  829. [StaticContentTableViewCell cellWithTitle:kTokenGetButtonText
  830. action:^{ [weakSelf getAppTokenWithForce:NO]; }],
  831. [StaticContentTableViewCell cellWithTitle:kTokenRefreshButtonText
  832. action:^{ [weakSelf getAppTokenWithForce:YES]; }],
  833. [StaticContentTableViewCell cellWithTitle:kTimeAuthInitTitle
  834. action:^{ [weakSelf timeAuthInitialization]; }]
  835. ]],
  836. [StaticContentTableViewSection sectionWithTitle:kSectionTitleListeners cells:@[
  837. [StaticContentTableViewCell cellWithTitle:kAddAuthStateListenerTitle
  838. action:^{ [weakSelf addAuthStateListener]; }],
  839. [StaticContentTableViewCell cellWithTitle:kRemoveAuthStateListenerTitle
  840. action:^{ [weakSelf removeAuthStateListener]; }],
  841. [StaticContentTableViewCell cellWithTitle:kAddIDTokenListenerTitle
  842. action:^{ [weakSelf addIDTokenListener]; }],
  843. [StaticContentTableViewCell cellWithTitle:kRemoveIDTokenListenerTitle
  844. action:^{ [weakSelf removeIDTokenListener]; }],
  845. ]],
  846. [StaticContentTableViewSection sectionWithTitle:kSectionTitleManualTests cells:@[
  847. [StaticContentTableViewCell cellWithTitle:kAutoBYOAuthTitle
  848. action:^{ [weakSelf automatedBYOAuth]; }],
  849. [StaticContentTableViewCell cellWithTitle:kAutoSignInGoogle
  850. action:^{ [weakSelf automatedSignInGoogle]; }],
  851. [StaticContentTableViewCell cellWithTitle:kAutoSignInFacebook
  852. action:^{ [weakSelf automatedSignInFacebook]; }],
  853. [StaticContentTableViewCell cellWithTitle:kAutoSignUpEmailPassword
  854. action:^{ [weakSelf automatedEmailSignUp]; }],
  855. [StaticContentTableViewCell cellWithTitle:kAutoSignInAnonymously
  856. action:^{ [weakSelf automatedAnonymousSignIn]; }],
  857. [StaticContentTableViewCell cellWithTitle:kAutoAccountLinking
  858. action:^{ [weakSelf automatedAccountLinking]; }],
  859. [StaticContentTableViewCell cellWithTitle:kAutoPhoneNumberSignIn
  860. action:^{ [weakSelf automatedPhoneNumberSignIn]; }]
  861. ]]
  862. ]];
  863. }
  864. #pragma mark - Interface Builder Actions
  865. - (IBAction)userToUseDidChange:(UISegmentedControl *)sender {
  866. _useUserInMemory = (sender.selectedSegmentIndex == 1);
  867. }
  868. - (IBAction)memoryPlus {
  869. _userInMemory = [AppManager auth].currentUser;
  870. [self updateUserInfo];
  871. }
  872. - (IBAction)memoryClear {
  873. _userInMemory = nil;
  874. [self updateUserInfo];
  875. }
  876. /** @fn parseURL
  877. @brief Parses an incoming URL into all available query items.
  878. @param urlString The url to be parsed.
  879. @return A dictionary of available query items in the target URL.
  880. */
  881. static NSDictionary<NSString *, NSString *> *parseURL(NSString *urlString) {
  882. NSString *linkURL = [NSURLComponents componentsWithString:urlString].query;
  883. NSArray<NSString *> *URLComponents = [linkURL componentsSeparatedByString:@"&"];
  884. NSMutableDictionary<NSString *, NSString *> *queryItems =
  885. [[NSMutableDictionary alloc] initWithCapacity:URLComponents.count];
  886. for (NSString *component in URLComponents) {
  887. NSRange equalRange = [component rangeOfString:@"="];
  888. if (equalRange.location != NSNotFound) {
  889. NSString *queryItemKey =
  890. [[component substringToIndex:equalRange.location] stringByRemovingPercentEncoding];
  891. NSString *queryItemValue =
  892. [[component substringFromIndex:equalRange.location + 1] stringByRemovingPercentEncoding];
  893. if (queryItemKey && queryItemValue) {
  894. queryItems[queryItemKey] = queryItemValue;
  895. }
  896. }
  897. }
  898. return queryItems;
  899. }
  900. #pragma mark public methods
  901. - (BOOL)handleIncomingLinkWithURL:(NSURL *)URL {
  902. // Parse the query portion of the incoming URL.
  903. NSDictionary<NSString *, NSString *> *queryItems =
  904. parseURL([NSURLComponents componentsWithString:URL.absoluteString].query);
  905. // Check that all necessary query items are available.
  906. NSString *actionCode = queryItems[@"oobCode"];
  907. NSString *mode = queryItems[@"mode"];
  908. if (!actionCode || !mode) {
  909. return NO;
  910. }
  911. // Handle Password Reset action.
  912. if ([mode isEqualToString:kPasswordResetAction]) {
  913. [self showTextInputPromptWithMessage:@"New Password:"
  914. completionBlock:^(BOOL userPressedOK, NSString *_Nullable newPassword) {
  915. if (!userPressedOK || !newPassword.length) {
  916. [UIPasteboard generalPasteboard].string = actionCode;
  917. return;
  918. }
  919. [self showSpinner:^() {
  920. [[AppManager auth] confirmPasswordResetWithCode:actionCode
  921. newPassword:newPassword
  922. completion:^(NSError *_Nullable error) {
  923. [self hideSpinner:^{
  924. if (error) {
  925. [self logFailure:@"Password reset in app failed" error:error];
  926. [self showMessagePrompt:error.localizedDescription];
  927. return;
  928. }
  929. [self logSuccess:@"Password reset in app succeeded."];
  930. [self showMessagePrompt:@"Password reset in app succeeded."];
  931. }];
  932. }];
  933. }];
  934. }];
  935. return YES;
  936. }
  937. if ([mode isEqualToString:kVerifyEmailAction]) {
  938. [self showMessagePromptWithTitle:@"Tap OK to verify email"
  939. message:actionCode
  940. showCancelButton:YES
  941. completion:^(BOOL userPressedOK, NSString *_Nullable userInput) {
  942. if (!userPressedOK) {
  943. return;
  944. }
  945. [self showSpinner:^() {
  946. [[AppManager auth] applyActionCode:actionCode completion:^(NSError *_Nullable error) {
  947. [self hideSpinner:^{
  948. if (error) {
  949. [self logFailure:@"Verify email in app failed" error:error];
  950. [self showMessagePrompt:error.localizedDescription];
  951. return;
  952. }
  953. [self logSuccess:@"Verify email in app succeeded."];
  954. [self showMessagePrompt:@"Verify email in app succeeded."];
  955. }];
  956. }];
  957. }];
  958. }];
  959. return YES;
  960. }
  961. return NO;
  962. }
  963. #pragma mark - Actions
  964. /** @fn signInWithProvider:provider:
  965. @brief Perform sign in with credential operataion, for given auth provider.
  966. @param provider The auth provider.
  967. @param callback The callback to continue the flow which executed this sign-in.
  968. */
  969. - (void)signInWithProvider:(nonnull id<AuthProvider>)provider callback:(void(^)(void))callback {
  970. if (!provider) {
  971. [self logFailedTest:@"A valid auth provider was not provided to the signInWithProvider."];
  972. return;
  973. }
  974. [provider getAuthCredentialWithPresentingViewController:self
  975. callback:^(FIRAuthCredential *credential,
  976. NSError *error) {
  977. if (!credential) {
  978. [self logFailedTest:@"The test needs a valid credential to continue."];
  979. return;
  980. }
  981. [[AppManager auth] signInAndRetrieveDataWithCredential:credential
  982. completion:^(FIRAuthDataResult *_Nullable result,
  983. NSError *_Nullable error) {
  984. if (error) {
  985. [self logFailure:@"sign-in with provider failed" error:error];
  986. [self logFailedTest:@"Sign-in should succeed"];
  987. return;
  988. } else {
  989. [self logSuccess:@"sign-in with provider succeeded."];
  990. callback();
  991. }
  992. }];
  993. }];
  994. }
  995. /** @fn automatedSignInGoogle
  996. @brief Automatically executes the manual test for sign-in with Google.
  997. */
  998. - (void)automatedSignInGoogle {
  999. [self showMessagePromptWithTitle:kAutoSignInGoogle
  1000. message:kSafariGoogleSignOutMessagePrompt
  1001. showCancelButton:NO
  1002. completion:^(BOOL userPressedOK, NSString *_Nullable userInput) {
  1003. FIRAuth *auth = [AppManager auth];
  1004. if (!auth) {
  1005. [self logFailedTest:@"Could not obtain auth object."];
  1006. return;
  1007. }
  1008. [auth signOut:NULL];
  1009. [self log:@"INITIATING AUTOMATED MANUAL TEST FOR GOOGLE SIGN IN:"];
  1010. [self signInWithProvider:[AuthProviders google] callback:^{
  1011. [self logSuccess:@"sign-in with Google provider succeeded."];
  1012. [auth signOut:NULL];
  1013. [self signInWithProvider:[AuthProviders google] callback:^{
  1014. [self logSuccess:@"sign-in with Google provider succeeded."];
  1015. [self updateEmailPasswordWithCompletion:^{
  1016. [self automatedSignInGoogleDisplayNamePhotoURL];
  1017. }];
  1018. }];
  1019. }];
  1020. }];
  1021. }
  1022. /** @fn automatedSignInGoogleDisplayNamePhotoURL
  1023. @brief Automatically executes the manual test for setting email and password for sign in with
  1024. Google.
  1025. */
  1026. - (void)automatedSignInGoogleDisplayNamePhotoURL {
  1027. [self signInWithProvider:[AuthProviders google] callback:^{
  1028. [self updateDisplayNameAndPhotoURlWithCompletion:^{
  1029. [self log:@"FINISHED AUTOMATED MANUAL TEST FOR SIGN-IN WITH GOOGlE."];
  1030. [self reloadUser];
  1031. }];
  1032. }];
  1033. }
  1034. /** @fn automatedSignInFacebook
  1035. @brief Automatically executes the manual test for sign-in with Facebook.
  1036. */
  1037. - (void)automatedSignInFacebook {
  1038. [self showMessagePromptWithTitle:kAutoSignInFacebook
  1039. message:kSafariFacebookSignOutMessagePrompt
  1040. showCancelButton:NO
  1041. completion:^(BOOL userPressedOK, NSString *_Nullable userInput) {
  1042. FIRAuth *auth = [AppManager auth];
  1043. if (!auth) {
  1044. [self logFailedTest:@"Could not obtain auth object."];
  1045. return;
  1046. }
  1047. [auth signOut:NULL];
  1048. [self log:@"INITIATING AUTOMATED MANUAL TEST FOR FACEBOOK SIGN IN:"];
  1049. [self signInWithProvider:[AuthProviders facebook] callback:^{
  1050. [self logSuccess:@"sign-in with Facebook provider succeeded."];
  1051. [auth signOut:NULL];
  1052. [self signInWithProvider:[AuthProviders facebook] callback:^{
  1053. [self logSuccess:@"sign-in with Facebook provider succeeded."];
  1054. [self updateEmailPasswordWithCompletion:^{
  1055. [self automatedSignInFacebookDisplayNamePhotoURL];
  1056. }];
  1057. }];
  1058. }];
  1059. }];
  1060. }
  1061. /** @fn automatedPhoneNumberSignIn
  1062. @brief Automatically executes the manual test for sign-in with phone number.
  1063. */
  1064. - (void)automatedPhoneNumberSignIn {
  1065. [self log:@"Automated phone number sign in"];
  1066. FIRAuth *auth = [AppManager auth];
  1067. if (!auth) {
  1068. [self logFailedTest:@"Could not obtain auth object."];
  1069. return;
  1070. }
  1071. [auth signOut:NULL];
  1072. [self log:@"INITIATING AUTOMATED MANUAL TEST FOR PHONE NUMBER SIGN IN:"];
  1073. [self commonPhoneNumberInputWithTitle:@"Phone for automation"
  1074. Completion:^(NSString *_Nullable phone) {
  1075. [self signInWithPhoneNumber:phone completion:^(NSError *error) {
  1076. if (error) {
  1077. [self logFailedTest:@"Could not sign in with phone number reCAPTCHA."];
  1078. }
  1079. [self logSuccess:@"sign-in with phone number reCAPTCHA test succeeded."];
  1080. [auth signOut:NULL];
  1081. [self signInWithPhoneNumber:phone completion:^(NSError *error) {
  1082. if (error) {
  1083. [self logFailedTest:@"Could not sign in with phone number reCAPTCHA."];
  1084. }
  1085. [self logSuccess:@"second sign-in with phone number reCAPTCHA test succeeded."];
  1086. [self updatePhoneNumber:phone completion:^(NSError *error) {
  1087. if (error) {
  1088. [self logFailedTest:@"Could not update phone number."];
  1089. }
  1090. [self logSuccess:@"update phone number test succeeded."];
  1091. [self unlinkFromProvider:FIRPhoneAuthProviderID completion:^(NSError *error) {
  1092. if (error) {
  1093. [self logFailedTest:@"Could not unlink phone number."];
  1094. }
  1095. [self logSuccess:@"unlink phone number test succeeded."];
  1096. [self linkPhoneNumber:phone completion:^(NSError *error) {
  1097. if (error) {
  1098. [self logFailedTest:@"Could not link phone number."];
  1099. }
  1100. [self logSuccess:@"link phone number test succeeded."];
  1101. [self log:@"FINISHED AUTOMATED MANUAL TEST FOR PHONE NUMBER SIGN IN."];
  1102. }];
  1103. }];
  1104. }];
  1105. }];
  1106. }];
  1107. }];
  1108. }
  1109. /** @fn automatedEmailSignUp
  1110. @brief Automatically executes the manual test for sign-up with email/password.
  1111. */
  1112. - (void)automatedEmailSignUp {
  1113. [self log:@"INITIATING AUTOMATED MANUAL TEST FOR FACEBOOK SIGN IN:"];
  1114. FIRAuth *auth = [AppManager auth];
  1115. if (!auth) {
  1116. [self logFailedTest:@"Could not obtain auth object."];
  1117. return;
  1118. }
  1119. [self signUpNewEmail:kFakeEmail password:kFakePassword callback:^(FIRUser *_Nullable user,
  1120. NSError *_Nullable error) {
  1121. if (error) {
  1122. [self logFailedTest: @" Email/Password Account account creation failed"];
  1123. return;
  1124. }
  1125. [auth signOut:NULL];
  1126. FIRAuthCredential *credential = [FIREmailAuthProvider credentialWithEmail:kFakeEmail
  1127. password:kFakePassword];
  1128. [auth signInAndRetrieveDataWithCredential:credential
  1129. completion:^(FIRAuthDataResult *_Nullable result,
  1130. NSError *_Nullable error) {
  1131. if (error) {
  1132. [self logFailure:@"sign-in with Email/Password failed" error:error];
  1133. [self logFailedTest:@"sign-in with Email/Password should succeed."];
  1134. return;
  1135. }
  1136. [self logSuccess:@"sign-in with Email/Password succeeded."];
  1137. [self log:@"FINISHED AUTOMATED MANUAL TEST FOR SIGN-IN WITH EMAIL/PASSWORD."];
  1138. // Delete the user so that we can reuse the fake email address for subsequent tests.
  1139. [auth.currentUser deleteWithCompletion:^(NSError *_Nullable error) {
  1140. if (error) {
  1141. [self logFailure:@"Failed to delete user" error:error];
  1142. [self logFailedTest:@"Deleting a user that was recently signed-in should succeed."];
  1143. return;
  1144. }
  1145. [self logSuccess:@"User deleted."];
  1146. }];
  1147. }];
  1148. }];
  1149. }
  1150. /** @fn automatedAnonymousSignIn
  1151. @brief Automatically executes the manual test for sign-in anonymously.
  1152. */
  1153. - (void)automatedAnonymousSignIn {
  1154. [self log:@"INITIATING AUTOMATED MANUAL TEST FOR ANONYMOUS SIGN IN:"];
  1155. FIRAuth *auth = [AppManager auth];
  1156. if (!auth) {
  1157. [self logFailedTest:@"Could not obtain auth object."];
  1158. return;
  1159. }
  1160. [auth signOut:NULL];
  1161. [self signInAnonymouslyWithCallback:^(FIRAuthDataResult *_Nullable result,
  1162. NSError *_Nullable error) {
  1163. if (result.user) {
  1164. NSString *anonymousUID = result.user.uid;
  1165. [self signInAnonymouslyWithCallback:^(FIRAuthDataResult *_Nullable user,
  1166. NSError *_Nullable error) {
  1167. if (![result.user.uid isEqual:anonymousUID]) {
  1168. [self logFailedTest:@"Consecutive anonymous sign-ins should yeild the same User ID"];
  1169. return;
  1170. }
  1171. [self log:@"FINISHED AUTOMATED MANUAL TEST FOR ANONYMOUS SIGN IN."];
  1172. }];
  1173. }
  1174. }];
  1175. }
  1176. /** @fn signInAnonymouslyWithCallback:
  1177. @brief Performs anonymous sign in and then executes callback.
  1178. @param callback The callback to be executed.
  1179. */
  1180. - (void)signInAnonymouslyWithCallback:(nullable FIRAuthDataResultCallback)callback {
  1181. FIRAuth *auth = [AppManager auth];
  1182. if (!auth) {
  1183. [self logFailedTest:@"Could not obtain auth object."];
  1184. return;
  1185. }
  1186. [auth signInAnonymouslyWithCompletion:^(FIRAuthDataResult *_Nullable result,
  1187. NSError *_Nullable error) {
  1188. if (error) {
  1189. [self logFailure:@"sign-in anonymously failed" error:error];
  1190. [self logFailedTest:@"Recently signed out user should be able to sign in anonymously."];
  1191. return;
  1192. }
  1193. [self logSuccess:@"sign-in anonymously succeeded."];
  1194. if (callback) {
  1195. callback(result, nil);
  1196. }
  1197. }];
  1198. }
  1199. /** @fn automatedAccountLinking
  1200. @brief Automatically executes the manual test for account linking.
  1201. */
  1202. - (void)automatedAccountLinking {
  1203. [self log:@"INITIATING AUTOMATED MANUAL TEST FOR ACCOUNT LINKING:"];
  1204. FIRAuth *auth = [AppManager auth];
  1205. if (!auth) {
  1206. [self logFailedTest:@"Could not obtain auth object."];
  1207. return;
  1208. }
  1209. [auth signOut:NULL];
  1210. [self signInAnonymouslyWithCallback:^(FIRAuthDataResult *_Nullable result,
  1211. NSError *_Nullable error) {
  1212. if (result.user) {
  1213. NSString *anonymousUID = result.user.uid;
  1214. [self showMessagePromptWithTitle:@"Sign In Instructions"
  1215. message:kUnlinkAccountMessagePrompt
  1216. showCancelButton:NO
  1217. completion:^(BOOL userPressedOK, NSString *_Nullable userInput) {
  1218. [[AuthProviders google]
  1219. getAuthCredentialWithPresentingViewController:self
  1220. callback:^(FIRAuthCredential *credential,
  1221. NSError *error) {
  1222. if (credential) {
  1223. [result.user linkAndRetrieveDataWithCredential:credential
  1224. completion:^(FIRAuthDataResult *result,
  1225. NSError *error) {
  1226. FIRUser *user = result.user;
  1227. if (error) {
  1228. [self logFailure:@"link auth provider failed" error:error];
  1229. [self logFailedTest:@"Account needs to be linked to complete the test."];
  1230. return;
  1231. }
  1232. [self logSuccess:@"link auth provider succeeded."];
  1233. if (user.isAnonymous) {
  1234. [self logFailure:@"link auth provider failed, user still anonymous" error:error];
  1235. [self logFailedTest:@"Account needs to be linked to complete the test."];
  1236. }
  1237. if (![user.uid isEqual:anonymousUID]){
  1238. [self logFailedTest:@"link auth provider failed, UID's are different. Make sure "
  1239. "you link an account that has NOT been Linked nor Signed-In before."];
  1240. return;
  1241. }
  1242. [self log:@"FINISHED AUTOMATED MANUAL TEST FOR ACCOUNT LINKING."];
  1243. }];
  1244. }
  1245. }];
  1246. }];
  1247. }
  1248. }];
  1249. }
  1250. /** @fn automatedSignInFacebookDisplayNamePhotoURL
  1251. @brief Automatically executes the manual test for setting email and password for sign-in with
  1252. Facebook.
  1253. */
  1254. - (void)automatedSignInFacebookDisplayNamePhotoURL {
  1255. [self signInWithProvider:[AuthProviders facebook] callback:^{
  1256. [self updateDisplayNameAndPhotoURlWithCompletion:^{
  1257. [self log:@"FINISHED AUTOMATED MANUAL TEST FOR SIGN-IN WITH FACEBOOK."];
  1258. [self reloadUser];
  1259. }];
  1260. }];
  1261. }
  1262. /** @fn automatedBYOauth
  1263. @brief Automatically executes the manual test for BYOAuth.
  1264. */
  1265. - (void)automatedBYOAuth {
  1266. [self log:@"INITIATING AUTOMATED MANUAL TEST FOR BYOAUTH:"];
  1267. [self showSpinner:^{
  1268. NSError *error;
  1269. NSString *customToken = [NSString stringWithContentsOfURL:[NSURL URLWithString:kCustomTokenUrl]
  1270. encoding:NSUTF8StringEncoding
  1271. error:&error];
  1272. NSString *expiredCustomToken =
  1273. [NSString stringWithContentsOfURL:[NSURL URLWithString:kExpiredCustomTokenUrl]
  1274. encoding:NSUTF8StringEncoding
  1275. error:&error];
  1276. [self hideSpinner:^{
  1277. if (error) {
  1278. [self log:@"There was an error retrieving the custom token."];
  1279. return;
  1280. }
  1281. FIRAuth *auth = [AppManager auth];
  1282. [auth signInWithCustomToken:customToken
  1283. completion:^(FIRAuthDataResult *_Nullable result, NSError *_Nullable error) {
  1284. if (error) {
  1285. [self logFailure:@"sign-in with custom token failed" error:error];
  1286. [self logFailedTest:@"A fresh custom token should succeed in signing-in."];
  1287. return;
  1288. }
  1289. [self logSuccess:@"sign-in with custom token succeeded."];
  1290. [auth.currentUser getIDTokenForcingRefresh:NO
  1291. completion:^(NSString *_Nullable token,
  1292. NSError *_Nullable error) {
  1293. if (error) {
  1294. [self logFailure:@"refresh token failed" error:error];
  1295. [self logFailedTest:@"Refresh token should be available."];
  1296. return;
  1297. }
  1298. [self logSuccess:@"refresh token succeeded."];
  1299. [auth signOut:NULL];
  1300. [auth signInWithCustomToken:expiredCustomToken
  1301. completion:^(FIRAuthDataResult *_Nullable result,
  1302. NSError *_Nullable error) {
  1303. if (!error) {
  1304. [self logSuccess:@"sign-in with custom token succeeded."];
  1305. [self logFailedTest:@"sign-in with an expired custom token should NOT succeed."];
  1306. return;
  1307. }
  1308. [self logFailure:@"sign-in with custom token failed" error:error];
  1309. [auth signInWithCustomToken:kInvalidCustomToken
  1310. completion:^(FIRAuthDataResult *_Nullable result,
  1311. NSError *_Nullable error) {
  1312. if (!error) {
  1313. [self logSuccess:@"sign-in with custom token succeeded."];
  1314. [self logFailedTest:@"sign-in with an invalid custom token should NOT succeed."];
  1315. return;
  1316. }
  1317. [self logFailure:@"sign-in with custom token failed" error:error];
  1318. //next step of automated test.
  1319. [self automatedBYOAuthEmailPassword];
  1320. }];
  1321. }];
  1322. }];
  1323. }];
  1324. }];
  1325. }];
  1326. }
  1327. /** @fn automatedBYOAuthEmailPassword
  1328. @brief Automatically executes the manual test for setting email and password in BYOAuth.
  1329. */
  1330. - (void)automatedBYOAuthEmailPassword {
  1331. [self showSpinner:^{
  1332. NSError *error;
  1333. NSString *customToken = [NSString stringWithContentsOfURL:[NSURL URLWithString:kCustomTokenUrl]
  1334. encoding:NSUTF8StringEncoding
  1335. error:&error];
  1336. [self hideSpinner:^{
  1337. if (error) {
  1338. [self log:@"There was an error retrieving the custom token."];
  1339. return;
  1340. }
  1341. [[AppManager auth] signInWithCustomToken:customToken
  1342. completion:^(FIRAuthDataResult *_Nullable user,
  1343. NSError *_Nullable error) {
  1344. if (error) {
  1345. [self logFailure:@"sign-in with custom token failed" error:error];
  1346. [self logFailedTest:@"A fresh custom token should succeed in signing-in."];
  1347. return;
  1348. }
  1349. [self logSuccess:@"sign-in with custom token succeeded."];
  1350. [self updateEmailPasswordWithCompletion:^{
  1351. [self automatedBYOAuthDisplayNameAndPhotoURl];
  1352. }];
  1353. }];
  1354. }];
  1355. }];
  1356. }
  1357. /** @fn automatedBYOAuthDisplayNameAndPhotoURl
  1358. @brief Automatically executes the manual test for setting display name and photo url in BYOAuth.
  1359. */
  1360. - (void)automatedBYOAuthDisplayNameAndPhotoURl {
  1361. [self showSpinner:^{
  1362. NSError *error;
  1363. NSString *customToken = [NSString stringWithContentsOfURL:[NSURL URLWithString:kCustomTokenUrl]
  1364. encoding:NSUTF8StringEncoding
  1365. error:&error];
  1366. [self hideSpinner:^{
  1367. if (error) {
  1368. [self log:@"There was an error retrieving the custom token."];
  1369. return;
  1370. }
  1371. [[AppManager auth] signInWithCustomToken:customToken
  1372. completion:^(FIRAuthDataResult *_Nullable result,
  1373. NSError *_Nullable error) {
  1374. if (error) {
  1375. [self logFailure:@"sign-in with custom token failed" error:error];
  1376. [self logFailedTest:@"A fresh custom token should succeed in signing-in."];
  1377. return;
  1378. }
  1379. [self logSuccess:@"sign-in with custom token succeeded."];
  1380. [self updateDisplayNameAndPhotoURlWithCompletion:^{
  1381. [self log:@"FINISHED AUTOMATED MANUAL TEST FOR BYOAUTH."];
  1382. [self reloadUser];
  1383. }];
  1384. }];
  1385. }];
  1386. }];
  1387. }
  1388. /** @fn updateEmailPasswordWithCompletion:
  1389. @brief Updates email and password for automatic manual tests, and signs user in with new email
  1390. and password.
  1391. @param completion The completion block to continue the automatic test flow.
  1392. */
  1393. - (void)updateEmailPasswordWithCompletion:(void(^)(void))completion {
  1394. FIRAuth *auth = [AppManager auth];
  1395. [auth.currentUser updateEmail:kFakeEmail completion:^(NSError *_Nullable error) {
  1396. if (error) {
  1397. [self logFailure:@"update email failed" error:error];
  1398. [self logFailedTest:@"Update email should succeed when properly signed-in."];
  1399. return;
  1400. }
  1401. [self logSuccess:@"update email succeeded."];
  1402. [auth.currentUser updatePassword:kFakePassword completion:^(NSError *_Nullable error) {
  1403. if (error) {
  1404. [self logFailure:@"update password failed" error:error];
  1405. [self logFailedTest:@"Update password should succeed when properly signed-in."];
  1406. return;
  1407. }
  1408. [self logSuccess:@"update password succeeded."];
  1409. [auth signOut:NULL];
  1410. FIRAuthCredential *credential =
  1411. [FIREmailAuthProvider credentialWithEmail:kFakeEmail password:kFakePassword];
  1412. [auth signInAndRetrieveDataWithCredential:credential
  1413. completion:^(FIRAuthDataResult *_Nullable result,
  1414. NSError *_Nullable error) {
  1415. if (error) {
  1416. [self logFailure:@"sign-in with Email/Password failed" error:error];
  1417. [self logFailedTest:@"sign-in with Email/Password should succeed."];
  1418. return;
  1419. }
  1420. [self logSuccess:@"sign-in with Email/Password succeeded."];
  1421. // Delete the user so that we can reuse the fake email address for subsequent tests.
  1422. [auth.currentUser deleteWithCompletion:^(NSError *_Nullable error) {
  1423. if (error) {
  1424. [self logFailure:@"Failed to delete user." error:error];
  1425. [self logFailedTest:@"Deleting a user that was recently signed-in should succeed"];
  1426. return;
  1427. }
  1428. [self logSuccess:@"User deleted."];
  1429. completion();
  1430. }];
  1431. }];
  1432. }];
  1433. }];
  1434. }
  1435. /** @fn updateDisplayNameAndPhotoURlWithCompletion:
  1436. @brief Automatically executes the manual test for setting displayName and photoUrl.
  1437. @param completion The completion block to continue the automatic test flow.
  1438. */
  1439. - (void)updateDisplayNameAndPhotoURlWithCompletion:(void(^)(void))completion {
  1440. FIRAuth *auth = [AppManager auth];
  1441. FIRUserProfileChangeRequest *changeRequest = [auth.currentUser profileChangeRequest];
  1442. changeRequest.photoURL = [NSURL URLWithString:kFakeDisplayPhotoUrl];
  1443. [changeRequest commitChangesWithCompletion:^(NSError *_Nullable error) {
  1444. if (error) {
  1445. [self logFailure:@"set photo URL failed" error:error];
  1446. [self logFailedTest:@"Change photo Url should succeed when signed-in."];
  1447. return;
  1448. }
  1449. [self logSuccess:@"set PhotoURL succeeded."];
  1450. FIRUserProfileChangeRequest *changeRequest = [auth.currentUser profileChangeRequest];
  1451. changeRequest.displayName = kFakeDisplayName;
  1452. [changeRequest commitChangesWithCompletion:^(NSError *_Nullable error) {
  1453. if (error) {
  1454. [self logFailure:@"set display name failed" error:error];
  1455. [self logFailedTest:@"Change display name should succeed when signed-in."];
  1456. return;
  1457. }
  1458. [self logSuccess:@"set display name succeeded."];
  1459. completion();
  1460. }];
  1461. }];
  1462. }
  1463. /** @fn addAuthStateListener
  1464. @brief Adds an auth state did change listener (block).
  1465. */
  1466. - (void)addAuthStateListener {
  1467. __weak typeof(self) weakSelf = self;
  1468. NSUInteger index = _authStateDidChangeListeners.count;
  1469. [self log:[NSString stringWithFormat:@"Auth State Did Change Listener #%lu was added.",
  1470. (unsigned long)index]];
  1471. FIRAuthStateDidChangeListenerHandle handle =
  1472. [[AppManager auth] addAuthStateDidChangeListener:^(FIRAuth *_Nonnull auth,
  1473. FIRUser *_Nullable user) {
  1474. [weakSelf log:[NSString stringWithFormat:
  1475. @"Auth State Did Change Listener #%lu was invoked on user '%@'.",
  1476. (unsigned long)index, user.uid]];
  1477. }];
  1478. [_authStateDidChangeListeners addObject:handle];
  1479. }
  1480. /** @fn removeAuthStateListener
  1481. @brief Removes an auth state did change listener (block).
  1482. */
  1483. - (void)removeAuthStateListener {
  1484. if (!_authStateDidChangeListeners.count) {
  1485. [self log:@"No remaining Auth State Did Change Listeners."];
  1486. return;
  1487. }
  1488. NSUInteger index = _authStateDidChangeListeners.count - 1;
  1489. FIRAuthStateDidChangeListenerHandle handle = _authStateDidChangeListeners.lastObject;
  1490. [[AppManager auth] removeAuthStateDidChangeListener:handle];
  1491. [_authStateDidChangeListeners removeObject:handle];
  1492. NSString *logString =
  1493. [NSString stringWithFormat:@"Auth State Did Change Listener #%lu was removed.",
  1494. (unsigned long)index];
  1495. [self log:logString];
  1496. }
  1497. /** @fn addIDTokenListener
  1498. @brief Adds an ID token did change listener (block).
  1499. */
  1500. - (void)addIDTokenListener {
  1501. __weak typeof(self) weakSelf = self;
  1502. NSUInteger index = _IDTokenDidChangeListeners.count;
  1503. [self log:[NSString stringWithFormat:@"ID Token Did Change Listener #%lu was added.",
  1504. (unsigned long)index]];
  1505. FIRIDTokenDidChangeListenerHandle handle =
  1506. [[AppManager auth] addIDTokenDidChangeListener:^(FIRAuth *_Nonnull auth,
  1507. FIRUser *_Nullable user) {
  1508. [weakSelf log:[NSString stringWithFormat:
  1509. @"ID Token Did Change Listener #%lu was invoked on user '%@'.",
  1510. (unsigned long)index, user.uid]];
  1511. }];
  1512. [_IDTokenDidChangeListeners addObject:handle];
  1513. }
  1514. /** @fn removeIDTokenListener
  1515. @brief Removes an ID token did change listener (block).
  1516. */
  1517. - (void)removeIDTokenListener {
  1518. if (!_IDTokenDidChangeListeners.count) {
  1519. [self log:@"No remaining ID Token Did Change Listeners."];
  1520. return;
  1521. }
  1522. NSUInteger index = _IDTokenDidChangeListeners.count - 1;
  1523. FIRIDTokenDidChangeListenerHandle handle = _IDTokenDidChangeListeners.lastObject;
  1524. [[AppManager auth] removeIDTokenDidChangeListener:handle];
  1525. [_IDTokenDidChangeListeners removeObject:handle];
  1526. NSString *logString =
  1527. [NSString stringWithFormat:@"ID Token Did Change Listener #%lu was removed.",
  1528. (unsigned long)index];
  1529. [self log:logString];
  1530. }
  1531. /** @fn log:
  1532. @brief Prints a log message to the sample app console.
  1533. @param string The string to add to the console.
  1534. */
  1535. - (void)log:(NSString *)string {
  1536. dispatch_async(dispatch_get_main_queue(), ^{
  1537. NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
  1538. dateFormatter.dateFormat = @"yyyy-MM-dd HH:mm:ss";
  1539. NSString *date = [dateFormatter stringFromDate:[NSDate date]];
  1540. if (!_consoleString) {
  1541. _consoleString = [NSMutableString string];
  1542. }
  1543. [_consoleString appendString:[NSString stringWithFormat:@"%@ %@\n", date, string]];
  1544. _consoleTextView.text = _consoleString;
  1545. CGRect targetRect = CGRectMake(0, _consoleTextView.contentSize.height - 1, 1, 1);
  1546. [_consoleTextView scrollRectToVisible:targetRect animated:YES];
  1547. });
  1548. }
  1549. /** @fn logSuccess:
  1550. @brief Wraps a string into a succeful log message format.
  1551. @param string Part of the log message.
  1552. @remarks The @string parameter should be a string ending with a period, as it is the end of the
  1553. log message.
  1554. */
  1555. - (void)logSuccess:(NSString *)string {
  1556. [self log:[NSString stringWithFormat:@"SUCCESS: %@", string]];
  1557. }
  1558. /** @fn logFailure:
  1559. @brief Wraps a string into a failed log message format.
  1560. @param string Part of the message to wrap.
  1561. @remarks The @string parameter should be a string that NEVER ends with a period, as it is
  1562. guaranteed not to be the last part fo the log message.
  1563. */
  1564. - (void)logFailure:(NSString *)string error:(NSError *) error {
  1565. NSString *message =
  1566. [NSString stringWithFormat:@"FAILURE: %@ Error Description: %@.", string, error.description];
  1567. [self log:message];
  1568. }
  1569. /** @fn logTestFailed
  1570. @brief Logs test failure to the console.
  1571. @param reason The reason why the test is considered a failure.
  1572. @remarks The calling method should immediately terminate after invoking this method i.e by
  1573. return statement or end of fucntions. The @reason parameter should be a string ending with a
  1574. period, as it is the end of the log message.
  1575. */
  1576. - (void)logFailedTest:( NSString *_Nonnull )reason {
  1577. [self log:[NSString stringWithFormat:@"FAILIURE: TEST FAILED - %@", reason]];
  1578. }
  1579. /** @fn presentSettings
  1580. @brief Invoked when the settings row is pressed.
  1581. */
  1582. - (void)presentSettings {
  1583. SettingsViewController *settingsViewController = [[SettingsViewController alloc]
  1584. initWithNibName:NSStringFromClass([SettingsViewController class])
  1585. bundle:nil];
  1586. [self showViewController:settingsViewController sender:self];
  1587. }
  1588. /** @fn presentUserInfo
  1589. @brief Invoked when the user info row is pressed.
  1590. */
  1591. - (void)presentUserInfo {
  1592. UserInfoViewController *userInfoViewController =
  1593. [[UserInfoViewController alloc] initWithUser:[AppManager auth].currentUser];
  1594. [self showViewController:userInfoViewController sender:self];
  1595. }
  1596. /** @fn presentUserInMemoryInfo
  1597. @brief Invoked when the user in memory info row is pressed.
  1598. */
  1599. - (void)presentUserInMemoryInfo {
  1600. UserInfoViewController *userInfoViewController =
  1601. [[UserInfoViewController alloc] initWithUser:_userInMemory];
  1602. [self showViewController:userInfoViewController sender:self];
  1603. }
  1604. /** @fn signInGoogle
  1605. @brief Invoked when "Sign in with Google" row is pressed.
  1606. */
  1607. - (void)signInGoogle {
  1608. [self signinWithProvider:[AuthProviders google] retrieveData:YES];
  1609. }
  1610. /** @fn signInGoogleAndRetrieveData
  1611. @brief Invoked when "Sign in with Google and retrieve data" row is pressed.
  1612. */
  1613. - (void)signInGoogleAndRetrieveData {
  1614. [self signinWithProvider:[AuthProviders google] retrieveData:YES];
  1615. }
  1616. - (void)signInGoogleProvider {
  1617. FIROAuthProvider *provider = _googleOAuthProvider;
  1618. provider.customParameters = @{
  1619. @"prompt" : @"consent",
  1620. };
  1621. provider.scopes = @[ @"profile", @"email", @"https://www.googleapis.com/auth/plus.me" ];
  1622. [self showSpinner:^{
  1623. [[AppManager auth] signInWithProvider:provider
  1624. UIDelegate:nil
  1625. completion:^(FIRAuthDataResult *_Nullable authResult,
  1626. NSError *_Nullable error) {
  1627. [self hideSpinner:^{
  1628. if (error) {
  1629. [self logFailure:@"sign-in with provider (Google) failed" error:error];
  1630. } else if (authResult.additionalUserInfo) {
  1631. [self logSuccess:[self stringWithAdditionalUserInfo:authResult.additionalUserInfo]];
  1632. if (_isNewUserToggleOn) {
  1633. NSString *newUserString = authResult.additionalUserInfo.newUser ?
  1634. @"New user" : @"Existing user";
  1635. [self showMessagePromptWithTitle:@"New or Existing"
  1636. message:newUserString
  1637. showCancelButton:NO
  1638. completion:nil];
  1639. }
  1640. }
  1641. [self showTypicalUIForUserUpdateResultsWithTitle:@"Sign-In Error" error:error];
  1642. }];
  1643. }];
  1644. }];
  1645. }
  1646. /** @fn signInGoogleHeadfulLite
  1647. @brief Invoked when "Sign in with Google (headful-lite)" row is pressed.
  1648. */
  1649. - (void)signInGoogleHeadfulLite {
  1650. FIROAuthProvider *provider = _googleOAuthProvider;
  1651. provider.customParameters = @{
  1652. @"prompt" : @"consent",
  1653. };
  1654. provider.scopes = @[ @"profile", @"email", @"https://www.googleapis.com/auth/plus.me" ];
  1655. [self showSpinner:^{
  1656. [provider getCredentialWithUIDelegate:nil completion:^(FIRAuthCredential *_Nullable credential,
  1657. NSError *_Nullable error) {
  1658. if (error) {
  1659. [self logFailure:@"sign-in with Google failed" error:error];
  1660. return;
  1661. }
  1662. [[AppManager auth] signInAndRetrieveDataWithCredential:credential
  1663. completion:^(FIRAuthDataResult *_Nullable
  1664. authResult,
  1665. NSError *_Nullable error) {
  1666. [self hideSpinner:^{
  1667. if (error) {
  1668. [self logFailure:@"sign-in with Google failed" error:error];
  1669. return;
  1670. } else {
  1671. [self logSuccess:@"sign-in with Google (headful-lite) succeeded."];
  1672. }
  1673. [self showTypicalUIForUserUpdateResultsWithTitle:@"Sign-In Error" error:error];
  1674. }];
  1675. }];
  1676. }];
  1677. }];
  1678. }
  1679. /** @fn signInMicrosoftHeadfulLite
  1680. @brief Invoked when "Sign in with Microsoft (headful-lite)" row is pressed.
  1681. */
  1682. - (void)signInMicrosoftHeadfulLite {
  1683. FIROAuthProvider *provider = _microsoftOAuthProvider;
  1684. provider.customParameters = @{
  1685. @"prompt" : @"consent",
  1686. @"login_hint" : @"tu8731@gmail.com",
  1687. };
  1688. provider.scopes = @[ @"user.readwrite,calendars.read" ];
  1689. [self showSpinner:^{
  1690. [provider getCredentialWithUIDelegate:nil completion:^(FIRAuthCredential *_Nullable credential,
  1691. NSError *_Nullable error) {
  1692. if (error) {
  1693. [self logFailure:@"sign-in with Microsoft failed" error:error];
  1694. return;
  1695. }
  1696. [[AppManager auth] signInAndRetrieveDataWithCredential:credential
  1697. completion:^(FIRAuthDataResult *_Nullable
  1698. authResult,
  1699. NSError *_Nullable error) {
  1700. [self hideSpinner:^{
  1701. if (error) {
  1702. [self logFailure:@"sign-in with Microsoft failed" error:error];
  1703. return;
  1704. } else {
  1705. [self logSuccess:@"sign-in with Microsoft (headful-lite) succeeded."];
  1706. }
  1707. [self showTypicalUIForUserUpdateResultsWithTitle:@"Sign-In Error" error:error];
  1708. }];
  1709. }];
  1710. }];
  1711. }];
  1712. }
  1713. /** @fn signInFacebook
  1714. @brief Invoked when "Sign in with Facebook" row is pressed.
  1715. */
  1716. - (void)signInFacebook {
  1717. [self signinWithProvider:[AuthProviders facebook] retrieveData:YES];
  1718. }
  1719. /** @fn signInFacebookAndRetrieveData
  1720. @brief Invoked when "Sign in with Facebook and retrieve data" row is pressed.
  1721. */
  1722. - (void)signInFacebookAndRetrieveData {
  1723. [self signinWithProvider:[AuthProviders facebook] retrieveData:YES];
  1724. }
  1725. - (void)logInWithSystemGameCenter {
  1726. GKLocalPlayer *localPlayer = [GKLocalPlayer localPlayer];
  1727. localPlayer.authenticateHandler = ^(UIViewController * _Nullable viewController,
  1728. NSError * _Nullable error) {
  1729. if (error) {
  1730. [self showTypicalUIForUserUpdateResultsWithTitle:@"Game Center Error" error:error];
  1731. } else if (viewController != nil) {
  1732. [self presentViewController:viewController animated:YES completion:nil];
  1733. }
  1734. };
  1735. }
  1736. - (void)signInWithGameCenter {
  1737. [FIRGameCenterAuthProvider getCredentialWithCompletion:
  1738. ^(FIRAuthCredential * _Nullable credential, NSError * _Nullable error) {
  1739. if (error) {
  1740. [self showTypicalUIForUserUpdateResultsWithTitle:@"Game Center Error" error:error];
  1741. } else {
  1742. [[AppManager auth] signInAndRetrieveDataWithCredential:credential
  1743. completion:^(FIRAuthDataResult * _Nullable result,
  1744. NSError * _Nullable error) {
  1745. [self hideSpinner:^{
  1746. if (error) {
  1747. [self logFailure:@"Sign in with Game Center failed" error:error];
  1748. } else {
  1749. [self logSuccess:@"Sign in with Game Center succeeded."];
  1750. }
  1751. [self showTypicalUIForUserUpdateResultsWithTitle:@"Sign In Error" error:error];
  1752. }];
  1753. }];
  1754. }
  1755. }];
  1756. }
  1757. - (void)linkWithGameCenter {
  1758. [FIRGameCenterAuthProvider getCredentialWithCompletion:
  1759. ^(FIRAuthCredential * _Nullable credential, NSError * _Nullable error) {
  1760. if (error) {
  1761. [self showTypicalUIForUserUpdateResultsWithTitle:@"Game Center Error" error:error];
  1762. } else {
  1763. [[self user] linkAndRetrieveDataWithCredential:credential
  1764. completion:^(FIRAuthDataResult * _Nullable result,
  1765. NSError * _Nullable error) {
  1766. [self hideSpinner:^{
  1767. if (error) {
  1768. [self logFailure:@"Link with Game Center failed" error:error];
  1769. } else {
  1770. [self logSuccess:@"Link with Game Center succeeded."];
  1771. }
  1772. [self showTypicalUIForUserUpdateResultsWithTitle:@"Link Error" error:error];
  1773. }];
  1774. }];
  1775. }
  1776. }];
  1777. }
  1778. - (void)reauthenticateWithGameCenter {
  1779. [FIRGameCenterAuthProvider getCredentialWithCompletion:
  1780. ^(FIRAuthCredential * _Nullable credential, NSError * _Nullable error) {
  1781. if (error) {
  1782. [self showTypicalUIForUserUpdateResultsWithTitle:@"Game Center Error" error:error];
  1783. } else {
  1784. [[self user] reauthenticateAndRetrieveDataWithCredential:credential
  1785. completion:^(FIRAuthDataResult * _Nullable result,
  1786. NSError * _Nullable error) {
  1787. [self hideSpinner:^{
  1788. if (error) {
  1789. [self logFailure:@"Reauthenticate with Game Center failed" error:error];
  1790. } else {
  1791. [self logSuccess:@"Reauthenticate with Game Center succeeded."];
  1792. }
  1793. [self showTypicalUIForUserUpdateResultsWithTitle:@"Reauthenticate Error" error:error];
  1794. }];
  1795. }];
  1796. }
  1797. }];
  1798. }
  1799. /** @fn signInEmailPassword
  1800. @brief Invoked when "Sign in with Email/Password" row is pressed.
  1801. */
  1802. - (void)signInEmailPassword {
  1803. [self showTextInputPromptWithMessage:@"Email Address:"
  1804. keyboardType:UIKeyboardTypeEmailAddress
  1805. completionBlock:^(BOOL userPressedOK, NSString *_Nullable email) {
  1806. if (!userPressedOK || !email.length) {
  1807. return;
  1808. }
  1809. [self showTextInputPromptWithMessage:@"Password:"
  1810. completionBlock:^(BOOL userPressedOK, NSString *_Nullable password) {
  1811. if (!userPressedOK) {
  1812. return;
  1813. }
  1814. FIRAuthCredential *credential =
  1815. [FIREmailAuthProvider credentialWithEmail:email
  1816. password:password];
  1817. [self showSpinner:^{
  1818. [[AppManager auth] signInAndRetrieveDataWithCredential:credential
  1819. completion:^(FIRAuthDataResult *_Nullable result,
  1820. NSError *_Nullable error) {
  1821. [self hideSpinner:^{
  1822. if (error) {
  1823. [self logFailure:@"sign-in with Email/Password failed" error:error];
  1824. } else {
  1825. [self logSuccess:@"sign-in with Email/Password succeeded."];
  1826. }
  1827. [self showTypicalUIForUserUpdateResultsWithTitle:@"Sign-In Error" error:error];
  1828. }];
  1829. }];
  1830. }];
  1831. }];
  1832. }];
  1833. }
  1834. - (void)signInEmailPasswordAuthDataResult {
  1835. [self showTextInputPromptWithMessage:@"Email Address:"
  1836. keyboardType:UIKeyboardTypeEmailAddress
  1837. completionBlock:^(BOOL userPressedOK, NSString *_Nullable email) {
  1838. if (!userPressedOK || !email.length) {
  1839. return;
  1840. }
  1841. [self showTextInputPromptWithMessage:@"Password:"
  1842. completionBlock:^(BOOL userPressedOK, NSString *_Nullable password) {
  1843. if (!userPressedOK) {
  1844. return;
  1845. }
  1846. [self showSpinner:^{
  1847. [[AppManager auth] signInWithEmail:email
  1848. password:password
  1849. completion:^(FIRAuthDataResult *_Nullable authResult,
  1850. NSError *_Nullable error) {
  1851. [self hideSpinner:^{
  1852. if (error) {
  1853. [self logFailure:@"sign-in with Email/Password failed" error:error];
  1854. } else {
  1855. [self logSuccess:@"sign-in with Email/Password succeeded."];
  1856. [self log:[NSString stringWithFormat:@"UID: %@",authResult.user.uid]];
  1857. }
  1858. [self showTypicalUIForUserUpdateResultsWithTitle:@"Sign-In Error" error:error];
  1859. }];
  1860. }];
  1861. }];
  1862. }];
  1863. }];
  1864. }
  1865. /** @fn signInWithEmailLink
  1866. @brief Invoked when "Sign in with email link" row is pressed.
  1867. */
  1868. - (void)signInWithEmailLink {
  1869. [self showTextInputPromptWithMessage:@"Email Address:"
  1870. keyboardType:UIKeyboardTypeEmailAddress
  1871. completionBlock:^(BOOL userPressedOK, NSString *_Nullable email) {
  1872. if (!userPressedOK || !email.length) {
  1873. return;
  1874. }
  1875. [self showTextInputPromptWithMessage:@"Email Sign-In Link:"
  1876. completionBlock:^(BOOL userPressedOK, NSString *_Nullable link) {
  1877. if (!userPressedOK) {
  1878. return;
  1879. }
  1880. if ([[FIRAuth auth] isSignInWithEmailLink:link]) {
  1881. [self showSpinner:^{
  1882. [[AppManager auth] signInWithEmail:email
  1883. link:link
  1884. completion:^(FIRAuthDataResult *_Nullable authResult,
  1885. NSError *_Nullable error) {
  1886. [self hideSpinner:^{
  1887. if (error) {
  1888. [self logFailure:@"sign-in with Email/Sign-In failed" error:error];
  1889. } else {
  1890. [self logSuccess:@"sign-in with Email/Sign-In link succeeded."];
  1891. [self log:[NSString stringWithFormat:@"UID: %@",authResult.user.uid]];
  1892. }
  1893. [self showTypicalUIForUserUpdateResultsWithTitle:@"Sign-In Error" error:error];
  1894. }];
  1895. }];
  1896. }];
  1897. } else {
  1898. [self log:@"The sign-in link is invalid"];
  1899. }
  1900. }];
  1901. }];
  1902. }
  1903. /** @fn verifyEmailLinkAccount
  1904. @brief Invoked to verify that the current user is an email-link user.
  1905. */
  1906. - (void)verifyEmailLinkAccount {
  1907. if (![FIRAuth auth].currentUser.email) {
  1908. [self showMessagePrompt:@"There is no signed-in user available."];
  1909. return;
  1910. }
  1911. [[FIRAuth auth] fetchSignInMethodsForEmail:[FIRAuth auth].currentUser.email
  1912. completion:^(NSArray<NSString *> *_Nullable signInMethods,
  1913. NSError *_Nullable error) {
  1914. if (error) {
  1915. [self showMessagePrompt:@"There was an error fetching sign-in methods."];
  1916. return;
  1917. }
  1918. if (![signInMethods containsObject:FIREmailLinkAuthSignInMethod]) {
  1919. [self showMessagePrompt:@"Error: The current user is NOT an email-link user."];
  1920. return;
  1921. }
  1922. [self showMessagePrompt:@"The current user is an email-link user."];
  1923. }];
  1924. }
  1925. /** @fn sendEmailSignInLink
  1926. @brief Invoked when "Send email sign-in link" row is pressed.
  1927. */
  1928. - (void)sendEmailSignInLink {
  1929. [self showTextInputPromptWithMessage:@"Email:"
  1930. completionBlock:^(BOOL userPressedOK, NSString *_Nullable userInput) {
  1931. if (!userPressedOK) {
  1932. return;
  1933. }
  1934. [self showSpinner:^{
  1935. void (^requestEmailSignInLink)(void (^)(NSError *)) = ^(void (^completion)(NSError *)) {
  1936. [[AppManager auth] sendSignInLinkToEmail:userInput
  1937. actionCodeSettings:[self actionCodeSettings]
  1938. completion:completion];
  1939. };
  1940. requestEmailSignInLink(^(NSError *_Nullable error) {
  1941. [self hideSpinner:^{
  1942. if (error) {
  1943. [self logFailure:@"Email Link request failed" error:error];
  1944. [self showMessagePrompt:error.localizedDescription];
  1945. return;
  1946. }
  1947. [self logSuccess:@"Email Link request succeeded."];
  1948. [self showMessagePrompt:@"Sent"];
  1949. }];
  1950. });
  1951. }];
  1952. }];
  1953. }
  1954. /** @fn signUpNewEmail
  1955. @brief Invoked if sign-in is attempted with new email/password.
  1956. @remarks Should only be called if @c FIRAuthErrorCodeInvalidEmail is encountered on attepmt to
  1957. sign in with email/password.
  1958. */
  1959. - (void)signUpNewEmail:(NSString *)email
  1960. password:(NSString *)password
  1961. callback:(nullable FIRAuthResultCallback)callback {
  1962. [[AppManager auth] createUserWithEmail:email
  1963. password:password
  1964. completion:^(FIRAuthDataResult *_Nullable result,
  1965. NSError *_Nullable error) {
  1966. if (error) {
  1967. [self logFailure:@"sign-up with Email/Password failed" error:error];
  1968. if (callback) {
  1969. callback(nil, error);
  1970. }
  1971. } else {
  1972. [self logSuccess:@"sign-up with Email/Password succeeded."];
  1973. if (callback) {
  1974. callback(result.user, nil);
  1975. }
  1976. }
  1977. [self showTypicalUIForUserUpdateResultsWithTitle:@"Sign-In" error:error];
  1978. }];
  1979. }
  1980. /** @fn signInWithCustomToken
  1981. @brief Signs the user in using a manually-entered custom token.
  1982. */
  1983. - (void)signInWithCustomToken {
  1984. CustomTokenDataEntryViewControllerCompletion action =
  1985. ^(BOOL cancelled, NSString *_Nullable userEnteredTokenText) {
  1986. if (cancelled) {
  1987. [self log:@"CANCELLED:sign-in with custom token cancelled."];
  1988. return;
  1989. }
  1990. [self doSignInWithCustomToken:userEnteredTokenText];
  1991. };
  1992. CustomTokenDataEntryViewController *dataEntryViewController =
  1993. [[CustomTokenDataEntryViewController alloc] initWithCompletion:action];
  1994. [self presentViewController:dataEntryViewController animated:YES completion:nil];
  1995. }
  1996. /** @fn signInWithCustomTokenAuthResult
  1997. @brief Signs the user in using a manually-entered custom token.
  1998. */
  1999. - (void)signInWithCustomTokenAuthResult {
  2000. CustomTokenDataEntryViewControllerCompletion action =
  2001. ^(BOOL cancelled, NSString *_Nullable userEnteredTokenText) {
  2002. if (cancelled) {
  2003. [self log:@"CANCELLED:sign-in with custom token cancelled."];
  2004. return;
  2005. }
  2006. [self doSignInAndRetrieveDataWithCustomToken:userEnteredTokenText];
  2007. };
  2008. CustomTokenDataEntryViewController *dataEntryViewController =
  2009. [[CustomTokenDataEntryViewController alloc] initWithCompletion:action];
  2010. [self presentViewController:dataEntryViewController animated:YES completion:nil];
  2011. }
  2012. /** @fn signOut
  2013. @brief Signs the user out.
  2014. */
  2015. - (void)signOut {
  2016. [[AuthProviders google] signOut];
  2017. [[AuthProviders facebook] signOut];
  2018. [[AppManager auth] signOut:NULL];
  2019. }
  2020. /** @fn deleteAccount
  2021. @brief Deletes the current user account and signs the user out.
  2022. */
  2023. - (void)deleteAccount {
  2024. FIRUser *user = [self user];
  2025. [user deleteWithCompletion:^(NSError *_Nullable error) {
  2026. if (error) {
  2027. [self logFailure:@"delete account failed" error:error];
  2028. }
  2029. [self showTypicalUIForUserUpdateResultsWithTitle:kDeleteUserText error:error];
  2030. }];
  2031. }
  2032. /** @fn reauthenticateGoogle
  2033. @brief Asks the user to reauthenticate with Google.
  2034. */
  2035. - (void)reauthenticateGoogle {
  2036. [self reauthenticate:[AuthProviders google] retrieveData:NO];
  2037. }
  2038. /** @fn reauthenticateGoogleAndRetrieveData
  2039. @brief Asks the user to reauthenticate with Google and retrieve additional data.
  2040. */
  2041. - (void)reauthenticateGoogleAndRetrieveData {
  2042. [self reauthenticate:[AuthProviders google] retrieveData:YES];
  2043. }
  2044. /** @fn reauthenticateFB
  2045. @brief Asks the user to reauthenticate with Facebook.
  2046. */
  2047. - (void)reauthenticateFB {
  2048. [self reauthenticate:[AuthProviders facebook] retrieveData:NO];
  2049. }
  2050. /** @fn reauthenticateFBAndRetrieveData
  2051. @brief Asks the user to reauthenticate with Facebook and retrieve additional data.
  2052. */
  2053. - (void)reauthenticateFBAndRetrieveData {
  2054. [self reauthenticate:[AuthProviders facebook] retrieveData:YES];
  2055. }
  2056. /** @fn reauthenticateEmailPassword
  2057. @brief Asks the user to reauthenticate with email/password.
  2058. */
  2059. - (void)reauthenticateEmailPassword {
  2060. FIRUser *user = [self user];
  2061. if (!user) {
  2062. NSString *title = @"Missing User";
  2063. NSString *message = @"There is no signed-in email/password user.";
  2064. [self showMessagePromptWithTitle:title message:message showCancelButton:NO completion:nil];
  2065. return;
  2066. }
  2067. [self showEmailPasswordDialogWithCompletion:^(FIRAuthCredential *credential) {
  2068. [self showSpinner:^{
  2069. [[self user] reauthenticateAndRetrieveDataWithCredential:credential
  2070. completion:^(FIRAuthDataResult *_Nullable result,
  2071. NSError *_Nullable error) {
  2072. if (error) {
  2073. [self logFailure:@"reauthicate with email/password failed" error:error];
  2074. } else {
  2075. [self logSuccess:@"reauthicate with email/password succeeded."];
  2076. }
  2077. [self hideSpinner:^{
  2078. [self showTypicalUIForUserUpdateResultsWithTitle:kReauthenticateEmailText error:error];
  2079. }];
  2080. }];
  2081. }];
  2082. }];
  2083. }
  2084. /** @fn reauthenticate:
  2085. @brief Reauthenticates user.
  2086. @param authProvider The auth provider to use for reauthentication.
  2087. @param retrieveData Defines if additional provider data should be read.
  2088. */
  2089. - (void)reauthenticate:(id<AuthProvider>)authProvider retrieveData:(BOOL)retrieveData {
  2090. FIRUser *user = [self user];
  2091. if (!user) {
  2092. NSString *provider = @"Firebase";
  2093. if ([authProvider isKindOfClass:[GoogleAuthProvider class]]) {
  2094. provider = @"Google";
  2095. } else if ([authProvider isKindOfClass:[FacebookAuthProvider class]]) {
  2096. provider = @"Facebook";
  2097. }
  2098. NSString *title = @"Missing User";
  2099. NSString *message =
  2100. [NSString stringWithFormat:@"There is no signed-in %@ user.", provider];
  2101. [self showMessagePromptWithTitle:title message:message showCancelButton:NO completion:nil];
  2102. return;
  2103. }
  2104. [authProvider getAuthCredentialWithPresentingViewController:self
  2105. callback:^(FIRAuthCredential *credential,
  2106. NSError *error) {
  2107. if (credential) {
  2108. FIRAuthDataResultCallback completion = ^(FIRAuthDataResult *_Nullable authResult,
  2109. NSError *_Nullable error) {
  2110. if (error) {
  2111. [self logFailure:@"reauthenticate operation failed" error:error];
  2112. } else {
  2113. [self logSuccess:@"reauthenticate operation succeeded."];
  2114. }
  2115. if (authResult.additionalUserInfo) {
  2116. [self logSuccess:[self stringWithAdditionalUserInfo:authResult.additionalUserInfo]];
  2117. }
  2118. [self showTypicalUIForUserUpdateResultsWithTitle:@"Reauthenticate" error:error];
  2119. };
  2120. FIRAuthDataResultCallback callback = ^(FIRAuthDataResult *_Nullable result,
  2121. NSError *_Nullable error) {
  2122. completion(nil, error);
  2123. };
  2124. if (retrieveData) {
  2125. [user reauthenticateAndRetrieveDataWithCredential:credential completion:completion];
  2126. } else {
  2127. [user reauthenticateAndRetrieveDataWithCredential:credential completion:callback];
  2128. }
  2129. }
  2130. }];
  2131. }
  2132. /** @fn signinWithProvider:
  2133. @brief Signs in the user with provided auth provider.
  2134. @param authProvider The auth provider to use for sign-in.
  2135. @param retrieveData Defines if additional provider data should be read.
  2136. */
  2137. - (void)signinWithProvider:(id<AuthProvider>)authProvider retrieveData:(BOOL)retrieveData {
  2138. FIRAuth *auth = [AppManager auth];
  2139. if (!auth) {
  2140. return;
  2141. }
  2142. [authProvider getAuthCredentialWithPresentingViewController:self
  2143. callback:^(FIRAuthCredential *credential,
  2144. NSError *error) {
  2145. if (credential) {
  2146. FIRAuthDataResultCallback completion = ^(FIRAuthDataResult *_Nullable authResult,
  2147. NSError *_Nullable error) {
  2148. if (error) {
  2149. [self logFailure:@"sign-in with provider failed" error:error];
  2150. } else {
  2151. [self logSuccess:@"sign-in with provider succeeded."];
  2152. }
  2153. if (authResult.additionalUserInfo) {
  2154. [self logSuccess:[self stringWithAdditionalUserInfo:authResult.additionalUserInfo]];
  2155. if (_isNewUserToggleOn) {
  2156. NSString *newUserString = authResult.additionalUserInfo.isNewUser ?
  2157. @"New user" : @"Existing user";
  2158. [self showMessagePromptWithTitle:@"New or Existing"
  2159. message:newUserString
  2160. showCancelButton:NO
  2161. completion:nil];
  2162. }
  2163. }
  2164. [self showTypicalUIForUserUpdateResultsWithTitle:@"Sign-In" error:error];
  2165. };
  2166. FIRAuthDataResultCallback callback = ^(FIRAuthDataResult *_Nullable result,
  2167. NSError *_Nullable error) {
  2168. completion(nil, error);
  2169. };
  2170. if (retrieveData) {
  2171. [auth signInAndRetrieveDataWithCredential:credential completion:completion];
  2172. } else {
  2173. [auth signInAndRetrieveDataWithCredential:credential completion:callback];
  2174. }
  2175. }
  2176. }];
  2177. }
  2178. /** @fn tokenCallback
  2179. @return A callback block to show the token.
  2180. */
  2181. - (FIRAuthTokenCallback)tokenCallback {
  2182. return ^(NSString *_Nullable token, NSError *_Nullable error) {
  2183. if (error) {
  2184. [self showMessagePromptWithTitle:kTokenRefreshErrorAlertTitle
  2185. message:error.localizedDescription
  2186. showCancelButton:NO
  2187. completion:nil];
  2188. [self logFailure:@"refresh token failed" error:error];
  2189. return;
  2190. }
  2191. [self logSuccess:@"refresh token succeeded."];
  2192. [self showMessagePromptWithTitle:kTokenRefreshedAlertTitle
  2193. message:token
  2194. showCancelButton:NO
  2195. completion:nil];
  2196. };
  2197. }
  2198. /** @fn getUserTokenWithForce:
  2199. @brief Gets the token from @c FIRUser, optionally a refreshed one.
  2200. @param force Whether the refresh is forced or not.
  2201. */
  2202. - (void)getUserTokenWithForce:(BOOL)force {
  2203. [[self user] getIDTokenForcingRefresh:force completion:[self tokenCallback]];
  2204. }
  2205. /** @fn getUserTokenResultWithForce:
  2206. @brief Gets the token result object from @c FIRUser, optionally a refreshed one.
  2207. @param force Whether the refresh is forced or not.
  2208. */
  2209. - (void)getUserTokenResultWithForce:(BOOL)force {
  2210. [[self user] getIDTokenResultForcingRefresh:force
  2211. completion:^(FIRAuthTokenResult *_Nullable tokenResult,
  2212. NSError *_Nullable error) {
  2213. if (error) {
  2214. [self showMessagePromptWithTitle:kTokenRefreshErrorAlertTitle
  2215. message:error.localizedDescription
  2216. showCancelButton:NO
  2217. completion:nil];
  2218. [self logFailure:@"refresh token failed" error:error];
  2219. return;
  2220. }
  2221. [self logSuccess:@"refresh token succeeded."];
  2222. NSMutableString *message =
  2223. [[NSMutableString alloc] initWithString:
  2224. [NSString stringWithFormat:@"Token : %@\n", tokenResult.token]];
  2225. [message appendString:[NSString stringWithFormat:@"Auth Date : %@\n", tokenResult.authDate]];
  2226. [message appendString:
  2227. [NSString stringWithFormat:@"EXP Date : %@\n", tokenResult.expirationDate]];
  2228. [message appendString:
  2229. [NSString stringWithFormat:@"Issued Date : %@\n", tokenResult.issuedAtDate]];
  2230. [self showMessagePromptWithTitle:kTokenRefreshedAlertTitle
  2231. message:message
  2232. showCancelButton:NO
  2233. completion:nil];
  2234. }];
  2235. }
  2236. /** @fn getAppTokenWithForce:
  2237. @brief Gets the token from @c FIRAuth , optionally a refreshed one.
  2238. @param force Whether the refresh is forced or not.
  2239. */
  2240. - (void)getAppTokenWithForce:(BOOL)force {
  2241. [[FIRAuth auth] getTokenForcingRefresh:force withCallback:[self tokenCallback]];
  2242. }
  2243. /** @fn setDisplayName
  2244. @brief Changes the display name of the current user.
  2245. */
  2246. - (void)setDisplayName {
  2247. [self showTextInputPromptWithMessage:@"Display Name:"
  2248. completionBlock:^(BOOL userPressedOK, NSString *_Nullable userInput) {
  2249. if (!userPressedOK || !userInput.length) {
  2250. return;
  2251. }
  2252. [self showSpinner:^{
  2253. FIRUserProfileChangeRequest *changeRequest = [[self user] profileChangeRequest];
  2254. changeRequest.displayName = userInput;
  2255. [changeRequest commitChangesWithCompletion:^(NSError *_Nullable error) {
  2256. [self hideSpinner:^{
  2257. if (error) {
  2258. [self logFailure:@"set display name failed" error:error];
  2259. } else {
  2260. [self logSuccess:@"set display name succeeded."];
  2261. }
  2262. [self showTypicalUIForUserUpdateResultsWithTitle:kSetDisplayNameTitle error:error];
  2263. }];
  2264. }];
  2265. }];
  2266. }];
  2267. }
  2268. /** @fn setPhotoURL
  2269. @brief Changes the photo url of the current user.
  2270. */
  2271. - (void)setPhotoURL {
  2272. [self showTextInputPromptWithMessage:@"Photo URL:"
  2273. completionBlock:^(BOOL userPressedOK, NSString *_Nullable userInput) {
  2274. if (!userPressedOK || !userInput.length) {
  2275. return;
  2276. }
  2277. [self showSpinner:^{
  2278. FIRUserProfileChangeRequest *changeRequest = [[self user] profileChangeRequest];
  2279. changeRequest.photoURL = [NSURL URLWithString:userInput];
  2280. [changeRequest commitChangesWithCompletion:^(NSError *_Nullable error) {
  2281. if (error) {
  2282. [self logFailure:@"set photo URL failed" error:error];
  2283. } else {
  2284. [self logSuccess:@"set Photo URL succeeded."];
  2285. }
  2286. [self hideSpinner:^{
  2287. [self showTypicalUIForUserUpdateResultsWithTitle:kSetPhotoURLText error:error];
  2288. }];
  2289. }];
  2290. }];
  2291. }];
  2292. }
  2293. /** @fn reloadUser
  2294. @brief Reloads the user from server.
  2295. */
  2296. - (void)reloadUser {
  2297. [self showSpinner:^() {
  2298. [[self user] reloadWithCompletion:^(NSError *_Nullable error) {
  2299. if (error) {
  2300. [self logFailure:@"reload user failed" error:error];
  2301. } else {
  2302. [self logSuccess:@"reload user succeeded."];
  2303. }
  2304. [self hideSpinner:^() {
  2305. [self showTypicalUIForUserUpdateResultsWithTitle:kReloadText error:error];
  2306. }];
  2307. }];
  2308. }];
  2309. }
  2310. /** @fn linkWithAuthProvider:retrieveData:
  2311. @brief Asks the user to sign in with an auth provider and link the current user with it.
  2312. @param authProvider The auth provider to sign in and link with.
  2313. @param retrieveData Defines if additional provider data should be read.
  2314. */
  2315. - (void)linkWithAuthProvider:(id<AuthProvider>)authProvider retrieveData:(BOOL)retrieveData {
  2316. FIRUser *user = [self user];
  2317. if (!user) {
  2318. return;
  2319. }
  2320. [authProvider getAuthCredentialWithPresentingViewController:self
  2321. callback:^(FIRAuthCredential *credential,
  2322. NSError *error) {
  2323. if (credential) {
  2324. FIRAuthDataResultCallback completion = ^(FIRAuthDataResult *_Nullable authResult,
  2325. NSError *_Nullable error) {
  2326. if (error) {
  2327. [self logFailure:@"link auth provider failed" error:error];
  2328. } else {
  2329. [self logSuccess:@"link auth provider succeeded."];
  2330. }
  2331. if (authResult.additionalUserInfo) {
  2332. [self logSuccess:[self stringWithAdditionalUserInfo:authResult.additionalUserInfo]];
  2333. }
  2334. if (retrieveData) {
  2335. [self showUIForAuthDataResultWithResult:authResult error:error];
  2336. } else {
  2337. [self showTypicalUIForUserUpdateResultsWithTitle:@"Link Account" error:error];
  2338. }
  2339. };
  2340. FIRAuthDataResultCallback callback = ^(FIRAuthDataResult *_Nullable result,
  2341. NSError *_Nullable error) {
  2342. completion(nil, error);
  2343. };
  2344. if (retrieveData) {
  2345. [user linkAndRetrieveDataWithCredential:credential completion:completion];
  2346. } else {
  2347. [user linkAndRetrieveDataWithCredential:credential completion:callback];
  2348. }
  2349. }
  2350. }];
  2351. }
  2352. /** @fn linkWithGoogle
  2353. @brief Asks the user to sign in with Google and link the current user with this Google account.
  2354. */
  2355. - (void)linkWithGoogle {
  2356. [self linkWithAuthProvider:[AuthProviders google] retrieveData:NO];
  2357. }
  2358. /** @fn linkWithGoogleAndRetrieveData
  2359. @brief Asks the user to sign in with Google and link the current user with this Google account
  2360. and retrieve additional data.
  2361. */
  2362. - (void)linkWithGoogleAndRetrieveData {
  2363. [self linkWithAuthProvider:[AuthProviders google] retrieveData:YES];
  2364. }
  2365. /** @fn linkWithFacebook
  2366. @brief Asks the user to sign in with Facebook and link the current user with this Facebook
  2367. account.
  2368. */
  2369. - (void)linkWithFacebook {
  2370. [self linkWithAuthProvider:[AuthProviders facebook] retrieveData:NO];
  2371. }
  2372. /** @fn linkWithFacebookAndRetrieveData
  2373. @brief Asks the user to sign in with Facebook and link the current user with this Facebook
  2374. account and retrieve additional data.
  2375. */
  2376. - (void)linkWithFacebookAndRetrieveData {
  2377. [self linkWithAuthProvider:[AuthProviders facebook] retrieveData:YES];
  2378. }
  2379. /** @fn linkWithEmailPassword
  2380. @brief Asks the user to sign in with Facebook and link the current user with this Facebook
  2381. account.
  2382. */
  2383. - (void)linkWithEmailPassword {
  2384. [self showEmailPasswordDialogWithCompletion:^(FIRAuthCredential *credential) {
  2385. [self showSpinner:^{
  2386. [[self user] linkAndRetrieveDataWithCredential:credential
  2387. completion:^(FIRAuthDataResult *result, NSError *error) {
  2388. if (error) {
  2389. [self logFailure:@"link Email/Password failed" error:error];
  2390. } else {
  2391. [self logSuccess:@"link Email/Password succeeded."];
  2392. }
  2393. [self hideSpinner:^{
  2394. [self showTypicalUIForUserUpdateResultsWithTitle:kLinkWithEmailPasswordText error:error];
  2395. }];
  2396. }];
  2397. }];
  2398. }];
  2399. }
  2400. /** @fn showEmailPasswordDialogWithCompletion:
  2401. @brief shows email/password input dialog.
  2402. @param completion The completion block that will do some operation on the credential email
  2403. /passwowrd credential obtained.
  2404. */
  2405. - (void)showEmailPasswordDialogWithCompletion:(ShowEmailPasswordDialogCompletion)completion {
  2406. [self showTextInputPromptWithMessage:@"Email Address:"
  2407. completionBlock:^(BOOL userPressedOK, NSString *_Nullable email) {
  2408. if (!userPressedOK || !email.length) {
  2409. return;
  2410. }
  2411. [self showTextInputPromptWithMessage:@"Password:"
  2412. completionBlock:^(BOOL userPressedOK, NSString *_Nullable password) {
  2413. if (!userPressedOK || !password.length) {
  2414. return;
  2415. }
  2416. FIRAuthCredential *credential = [FIREmailAuthProvider credentialWithEmail:email
  2417. password:password];
  2418. completion(credential);
  2419. }];
  2420. }];
  2421. }
  2422. /** @fn unlinkFromProvider:
  2423. @brief Unlinks the current user from the provider with the specified provider ID.
  2424. @param provider The provider ID of the provider to unlink the current user's account from.
  2425. @completion A completion block to be executed after the provider is unlinked.
  2426. */
  2427. - (void)unlinkFromProvider:(NSString *)provider
  2428. completion:(nullable testAutomationCallback)completion {
  2429. [[self user] unlinkFromProvider:provider
  2430. completion:^(FIRUser *_Nullable user,
  2431. NSError *_Nullable error) {
  2432. if (error) {
  2433. [self logFailure:@"unlink auth provider failed" error:error];
  2434. if (completion) {
  2435. completion(error);
  2436. }
  2437. return;
  2438. }
  2439. [self logSuccess:@"unlink auth provider succeeded."];
  2440. if (completion) {
  2441. completion(nil);
  2442. }
  2443. [self showTypicalUIForUserUpdateResultsWithTitle:kUnlinkTitle error:error];
  2444. }];
  2445. }
  2446. /** @fn getProvidersForEmail
  2447. @brief Prompts the user for an email address, calls @c FIRAuth.getProvidersForEmail:callback:
  2448. and displays the result.
  2449. */
  2450. - (void)getProvidersForEmail {
  2451. [self showTextInputPromptWithMessage:@"Email:"
  2452. completionBlock:^(BOOL userPressedOK, NSString *_Nullable userInput) {
  2453. if (!userPressedOK || !userInput.length) {
  2454. return;
  2455. }
  2456. [self showSpinner:^{
  2457. [[AppManager auth] fetchProvidersForEmail:userInput
  2458. completion:^(NSArray<NSString *> *_Nullable providers,
  2459. NSError *_Nullable error) {
  2460. if (error) {
  2461. [self logFailure:@"get providers for email failed" error:error];
  2462. } else {
  2463. [self logSuccess:@"get providers for email succeeded."];
  2464. }
  2465. [self hideSpinner:^{
  2466. if (error) {
  2467. [self showMessagePrompt:error.localizedDescription];
  2468. return;
  2469. }
  2470. [self showMessagePrompt:[providers componentsJoinedByString:@", "]];
  2471. }];
  2472. }];
  2473. }];
  2474. }];
  2475. }
  2476. /** @fn getAllSignInMethodsForEmail
  2477. @brief Prompts user for an email address, calls @c FIRAuth.getAllSignInMethodsForEmail:callback:
  2478. and displays the result.
  2479. */
  2480. - (void)getAllSignInMethodsForEmail {
  2481. [self showTextInputPromptWithMessage:@"Email:"
  2482. completionBlock:^(BOOL userPressedOK, NSString *_Nullable userInput) {
  2483. if (!userPressedOK || !userInput.length) {
  2484. return;
  2485. }
  2486. [self showSpinner:^{
  2487. [[AppManager auth] fetchSignInMethodsForEmail:userInput
  2488. completion:^(NSArray<NSString *> *_Nullable signInMethods,
  2489. NSError *_Nullable error) {
  2490. if (error) {
  2491. [self logFailure:@"get sign-in methods for email failed" error:error];
  2492. } else {
  2493. [self logSuccess:@"get sign-in methods for email succeeded."];
  2494. }
  2495. [self hideSpinner:^{
  2496. if (error) {
  2497. [self showMessagePrompt:error.localizedDescription];
  2498. return;
  2499. }
  2500. [self showMessagePrompt:[signInMethods componentsJoinedByString:@", "]];
  2501. }];
  2502. }];
  2503. }];
  2504. }];
  2505. }
  2506. /** @fn actionCodeRequestTypeString
  2507. @brief Returns a string description for the type of the next action code request.
  2508. */
  2509. - (NSString *)actionCodeRequestTypeString {
  2510. switch (_actionCodeRequestType) {
  2511. case ActionCodeRequestTypeInApp:
  2512. return @"In-App + Continue URL";
  2513. case ActionCodeRequestTypeContinue:
  2514. return @"Continue URL";
  2515. case ActionCodeRequestTypeEmail:
  2516. return @"Email Only";
  2517. }
  2518. }
  2519. /** @fn toggleActionCodeRequestType
  2520. @brief Toggle the next action code request type.
  2521. */
  2522. - (void)toggleActionCodeRequestType {
  2523. switch (_actionCodeRequestType) {
  2524. case ActionCodeRequestTypeInApp:
  2525. _actionCodeRequestType = ActionCodeRequestTypeContinue;
  2526. break;
  2527. case ActionCodeRequestTypeContinue:
  2528. _actionCodeRequestType = ActionCodeRequestTypeEmail;
  2529. break;
  2530. case ActionCodeRequestTypeEmail:
  2531. _actionCodeRequestType = ActionCodeRequestTypeInApp;
  2532. break;
  2533. }
  2534. [self updateTable];
  2535. }
  2536. - (void)changeActionCodeContinueURL {
  2537. [self showTextInputPromptWithMessage:kContinueURLDescription
  2538. completionBlock:^(BOOL userPressedOK, NSString *_Nullable userInput) {
  2539. if (userPressedOK) {
  2540. _actionCodeContinueURL = userInput.length ? [NSURL URLWithString:userInput] : nil;
  2541. [self updateTable];
  2542. }
  2543. }];
  2544. }
  2545. /** @fn requestVerifyEmail
  2546. @brief Requests a "verify email" email be sent.
  2547. */
  2548. - (void)requestVerifyEmail {
  2549. [self showSpinner:^{
  2550. void (^sendEmailVerification)(void (^)(NSError *)) = ^(void (^completion)(NSError *)) {
  2551. if (_actionCodeRequestType == ActionCodeRequestTypeEmail) {
  2552. [[self user] sendEmailVerificationWithCompletion:completion];
  2553. } else {
  2554. [[self user] sendEmailVerificationWithActionCodeSettings:[self actionCodeSettings]
  2555. completion:completion];
  2556. }
  2557. };
  2558. sendEmailVerification(^(NSError *_Nullable error) {
  2559. [self hideSpinner:^{
  2560. if (error) {
  2561. [self logFailure:@"request verify email failed" error:error];
  2562. [self showMessagePrompt:error.localizedDescription];
  2563. return;
  2564. }
  2565. [self logSuccess:@"request verify email succeeded."];
  2566. [self showMessagePrompt:@"Sent"];
  2567. }];
  2568. });
  2569. }];
  2570. }
  2571. /** @fn requestPasswordReset
  2572. @brief Requests a "password reset" email be sent.
  2573. */
  2574. - (void)requestPasswordReset {
  2575. [self showTextInputPromptWithMessage:@"Email:"
  2576. completionBlock:^(BOOL userPressedOK, NSString *_Nullable userInput) {
  2577. if (!userPressedOK || !userInput.length) {
  2578. return;
  2579. }
  2580. [self showSpinner:^{
  2581. void (^requestPasswordReset)(void (^)(NSError *)) = ^(void (^completion)(NSError *)) {
  2582. if (_actionCodeRequestType == ActionCodeRequestTypeEmail) {
  2583. [[AppManager auth] sendPasswordResetWithEmail:userInput completion:completion];
  2584. } else {
  2585. [[AppManager auth] sendPasswordResetWithEmail:userInput
  2586. actionCodeSettings:[self actionCodeSettings]
  2587. completion:completion];
  2588. }
  2589. };
  2590. requestPasswordReset(^(NSError *_Nullable error) {
  2591. [self hideSpinner:^{
  2592. if (error) {
  2593. [self logFailure:@"request password reset failed" error:error];
  2594. [self showMessagePrompt:error.localizedDescription];
  2595. return;
  2596. }
  2597. [self logSuccess:@"request password reset succeeded."];
  2598. [self showMessagePrompt:@"Sent"];
  2599. }];
  2600. });
  2601. }];
  2602. }];
  2603. }
  2604. /** @fn resetPassword
  2605. @brief Performs a password reset operation.
  2606. */
  2607. - (void)resetPassword {
  2608. [self showTextInputPromptWithMessage:@"OOB Code:"
  2609. completionBlock:^(BOOL userPressedOK, NSString *_Nullable userInput) {
  2610. if (!userPressedOK || !userInput.length) {
  2611. return;
  2612. }
  2613. NSString *code = userInput;
  2614. [self showTextInputPromptWithMessage:@"New Password:"
  2615. completionBlock:^(BOOL userPressedOK, NSString *_Nullable userInput) {
  2616. if (!userPressedOK || !userInput.length) {
  2617. return;
  2618. }
  2619. [self showSpinner:^{
  2620. [[AppManager auth] confirmPasswordResetWithCode:code
  2621. newPassword:userInput
  2622. completion:^(NSError *_Nullable error) {
  2623. [self hideSpinner:^{
  2624. if (error) {
  2625. [self logFailure:@"Password reset failed" error:error];
  2626. [self showMessagePrompt:error.localizedDescription];
  2627. return;
  2628. }
  2629. [self logSuccess:@"Password reset succeeded."];
  2630. [self showMessagePrompt:@"Password reset succeeded."];
  2631. }];
  2632. }];
  2633. }];
  2634. }];
  2635. }];
  2636. }
  2637. /** @fn checkActionCode
  2638. @brief Performs a "check action code" operation.
  2639. */
  2640. - (void)checkActionCode {
  2641. [self showTextInputPromptWithMessage:@"OOB Code:"
  2642. completionBlock:^(BOOL userPressedOK, NSString *_Nullable userInput) {
  2643. if (!userPressedOK || !userInput.length) {
  2644. return;
  2645. }
  2646. [self showSpinner:^{
  2647. [[AppManager auth] checkActionCode:userInput completion:^(FIRActionCodeInfo *_Nullable info,
  2648. NSError *_Nullable error) {
  2649. [self hideSpinner:^{
  2650. if (error) {
  2651. [self logFailure:@"Check action code failed" error:error];
  2652. [self showMessagePrompt:error.localizedDescription];
  2653. return;
  2654. }
  2655. [self logSuccess:@"Check action code succeeded."];
  2656. NSString *email = [info dataForKey:FIRActionCodeEmailKey];
  2657. NSString *fromEmail = [info dataForKey:FIRActionCodeFromEmailKey];
  2658. NSString *message =
  2659. fromEmail ? [NSString stringWithFormat:@"%@ -> %@", fromEmail, email] : email;
  2660. NSString *operation = [self nameForActionCodeOperation:info.operation];
  2661. [self showMessagePromptWithTitle:operation
  2662. message:message
  2663. showCancelButton:NO
  2664. completion:nil];
  2665. }];
  2666. }];
  2667. }];
  2668. }];
  2669. }
  2670. /** @fn applyActionCode
  2671. @brief Performs a "apply action code" operation.
  2672. */
  2673. - (void)applyActionCode {
  2674. [self showTextInputPromptWithMessage:@"OOB Code:"
  2675. completionBlock:^(BOOL userPressedOK, NSString *_Nullable userInput) {
  2676. if (!userPressedOK || !userInput.length) {
  2677. return;
  2678. }
  2679. [self showSpinner:^{
  2680. [[AppManager auth] applyActionCode:userInput completion:^(NSError *_Nullable error) {
  2681. [self hideSpinner:^{
  2682. if (error) {
  2683. [self logFailure:@"Apply action code failed" error:error];
  2684. [self showMessagePrompt:error.localizedDescription];
  2685. return;
  2686. }
  2687. [self logSuccess:@"Apply action code succeeded."];
  2688. [self showMessagePrompt:@"Action code was properly applied."];
  2689. }];
  2690. }];
  2691. }];
  2692. }];
  2693. }
  2694. /** @fn verifyPasswordResetCode
  2695. @brief Performs a "verify password reset code" operation.
  2696. */
  2697. - (void)verifyPasswordResetCode {
  2698. [self showTextInputPromptWithMessage:@"OOB Code:"
  2699. completionBlock:^(BOOL userPressedOK, NSString *_Nullable userInput) {
  2700. if (!userPressedOK || !userInput.length) {
  2701. return;
  2702. }
  2703. [self showSpinner:^{
  2704. [[AppManager auth] verifyPasswordResetCode:userInput completion:^(NSString *_Nullable email,
  2705. NSError *_Nullable error) {
  2706. [self hideSpinner:^{
  2707. if (error) {
  2708. [self logFailure:@"Verify password reset code failed" error:error];
  2709. [self showMessagePrompt:error.localizedDescription];
  2710. return;
  2711. }
  2712. [self logSuccess:@"Verify password resest code succeeded."];
  2713. NSString *alertMessage =
  2714. [[NSString alloc] initWithFormat:@"Code verified for email: %@", email];
  2715. [self showMessagePrompt:alertMessage];
  2716. }];
  2717. }];
  2718. }];
  2719. }];
  2720. }
  2721. /** @fn nameForActionCodeOperation
  2722. @brief Returns the string value of the provided FIRActionCodeOperation value.
  2723. @param operation the FIRActionCodeOperation value to convert to string.
  2724. @return String conversion of FIRActionCodeOperation value.
  2725. */
  2726. - (NSString *)nameForActionCodeOperation:(FIRActionCodeOperation)operation {
  2727. switch (operation) {
  2728. case FIRActionCodeOperationVerifyEmail:
  2729. return @"Verify Email";
  2730. case FIRActionCodeOperationRecoverEmail:
  2731. return @"Recover Email";
  2732. case FIRActionCodeOperationPasswordReset:
  2733. return @"Password Reset";
  2734. case FIRActionCodeOperationEmailLink:
  2735. return @"Email Sign-In Link";
  2736. case FIRActionCodeOperationUnknown:
  2737. return @"Unknown action";
  2738. }
  2739. }
  2740. /** @fn updateEmail
  2741. @brief Changes the email address of the current user.
  2742. */
  2743. - (void)updateEmail {
  2744. [self showTextInputPromptWithMessage:@"Email Address:"
  2745. completionBlock:^(BOOL userPressedOK, NSString *_Nullable userInput) {
  2746. if (!userPressedOK || !userInput.length) {
  2747. return;
  2748. }
  2749. [self showSpinner:^{
  2750. [[self user] updateEmail:userInput completion:^(NSError *_Nullable error) {
  2751. if (error) {
  2752. [self logFailure:@"update email failed" error:error];
  2753. } else {
  2754. [self logSuccess:@"update email succeeded."];
  2755. }
  2756. [self hideSpinner:^{
  2757. [self showTypicalUIForUserUpdateResultsWithTitle:kUpdateEmailText error:error];
  2758. }];
  2759. }];
  2760. }];
  2761. }];
  2762. }
  2763. /** @fn updatePassword
  2764. @brief Updates the password of the current user.
  2765. */
  2766. - (void)updatePassword {
  2767. [self showTextInputPromptWithMessage:@"New Password:"
  2768. completionBlock:^(BOOL userPressedOK, NSString *_Nullable userInput) {
  2769. if (!userPressedOK) {
  2770. return;
  2771. }
  2772. [self showSpinner:^{
  2773. [[self user] updatePassword:userInput completion:^(NSError *_Nullable error) {
  2774. if (error) {
  2775. [self logFailure:@"update password failed" error:error];
  2776. } else {
  2777. [self logSuccess:@"update password succeeded."];
  2778. }
  2779. [self hideSpinner:^{
  2780. [self showTypicalUIForUserUpdateResultsWithTitle:kUpdatePasswordText error:error];
  2781. }];
  2782. }];
  2783. }];
  2784. }];
  2785. }
  2786. /** @fn updateToSavedUser
  2787. @brief updates the current user to the saved user.
  2788. */
  2789. - (void)updateToSavedUser {
  2790. if(![AppManager auth].currentUser) {
  2791. NSLog(@"You must be signed in to perform this action");
  2792. return;
  2793. }
  2794. if (!_userInMemory) {
  2795. [self showMessagePrompt:[NSString stringWithFormat:@"You need an in-memory user to perform this"
  2796. "action, use the M+ button to save a user to memory.", nil]];
  2797. return;
  2798. }
  2799. [[AppManager auth] updateCurrentUser:_userInMemory completion:^(NSError *_Nullable error) {
  2800. if (error) {
  2801. [self showMessagePrompt:
  2802. [NSString stringWithFormat:@"An error Occurred: %@", error.localizedDescription]];
  2803. return;
  2804. }
  2805. }];
  2806. }
  2807. /** @fn createUser
  2808. @brief Creates a new user.
  2809. */
  2810. - (void)createUser {
  2811. [self showTextInputPromptWithMessage:@"Email:"
  2812. keyboardType:UIKeyboardTypeEmailAddress
  2813. completionBlock:^(BOOL userPressedOK, NSString *_Nullable email) {
  2814. if (!userPressedOK || !email.length) {
  2815. return;
  2816. }
  2817. [self showTextInputPromptWithMessage:@"Password:"
  2818. completionBlock:^(BOOL userPressedOK, NSString *_Nullable password) {
  2819. if (!userPressedOK) {
  2820. return;
  2821. }
  2822. [self showSpinner:^{
  2823. [[AppManager auth] createUserWithEmail:email
  2824. password:password
  2825. completion:^(FIRAuthDataResult *_Nullable result,
  2826. NSError *_Nullable error) {
  2827. if (error) {
  2828. [self logFailure:@"create user failed" error:error];
  2829. } else {
  2830. [self logSuccess:@"create user succeeded."];
  2831. }
  2832. [self hideSpinner:^{
  2833. [self showTypicalUIForUserUpdateResultsWithTitle:kCreateUserTitle error:error];
  2834. }];
  2835. }];
  2836. }];
  2837. }];
  2838. }];
  2839. }
  2840. /** @fn createUserAuthDataResult
  2841. @brief Creates a new user.
  2842. */
  2843. - (void)createUserAuthDataResult {
  2844. [self showTextInputPromptWithMessage:@"Email:"
  2845. keyboardType:UIKeyboardTypeEmailAddress
  2846. completionBlock:^(BOOL userPressedOK, NSString *_Nullable email) {
  2847. if (!userPressedOK || !email.length) {
  2848. return;
  2849. }
  2850. [self showTextInputPromptWithMessage:@"Password:"
  2851. completionBlock:^(BOOL userPressedOK, NSString *_Nullable password) {
  2852. if (!userPressedOK) {
  2853. return;
  2854. }
  2855. [self showSpinner:^{
  2856. [[AppManager auth] createUserWithEmail:email
  2857. password:password
  2858. completion:^(FIRAuthDataResult *_Nullable result,
  2859. NSError *_Nullable error) {
  2860. if (error) {
  2861. [self logFailure:@"create user failed" error:error];
  2862. } else {
  2863. [self logSuccess:@"create user succeeded."];
  2864. [self log:result.user.uid];
  2865. }
  2866. [self hideSpinner:^{
  2867. [self showTypicalUIForUserUpdateResultsWithTitle:kCreateUserTitle error:error];
  2868. }];
  2869. }];
  2870. }];
  2871. }];
  2872. }];
  2873. }
  2874. /** @fn signInWithPhoneNumber
  2875. @brief Allows sign in with phone number using reCAPTCHA.
  2876. @param phoneNumber Number pass in string.
  2877. @completion A completion block to be executed after successful phone number sign in.
  2878. */
  2879. - (void)signInWithPhoneNumber:(NSString *_Nullable)phoneNumber
  2880. completion:(nullable testAutomationCallback)completion {
  2881. [self showSpinner:^{
  2882. [[AppManager phoneAuthProvider] verifyPhoneNumber:phoneNumber
  2883. UIDelegate:nil
  2884. completion:^(NSString *_Nullable verificationID,
  2885. NSError *_Nullable error) {
  2886. [self hideSpinner:^{
  2887. if (error) {
  2888. [self logFailure:@"failed to send verification code" error:error];
  2889. [self showMessagePrompt:error.localizedDescription];
  2890. if (completion) {
  2891. completion(error);
  2892. }
  2893. return;
  2894. }
  2895. [self logSuccess:@"Code sent"];
  2896. [self commonPhoneNumberInputWithTitle:@"Code"
  2897. Completion:^(NSString *_Nullable verificationCode) {
  2898. [self commontPhoneVerificationWithVerificationID:verificationID
  2899. verificationCode:verificationCode];
  2900. if (completion) {
  2901. completion(nil);
  2902. }
  2903. }];
  2904. }];
  2905. }];
  2906. }];
  2907. }
  2908. /** @fn verifyIOSClient
  2909. @brief Trigger verify iOS client by sending a verification code to the test number.
  2910. */
  2911. - (void)verifyIOSClient {
  2912. [[AppManager auth].tokenManager getTokenWithCallback:^(FIRAuthAPNSToken *_Nullable token,
  2913. NSError *_Nullable error) {
  2914. if (!token) {
  2915. [self logFailure:@"Verify iOS client failed." error:error];
  2916. return;
  2917. }
  2918. FIRVerifyClientRequest *request =
  2919. [[FIRVerifyClientRequest alloc] initWithAppToken:token.string
  2920. isSandbox:token.type == FIRAuthAPNSTokenTypeSandbox
  2921. requestConfiguration:[AppManager auth].requestConfiguration];
  2922. [FIRAuthBackend verifyClient:request callback:^(FIRVerifyClientResponse *_Nullable response,
  2923. NSError *_Nullable error) {
  2924. if (error) {
  2925. [self logFailure:@"Verify iOS client failed." error:error];
  2926. return;
  2927. }
  2928. NSTimeInterval timeout = [response.suggestedTimeOutDate timeIntervalSinceNow];
  2929. [[AppManager auth].appCredentialManager
  2930. didStartVerificationWithReceipt:response.receipt
  2931. timeout:timeout
  2932. callback:^(FIRAuthAppCredential *credential) {
  2933. if (!credential.secret) {
  2934. [self logFailure:@"Failed to receive remote notification to verify app identity."
  2935. error:error];
  2936. return;
  2937. }
  2938. NSString *testPhoneNumber = @"+16509964692";
  2939. FIRSendVerificationCodeRequest *request =
  2940. [[FIRSendVerificationCodeRequest alloc] initWithPhoneNumber:testPhoneNumber
  2941. appCredential:credential
  2942. reCAPTCHAToken:nil
  2943. requestConfiguration:
  2944. [AppManager auth].requestConfiguration];
  2945. [FIRAuthBackend sendVerificationCode:request
  2946. callback:^(FIRSendVerificationCodeResponse *_Nullable response,
  2947. NSError *_Nullable error) {
  2948. if (error) {
  2949. [self logFailure:@"Verify iOS client failed." error:error];
  2950. return;
  2951. } else {
  2952. [self logSuccess:@"Verify iOS client succeeded."];
  2953. [self showMessagePrompt:@"Verify iOS client succeed."];
  2954. }
  2955. }];
  2956. }];
  2957. }];
  2958. }];
  2959. }
  2960. /** @fn signInWithPhoneNumberWithPrompt
  2961. @brief Allows sign in with phone number via popup prompt.
  2962. */
  2963. - (void)signInWithPhoneNumberWithPrompt {
  2964. [self commonPhoneNumberInputWithTitle:@"Phone #"
  2965. Completion:^(NSString *_Nullable phone) {
  2966. [self signInWithPhoneNumber:phone completion:nil];
  2967. }];
  2968. }
  2969. /** @fn commonPhoneNumberInputWithLabel:Completion
  2970. @brief Allows user input into a text field.
  2971. @param title of the promt.
  2972. */
  2973. - (void)commonPhoneNumberInputWithTitle:(NSString *)title
  2974. Completion:(textInputCompletionBlock)completion {
  2975. [self showTextInputPromptWithMessage:title
  2976. keyboardType:UIKeyboardTypePhonePad
  2977. completionBlock:^(BOOL userPressedOK, NSString *_Nullable phoneNumber) {
  2978. if (!userPressedOK) {
  2979. return;
  2980. }
  2981. completion(phoneNumber);
  2982. }];
  2983. }
  2984. /** @fn commonPhoneNumberInputWithLabel:Completion
  2985. @brief Finishes the phone number verification flow.
  2986. @param verificationID The verificationID from the backend.
  2987. @param verificationCode The verificationCode from the SMS message.
  2988. */
  2989. - (void)commontPhoneVerificationWithVerificationID:(NSString *)verificationID
  2990. verificationCode:(NSString *)verificationCode {
  2991. [self showSpinner:^{
  2992. FIRAuthCredential *credential =
  2993. [[AppManager phoneAuthProvider] credentialWithVerificationID:verificationID
  2994. verificationCode:verificationCode];
  2995. [[AppManager auth] signInAndRetrieveDataWithCredential:credential
  2996. completion:^(FIRAuthDataResult *_Nullable result,
  2997. NSError *_Nullable error) {
  2998. [self hideSpinner:^{
  2999. if (error) {
  3000. [self logFailure:@"failed to verify phone number" error:error];
  3001. [self showMessagePrompt:error.localizedDescription];
  3002. return;
  3003. }
  3004. if (_isNewUserToggleOn) {
  3005. NSString *newUserString = result.additionalUserInfo.isNewUser ?
  3006. @"New user" : @"Existing user";
  3007. [self showMessagePromptWithTitle:@"New or Existing"
  3008. message:newUserString
  3009. showCancelButton:NO
  3010. completion:nil];
  3011. }
  3012. }];
  3013. }];
  3014. }];
  3015. }
  3016. /** @fn updatePhoneNumber
  3017. @brief Allows adding a verified phone number to the currently signed user.
  3018. @param phoneNumber Number pass in string.
  3019. @completion A completion block to be executed after phone number is updated.
  3020. */
  3021. - (void)updatePhoneNumber:(NSString *_Nullable)phoneNumber
  3022. completion:(nullable testAutomationCallback)completion {
  3023. [self showSpinner:^{
  3024. [[AppManager phoneAuthProvider] verifyPhoneNumber:phoneNumber
  3025. UIDelegate:nil
  3026. completion:^(NSString *_Nullable verificationID,
  3027. NSError *_Nullable error) {
  3028. if (error) {
  3029. [self logFailure:@"failed to send verification code" error:error];
  3030. [self showMessagePrompt:error.localizedDescription];
  3031. if (completion) {
  3032. completion(error);
  3033. }
  3034. return;
  3035. }
  3036. [self logSuccess:@"Code sent"];
  3037. [self showTextInputPromptWithMessage:@"Verification code:"
  3038. keyboardType:UIKeyboardTypeNumberPad
  3039. completionBlock:^(BOOL userPressedOK,
  3040. NSString *_Nullable verificationCode) {
  3041. if (!userPressedOK || !verificationCode.length) {
  3042. return;
  3043. }
  3044. [self showSpinner:^{
  3045. FIRPhoneAuthCredential *credential =
  3046. [[AppManager phoneAuthProvider] credentialWithVerificationID:verificationID
  3047. verificationCode:verificationCode];
  3048. [[self user] updatePhoneNumberCredential:credential
  3049. completion:^(NSError *_Nullable error) {
  3050. if (error) {
  3051. [self logFailure:@"update phone number failed" error:error];
  3052. [self showMessagePrompt:error.localizedDescription];
  3053. if (completion) {
  3054. completion(error);
  3055. }
  3056. } else {
  3057. [self logSuccess:@"update phone number succeeded."];
  3058. if (completion) {
  3059. completion(nil);
  3060. }
  3061. }
  3062. }];
  3063. }];
  3064. }];
  3065. [self hideSpinner:^{
  3066. [self showTypicalUIForUserUpdateResultsWithTitle:kCreateUserTitle error:error];
  3067. }];
  3068. }];
  3069. }];
  3070. }
  3071. /** @fn updatePhoneNumberWithPrompt
  3072. @brief Allows adding a verified phone number to the currently signed user via popup prompt.
  3073. */
  3074. - (void)updatePhoneNumberWithPrompt {
  3075. [self showTextInputPromptWithMessage:@"Update Phone #:"
  3076. keyboardType:UIKeyboardTypePhonePad
  3077. completionBlock:^(BOOL userPressedOK, NSString *_Nullable phoneNumber) {
  3078. if (!userPressedOK || !phoneNumber.length) {
  3079. return;
  3080. }
  3081. [self updatePhoneNumber:phoneNumber completion:nil];
  3082. }];
  3083. }
  3084. /** @fn linkPhoneNumber
  3085. @brief Allows linking a verified phone number to the currently signed user.
  3086. @param phoneNumber Number pass in string.
  3087. @completion A completion block to be executed after linking phone number.
  3088. */
  3089. - (void)linkPhoneNumber:(NSString *_Nullable)phoneNumber
  3090. completion:(nullable testAutomationCallback)completion {
  3091. [self showSpinner:^{
  3092. [[AppManager phoneAuthProvider] verifyPhoneNumber:phoneNumber
  3093. UIDelegate:nil
  3094. completion:^(NSString *_Nullable verificationID,
  3095. NSError *_Nullable error) {
  3096. [self hideSpinner:^{
  3097. if (error) {
  3098. [self logFailure:@"failed to send verification code" error:error];
  3099. [self showMessagePrompt:error.localizedDescription];
  3100. if (completion) {
  3101. completion(error);
  3102. }
  3103. return;
  3104. }
  3105. [self logSuccess:@"Code sent"];
  3106. [self showTextInputPromptWithMessage:@"Verification code:"
  3107. keyboardType:UIKeyboardTypeNumberPad
  3108. completionBlock:^(BOOL userPressedOK,
  3109. NSString *_Nullable verificationCode) {
  3110. if (!userPressedOK || !verificationCode.length) {
  3111. return;
  3112. }
  3113. [self showSpinner:^{
  3114. FIRPhoneAuthCredential *credential =
  3115. [[AppManager phoneAuthProvider] credentialWithVerificationID:verificationID
  3116. verificationCode:verificationCode];
  3117. [[self user] linkAndRetrieveDataWithCredential:credential
  3118. completion:^(FIRAuthDataResult *_Nullable result,
  3119. NSError *_Nullable error) {
  3120. [self hideSpinner:^{
  3121. if (error) {
  3122. if (error.code == FIRAuthErrorCodeCredentialAlreadyInUse) {
  3123. [self showMessagePromptWithTitle:@"Phone number is already linked to "
  3124. @"another user"
  3125. message:@"Tap Ok to sign in with that user now."
  3126. showCancelButton:YES
  3127. completion:^(BOOL userPressedOK,
  3128. NSString *_Nullable userInput) {
  3129. if (userPressedOK) {
  3130. // If FIRAuthErrorCodeCredentialAlreadyInUse error, sign in with the
  3131. // provided credential.
  3132. [self showSpinner:^{
  3133. FIRPhoneAuthCredential *credential =
  3134. error.userInfo[FIRAuthErrorUserInfoUpdatedCredentialKey];
  3135. [[AppManager auth] signInAndRetrieveDataWithCredential:credential
  3136. completion:^(FIRAuthDataResult *_Nullable result,
  3137. NSError *_Nullable error) {
  3138. [self hideSpinner:^{
  3139. if (error) {
  3140. [self logFailure:@"failed to verify phone number" error:error];
  3141. [self showMessagePrompt:error.localizedDescription];
  3142. if (completion) {
  3143. completion(error);
  3144. }
  3145. return;
  3146. }
  3147. }];
  3148. }];
  3149. }];
  3150. }
  3151. }];
  3152. } else {
  3153. [self logFailure:@"link phone number failed" error:error];
  3154. [self showMessagePrompt:error.localizedDescription];
  3155. }
  3156. return;
  3157. }
  3158. [self logSuccess:@"link phone number succeeded."];
  3159. if (completion) {
  3160. completion(nil);
  3161. }
  3162. }];
  3163. }];
  3164. }];
  3165. }];
  3166. }];
  3167. }];
  3168. }];
  3169. }
  3170. /** @fn linkPhoneNumberWithPrompt
  3171. @brief Allows linking a verified phone number to the currently signed user via popup prompt.
  3172. */
  3173. - (void)linkPhoneNumberWithPrompt {
  3174. [self showTextInputPromptWithMessage:@"Phone #:"
  3175. keyboardType:UIKeyboardTypePhonePad
  3176. completionBlock:^(BOOL userPressedOK, NSString *_Nullable phoneNumber) {
  3177. if (!userPressedOK || !phoneNumber.length) {
  3178. return;
  3179. }
  3180. [self linkPhoneNumber:phoneNumber completion:nil];
  3181. }];
  3182. }
  3183. /** @fn signInAnonymously
  3184. @brief Signs in as an anonymous user.
  3185. */
  3186. - (void)signInAnonymously {
  3187. [[AppManager auth] signInAnonymouslyWithCompletion:^(FIRAuthDataResult *_Nullable result,
  3188. NSError *_Nullable error) {
  3189. if (error) {
  3190. [self logFailure:@"sign-in anonymously failed" error:error];
  3191. } else {
  3192. [self logSuccess:@"sign-in anonymously succeeded."];
  3193. [self log:[NSString stringWithFormat:@"User ID : %@", result.user.uid]];
  3194. }
  3195. [self showTypicalUIForUserUpdateResultsWithTitle:kSignInAnonymouslyButtonText error:error];
  3196. }];
  3197. }
  3198. /** @fn signInAnonymouslyAuthDataResult
  3199. @brief Signs in as an anonymous user, receiving an auth result containing a signed in user upon
  3200. success.
  3201. */
  3202. - (void)signInAnonymouslyAuthDataResult {
  3203. [[AppManager auth] signInAnonymouslyWithCompletion:
  3204. ^(FIRAuthDataResult *_Nullable authResult, NSError *_Nullable error) {
  3205. if (error) {
  3206. [self logFailure:@"sign-in anonymously failed" error:error];
  3207. } else {
  3208. [self logSuccess:@"sign-in anonymously succeeded."];
  3209. [self log:[NSString stringWithFormat:@"User ID : %@", authResult.user.uid]];
  3210. }
  3211. [self showTypicalUIForUserUpdateResultsWithTitle:kSignInAnonymouslyButtonText error:error];
  3212. }];
  3213. }
  3214. /** @fn signInWithGitHub
  3215. @brief Signs in as a GitHub user. Prompts the user for an access token and uses this access
  3216. token to create a GitHub (generic) credential for signing in.
  3217. */
  3218. - (void)signInWithGitHub {
  3219. [self showTextInputPromptWithMessage:@"GitHub Access Token:"
  3220. completionBlock:^(BOOL userPressedOK, NSString *_Nullable accessToken) {
  3221. if (!userPressedOK || !accessToken.length) {
  3222. return;
  3223. }
  3224. FIROAuthCredential *credential =
  3225. [FIROAuthProvider credentialWithProviderID:FIRGitHubAuthProviderID accessToken:accessToken];
  3226. if (credential) {
  3227. [[AppManager auth] signInAndRetrieveDataWithCredential:credential
  3228. completion:^(FIRAuthDataResult *_Nullable result,
  3229. NSError *_Nullable error) {
  3230. if (error) {
  3231. [self logFailure:@"sign-in with provider failed" error:error];
  3232. } else {
  3233. [self logSuccess:@"sign-in with provider succeeded."];
  3234. }
  3235. [self showTypicalUIForUserUpdateResultsWithTitle:@"Sign-In" error:error];
  3236. }];
  3237. }
  3238. }];
  3239. }
  3240. /** @fn deleteApp
  3241. @brief Deletes the @c FIRApp associated with our @c FIRAuth instance.
  3242. */
  3243. - (void)deleteApp {
  3244. [[FIRApp defaultApp] deleteApp:^(BOOL success) {
  3245. [self log:success ? @"App deleted successfully." : @"Failed to delete app."];
  3246. }];
  3247. }
  3248. /** @fn timeAuthInitialization
  3249. @brief Times FIRAuth instance initialization time.
  3250. */
  3251. - (void)timeAuthInitialization {
  3252. // Temporarily disable auth state listener to avoid interfering with the result.
  3253. [[NSNotificationCenter defaultCenter] removeObserver:self
  3254. name:FIRAuthStateDidChangeNotification
  3255. object:nil];
  3256. [self showSpinner:^() {
  3257. dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_HIGH, 0), ^() {
  3258. const int numberOfRuns = 4096;
  3259. FIRApp *app = [FIRApp defaultApp];
  3260. NSString *key = NSStringFromClass([FIRAuth class]);
  3261. NSDate *startTime = [NSDate date];
  3262. for (int i = 0; i < numberOfRuns; i++) {
  3263. @autoreleasepool {
  3264. [FIRAppAssociationRegistration deregisterObjectWithHost:app key:key];
  3265. [FIRAuth auth];
  3266. }
  3267. }
  3268. NSDate *endTime = [NSDate date];
  3269. dispatch_async(dispatch_get_main_queue(), ^() {
  3270. // Re-enable auth state listener.
  3271. [[NSNotificationCenter defaultCenter] addObserver:self
  3272. selector:@selector(authStateChangedForAuth:)
  3273. name:FIRAuthStateDidChangeNotification
  3274. object:nil];
  3275. [self hideSpinner:^() {
  3276. NSTimeInterval averageTime = [endTime timeIntervalSinceDate:startTime] / numberOfRuns;
  3277. NSString *message = [NSString stringWithFormat:
  3278. @"Each [FIRAuth auth] takes average of %.3f ms for %d runs",
  3279. averageTime * 1000, numberOfRuns];
  3280. [self showMessagePromptWithTitle:@"Timing Result"
  3281. message:message
  3282. showCancelButton:NO
  3283. completion:nil];
  3284. }];
  3285. });
  3286. });
  3287. }];
  3288. }
  3289. #pragma mark - Helpers
  3290. /** @fn user
  3291. @brief The user to use for user operations. Takes into account the "use signed-in user vs. use
  3292. user in memory" setting.
  3293. */
  3294. - (FIRUser *)user {
  3295. return _useUserInMemory ? _userInMemory : [AppManager auth].currentUser;
  3296. }
  3297. /** @fn actionCodeSettings
  3298. @brief Returns the action code settings for this app.
  3299. */
  3300. - (FIRActionCodeSettings *)actionCodeSettings {
  3301. FIRActionCodeSettings *actionCodeSettings = [[FIRActionCodeSettings alloc] init];
  3302. actionCodeSettings.URL = _actionCodeContinueURL;
  3303. actionCodeSettings.handleCodeInApp = _actionCodeRequestType == ActionCodeRequestTypeInApp;
  3304. return actionCodeSettings;
  3305. }
  3306. /** @fn stringWithAdditionalUserInfo:
  3307. @brief Gets the string description of the additional user info.
  3308. @param additionalUserInfo The additional user info in question.
  3309. @return A string to description the additional user info.
  3310. */
  3311. - (NSString *)stringWithAdditionalUserInfo:(nullable FIRAdditionalUserInfo *)additionalUserInfo {
  3312. if (!additionalUserInfo) {
  3313. return @"(no additional user info)";
  3314. }
  3315. NSString *newUserString = additionalUserInfo.isNewUser ? @"new user" : @"existing user";
  3316. return [NSString stringWithFormat:@"%@: %@", newUserString, additionalUserInfo.profile];
  3317. }
  3318. /** @fn showTypicalUIForUserUpdateResultsWithTitle:error:
  3319. @brief Shows a prompt if error is non-nil with the localized description of the error.
  3320. @param resultsTitle The title of the prompt
  3321. @param error The error details to display if non-nil.
  3322. */
  3323. - (void)showTypicalUIForUserUpdateResultsWithTitle:(NSString *)resultsTitle
  3324. error:(NSError *)error {
  3325. if (error) {
  3326. NSString *message = [NSString stringWithFormat:@"%@ (%ld)\n%@",
  3327. error.domain,
  3328. (long)error.code,
  3329. error.localizedDescription];
  3330. if (error.code == FIRAuthErrorCodeAccountExistsWithDifferentCredential) {
  3331. NSString *errorEmail = error.userInfo[FIRAuthErrorUserInfoEmailKey];
  3332. resultsTitle = [NSString stringWithFormat:@"Existing email : %@", errorEmail];
  3333. }
  3334. [self showMessagePromptWithTitle:resultsTitle
  3335. message:message
  3336. showCancelButton:NO
  3337. completion:nil];
  3338. return;
  3339. }
  3340. [self updateUserInfo];
  3341. }
  3342. /** @fn showUIForAuthDataResultWithResult:error:
  3343. @brief Shows a prompt if error is non-nil with the localized description of the error.
  3344. @param result The auth data result if non-nil.
  3345. @param error The error details to display if non-nil.
  3346. */
  3347. - (void)showUIForAuthDataResultWithResult:(FIRAuthDataResult *)result
  3348. error:(NSError *)error {
  3349. NSString *errorMessage = [NSString stringWithFormat:@"%@ (%ld)\n%@",
  3350. error.domain ?: @"",
  3351. (long)error.code,
  3352. error.localizedDescription ?: @""];
  3353. [self showMessagePromptWithTitle:@"Error"
  3354. message:errorMessage
  3355. showCancelButton:NO
  3356. completion:^(BOOL userPressedOK,
  3357. NSString *_Nullable userInput) {
  3358. [self showMessagePromptWithTitle:@"Profile Info"
  3359. message:[self stringWithAdditionalUserInfo:result.additionalUserInfo]
  3360. showCancelButton:NO
  3361. completion:nil];
  3362. [self updateUserInfo];
  3363. }];
  3364. }
  3365. - (void)doSignInWithCustomToken:(NSString *_Nullable)userEnteredTokenText {
  3366. [[AppManager auth] signInWithCustomToken:userEnteredTokenText
  3367. completion:^(FIRAuthDataResult *_Nullable result,
  3368. NSError *_Nullable error) {
  3369. if (error) {
  3370. [self logFailure:@"sign-in with custom token failed" error:error];
  3371. [self showMessagePromptWithTitle:kSignInErrorAlertTitle
  3372. message:error.localizedDescription
  3373. showCancelButton:NO
  3374. completion:nil];
  3375. return;
  3376. }
  3377. [self logSuccess:@"sign-in with custom token succeeded."];
  3378. [self showMessagePromptWithTitle:kSignedInAlertTitle
  3379. message:result.user.displayName
  3380. showCancelButton:NO
  3381. completion:nil];
  3382. }];
  3383. }
  3384. - (void)doSignInAndRetrieveDataWithCustomToken:(NSString *_Nullable)userEnteredTokenText {
  3385. [[AppManager auth] signInWithCustomToken:userEnteredTokenText
  3386. completion:^(FIRAuthDataResult *_Nullable result,
  3387. NSError *_Nullable error) {
  3388. if (error) {
  3389. [self logFailure:@"sign-in with custom token failed" error:error];
  3390. [self showMessagePromptWithTitle:kSignInErrorAlertTitle
  3391. message:error.localizedDescription
  3392. showCancelButton:NO
  3393. completion:nil];
  3394. return;
  3395. }
  3396. [self logSuccess:@"sign-in with custom token succeeded."];
  3397. [self showMessagePromptWithTitle:kSignedInAlertTitle
  3398. message:result.user.displayName
  3399. showCancelButton:NO
  3400. completion:nil];
  3401. }];
  3402. }
  3403. - (void)updateUserInfo {
  3404. [_userInfoTableViewCell updateContentsWithUser:[AppManager auth].currentUser];
  3405. [_userInMemoryInfoTableViewCell updateContentsWithUser:_userInMemory];
  3406. }
  3407. - (void)authStateChangedForAuth:(NSNotification *)notification {
  3408. [self updateUserInfo];
  3409. if (notification) {
  3410. [self log:[NSString stringWithFormat:
  3411. @"received FIRAuthStateDidChange notification on user '%@'.",
  3412. ((FIRAuth *)notification.object).currentUser.uid]];
  3413. }
  3414. }
  3415. /** @fn clearConsole
  3416. @brief Clears the console text.
  3417. */
  3418. - (IBAction)clearConsole:(id)sender {
  3419. [_consoleString appendString:@"\n\n"];
  3420. _consoleTextView.text = @"";
  3421. }
  3422. /** @fn copyConsole
  3423. @brief Copies the current console string to the clipboard.
  3424. */
  3425. - (IBAction)copyConsole:(id)sender {
  3426. UIPasteboard *pasteboard = [UIPasteboard generalPasteboard];
  3427. pasteboard.string = _consoleString ?: @"";
  3428. }
  3429. @end
  3430. NS_ASSUME_NONNULL_END