MainViewController+OAuth.m 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  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+OAuth.h"
  17. #import "AppManager.h"
  18. #import "FIROAuthProvider.h"
  19. #import "MainViewController+Internal.h"
  20. NS_ASSUME_NONNULL_BEGIN
  21. @implementation MainViewController (OAuth)
  22. - (StaticContentTableViewSection *)oAuthSection {
  23. __weak typeof(self) weakSelf = self;
  24. return [StaticContentTableViewSection sectionWithTitle:@"OAuth" cells:@[
  25. [StaticContentTableViewCell cellWithTitle:@"Sign in with Google"
  26. action:^{ [weakSelf signInGoogleHeadfulLite]; }],
  27. [StaticContentTableViewCell cellWithTitle:@"Sign in with Microsoft"
  28. action:^{ [weakSelf signInMicrosoftHeadfulLite]; }],
  29. [StaticContentTableViewCell cellWithTitle:@"Sign In with GitHub"
  30. action:^{ [weakSelf signInWithGitHub]; }],
  31. ]];
  32. }
  33. - (void)signInGoogleHeadfulLite {
  34. FIROAuthProvider *provider = self.googleOAuthProvider;
  35. provider.customParameters = @{
  36. @"prompt" : @"consent",
  37. };
  38. provider.scopes = @[ @"profile", @"email", @"https://www.googleapis.com/auth/plus.me" ];
  39. [self showSpinner:^{
  40. [[AppManager auth] signInWithProvider:provider
  41. UIDelegate:nil
  42. completion:^(FIRAuthDataResult *_Nullable authResult,
  43. NSError *_Nullable error) {
  44. [self hideSpinner:^{
  45. if (error) {
  46. [self logFailure:@"sign-in with provider (Google) failed" error:error];
  47. } else if (authResult.additionalUserInfo) {
  48. [self logSuccess:[self stringWithAdditionalUserInfo:authResult.additionalUserInfo]];
  49. if (self.isNewUserToggleOn) {
  50. NSString *newUserString = authResult.additionalUserInfo.newUser ?
  51. @"New user" : @"Existing user";
  52. [self showMessagePromptWithTitle:@"New or Existing"
  53. message:newUserString
  54. showCancelButton:NO
  55. completion:nil];
  56. }
  57. }
  58. [self showTypicalUIForUserUpdateResultsWithTitle:@"Sign-In Error" error:error];
  59. }];
  60. }];
  61. }];
  62. }
  63. - (void)signInMicrosoftHeadfulLite {
  64. FIROAuthProvider *provider = self.microsoftOAuthProvider;
  65. provider.customParameters = @{
  66. @"prompt" : @"consent",
  67. @"login_hint" : @"tu8731@gmail.com",
  68. };
  69. provider.scopes = @[ @"user.readwrite,calendars.read" ];
  70. [self showSpinner:^{
  71. [provider getCredentialWithUIDelegate:nil completion:^(FIRAuthCredential *_Nullable credential,
  72. NSError *_Nullable error) {
  73. if (error) {
  74. [self logFailure:@"sign-in with Microsoft failed" error:error];
  75. return;
  76. }
  77. [[AppManager auth] signInWithCredential:credential
  78. completion:^(FIRAuthDataResult *_Nullable
  79. authResult,
  80. NSError *_Nullable error) {
  81. [self hideSpinner:^{
  82. if (error) {
  83. [self logFailure:@"sign-in with Microsoft failed" error:error];
  84. return;
  85. } else {
  86. [self logSuccess:@"sign-in with Microsoft (headful-lite) succeeded."];
  87. }
  88. [self showTypicalUIForUserUpdateResultsWithTitle:@"Sign-In Error" error:error];
  89. }];
  90. }];
  91. }];
  92. }];
  93. }
  94. - (void)signInWithGitHub {
  95. [self showTextInputPromptWithMessage:@"GitHub Access Token:"
  96. completionBlock:^(BOOL userPressedOK, NSString *_Nullable accessToken) {
  97. if (!userPressedOK || !accessToken.length) {
  98. return;
  99. }
  100. FIROAuthCredential *credential =
  101. [FIROAuthProvider credentialWithProviderID:FIRGitHubAuthProviderID accessToken:accessToken];
  102. if (credential) {
  103. [[AppManager auth] signInWithCredential:credential
  104. completion:^(FIRAuthDataResult *_Nullable result,
  105. NSError *_Nullable error) {
  106. if (error) {
  107. [self logFailure:@"sign-in with provider failed" error:error];
  108. } else {
  109. [self logSuccess:@"sign-in with provider succeeded."];
  110. }
  111. [self showTypicalUIForUserUpdateResultsWithTitle:@"Sign-In" error:error];
  112. }];
  113. }
  114. }];
  115. }
  116. @end
  117. NS_ASSUME_NONNULL_END