MainViewController+Auth.m 2.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. /*
  2. * Copyright 2019 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+Auth.h"
  17. #import "AppManager.h"
  18. #import "MainViewController+Internal.h"
  19. NS_ASSUME_NONNULL_BEGIN
  20. @implementation MainViewController (Auth)
  21. - (StaticContentTableViewSection *)authSection {
  22. __weak typeof(self) weakSelf = self;
  23. return [StaticContentTableViewSection sectionWithTitle:@"Auth" cells:@[
  24. [StaticContentTableViewCell cellWithTitle:@"Sign in Anonymously"
  25. action:^{ [weakSelf signInAnonymously]; }],
  26. [StaticContentTableViewCell cellWithTitle:@"Sign out"
  27. action:^{ [weakSelf signOut]; }]
  28. ]];
  29. }
  30. - (void)signInAnonymously {
  31. [[AppManager auth] signInAnonymouslyWithCompletion:^(FIRAuthDataResult *_Nullable result,
  32. NSError *_Nullable error) {
  33. if (error) {
  34. [self logFailure:@"sign-in anonymously failed" error:error];
  35. } else {
  36. [self logSuccess:@"sign-in anonymously succeeded."];
  37. [self log:[NSString stringWithFormat:@"User ID : %@", result.user.uid]];
  38. }
  39. [self showTypicalUIForUserUpdateResultsWithTitle:@"Sign in Anonymously" error:error];
  40. }];
  41. }
  42. - (void)signInAnonymouslyWithCallback:(nullable FIRAuthDataResultCallback)callback {
  43. FIRAuth *auth = [AppManager auth];
  44. if (!auth) {
  45. [self logFailedTest:@"Could not obtain auth object."];
  46. return;
  47. }
  48. [auth signInAnonymouslyWithCompletion:^(FIRAuthDataResult *_Nullable result,
  49. NSError *_Nullable error) {
  50. if (error) {
  51. [self logFailure:@"sign-in anonymously failed" error:error];
  52. [self logFailedTest:@"Recently signed out user should be able to sign in anonymously."];
  53. return;
  54. }
  55. [self logSuccess:@"sign-in anonymously succeeded."];
  56. if (callback) {
  57. callback(result, nil);
  58. }
  59. }];
  60. }
  61. - (void)signOut {
  62. [[AuthProviders google] signOut];
  63. [[AuthProviders facebook] signOut];
  64. [[AppManager auth] signOut:NULL];
  65. }
  66. @end
  67. NS_ASSUME_NONNULL_END