MainViewController.m 148 KB

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