MainViewController.m 148 KB

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