MainViewController+Custom.m 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  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+Custom.h"
  17. #import "AppManager.h"
  18. #import "MainViewController+Internal.h"
  19. #import "CustomTokenDataEntryViewController.h"
  20. NS_ASSUME_NONNULL_BEGIN
  21. @implementation MainViewController (Custom)
  22. - (StaticContentTableViewSection *)customAuthSection {
  23. __weak typeof(self) weakSelf = self;
  24. return [StaticContentTableViewSection sectionWithTitle:@"Custom Auth" cells:@[
  25. [StaticContentTableViewCell cellWithTitle:@"Sign in with Custom Token"
  26. action:^{ [weakSelf signInWithCustomToken]; }],
  27. ]];
  28. }
  29. - (void)signInWithCustomToken {
  30. CustomTokenDataEntryViewControllerCompletion action =
  31. ^(BOOL cancelled, NSString *_Nullable userEnteredTokenText) {
  32. if (cancelled) {
  33. [self log:@"CANCELLED:sign-in with custom token cancelled."];
  34. return;
  35. }
  36. [self doSignInWithCustomToken:userEnteredTokenText];
  37. };
  38. CustomTokenDataEntryViewController *dataEntryViewController =
  39. [[CustomTokenDataEntryViewController alloc] initWithCompletion:action];
  40. [self presentViewController:dataEntryViewController animated:YES completion:nil];
  41. }
  42. - (void)doSignInWithCustomToken:(NSString *_Nullable)userEnteredTokenText {
  43. [[AppManager auth] signInWithCustomToken:userEnteredTokenText
  44. completion:^(FIRAuthDataResult *_Nullable result,
  45. NSError *_Nullable error) {
  46. if (error) {
  47. [self logFailure:@"sign-in with custom token failed" error:error];
  48. [self showMessagePromptWithTitle:kSignInErrorAlertTitle
  49. message:error.localizedDescription
  50. showCancelButton:NO
  51. completion:nil];
  52. return;
  53. }
  54. [self logSuccess:@"sign-in with custom token succeeded."];
  55. [self showMessagePromptWithTitle:kSignedInAlertTitle
  56. message:result.user.displayName
  57. showCancelButton:NO
  58. completion:nil];
  59. }];
  60. }
  61. @end
  62. NS_ASSUME_NONNULL_END