MainViewController+OAuth.m 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214
  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. [StaticContentTableViewCell cellWithTitle:@"Sign In with Twitter (headful-lite)"
  32. action:^{ [weakSelf signInTwitterHeadfulLite]; }],
  33. [StaticContentTableViewCell cellWithTitle:@"Sign In with Linkedin"
  34. action:^{ [weakSelf signInLinkedinHeadfulLite]; }],
  35. [StaticContentTableViewCell cellWithTitle:@"Sign In with Yahoo"
  36. action:^{ [weakSelf signInYahooHeadfulLite]; }],
  37. ]];
  38. }
  39. - (void)signInGoogleHeadfulLite {
  40. FIROAuthProvider *provider = self.googleOAuthProvider;
  41. provider.customParameters = @{
  42. @"prompt" : @"consent",
  43. };
  44. provider.scopes = @[ @"profile", @"email", @"https://www.googleapis.com/auth/plus.me" ];
  45. [self showSpinner:^{
  46. [[AppManager auth] signInWithProvider:provider
  47. UIDelegate:nil
  48. completion:^(FIRAuthDataResult *_Nullable authResult,
  49. NSError *_Nullable error) {
  50. [self hideSpinner:^{
  51. if (error) {
  52. [self logFailure:@"sign-in with provider (Google) failed" error:error];
  53. } else if (authResult.additionalUserInfo) {
  54. [self logSuccess:[self stringWithAdditionalUserInfo:authResult.additionalUserInfo]];
  55. if (self.isNewUserToggleOn) {
  56. NSString *newUserString = authResult.additionalUserInfo.newUser ?
  57. @"New user" : @"Existing user";
  58. [self showMessagePromptWithTitle:@"New or Existing"
  59. message:newUserString
  60. showCancelButton:NO
  61. completion:nil];
  62. }
  63. }
  64. [self showTypicalUIForUserUpdateResultsWithTitle:@"Sign-In Error" error:error];
  65. }];
  66. }];
  67. }];
  68. }
  69. - (void)signInMicrosoftHeadfulLite {
  70. FIROAuthProvider *provider = self.microsoftOAuthProvider;
  71. provider.customParameters = @{
  72. @"prompt" : @"consent",
  73. @"login_hint" : @"tu8731@gmail.com",
  74. };
  75. provider.scopes = @[ @"user.readwrite,calendars.read" ];
  76. [self showSpinner:^{
  77. [provider getCredentialWithUIDelegate:nil completion:^(FIRAuthCredential *_Nullable credential,
  78. NSError *_Nullable error) {
  79. if (error) {
  80. [self logFailure:@"sign-in with Microsoft failed" error:error];
  81. return;
  82. }
  83. [[AppManager auth] signInWithCredential:credential
  84. completion:^(FIRAuthDataResult *_Nullable
  85. authResult,
  86. NSError *_Nullable error) {
  87. [self hideSpinner:^{
  88. if (error) {
  89. [self logFailure:@"sign-in with Microsoft failed" error:error];
  90. return;
  91. } else {
  92. [self logSuccess:@"sign-in with Microsoft (headful-lite) succeeded."];
  93. }
  94. [self showTypicalUIForUserUpdateResultsWithTitle:@"Sign-In Error" error:error];
  95. }];
  96. }];
  97. }];
  98. }];
  99. }
  100. - (void)signInWithGitHub {
  101. [self showTextInputPromptWithMessage:@"GitHub Access Token:"
  102. completionBlock:^(BOOL userPressedOK, NSString *_Nullable accessToken) {
  103. if (!userPressedOK || !accessToken.length) {
  104. return;
  105. }
  106. FIROAuthCredential *credential =
  107. [FIROAuthProvider credentialWithProviderID:FIRGitHubAuthProviderID accessToken:accessToken];
  108. if (credential) {
  109. [[AppManager auth] signInWithCredential:credential
  110. completion:^(FIRAuthDataResult *_Nullable result,
  111. NSError *_Nullable error) {
  112. if (error) {
  113. [self logFailure:@"sign-in with provider failed" error:error];
  114. } else {
  115. [self logSuccess:@"sign-in with provider succeeded."];
  116. }
  117. [self showTypicalUIForUserUpdateResultsWithTitle:@"Sign-In" error:error];
  118. }];
  119. }
  120. }];
  121. }
  122. - (void)signInTwitterHeadfulLite {
  123. FIROAuthProvider *provider = self.twitterOAuthProvider;
  124. [self showSpinner:^{
  125. [provider getCredentialWithUIDelegate:nil completion:^(FIRAuthCredential *_Nullable credential,
  126. NSError *_Nullable error) {
  127. if (error) {
  128. [self logFailure:@"sign-in with Twitter failed" error:error];
  129. return;
  130. }
  131. [[AppManager auth] signInWithCredential:credential
  132. completion:^(FIRAuthDataResult *_Nullable
  133. authResult,
  134. NSError *_Nullable error) {
  135. [self hideSpinner:^{
  136. if (error) {
  137. [self logFailure:@"sign-in with Twitter (headful-lite) failed" error:error];
  138. return;
  139. } else {
  140. [self logSuccess:@"sign-in with Twitter (headful-lite) succeeded."];
  141. }
  142. [self showTypicalUIForUserUpdateResultsWithTitle:@"Sign-In Error" error:error];
  143. }];
  144. }];
  145. }];
  146. }];
  147. }
  148. - (void)signInLinkedinHeadfulLite {
  149. FIROAuthProvider *provider = self.linkedinOAuthProvider;
  150. [self showSpinner:^{
  151. [provider getCredentialWithUIDelegate:nil completion:^(FIRAuthCredential *_Nullable credential,
  152. NSError *_Nullable error) {
  153. if (error) {
  154. [self logFailure:@"sign-in with Linkedin failed" error:error];
  155. return;
  156. }
  157. [[AppManager auth] signInWithCredential:credential
  158. completion:^(FIRAuthDataResult *_Nullable
  159. authResult,
  160. NSError *_Nullable error) {
  161. [self hideSpinner:^{
  162. if (error) {
  163. [self logFailure:@"sign-in with Linkedin (headful-lite) failed" error:error];
  164. return;
  165. } else {
  166. [self logSuccess:@"sign-in with Linkedin (headful-lite) succeeded."];
  167. }
  168. [self showTypicalUIForUserUpdateResultsWithTitle:@"Sign-In Error" error:error];
  169. }];
  170. }];
  171. }];
  172. }];
  173. }
  174. - (void)signInYahooHeadfulLite {
  175. FIROAuthProvider *provider = self.yahooOAuthProvider;
  176. [self showSpinner:^{
  177. [provider getCredentialWithUIDelegate:nil completion:^(FIRAuthCredential *_Nullable credential,
  178. NSError *_Nullable error) {
  179. if (error) {
  180. [self logFailure:@"sign-in with Yahoo failed" error:error];
  181. return;
  182. }
  183. [[AppManager auth] signInWithCredential:credential
  184. completion:^(FIRAuthDataResult *_Nullable
  185. authResult,
  186. NSError *_Nullable error) {
  187. [self hideSpinner:^{
  188. if (error) {
  189. [self logFailure:@"sign-in with Yahoo (headful-lite) failed" error:error];
  190. return;
  191. } else {
  192. [self logSuccess:@"sign-in with Yahoo (headful-lite) succeeded."];
  193. }
  194. [self showTypicalUIForUserUpdateResultsWithTitle:@"Sign-In Error" error:error];
  195. }];
  196. }];
  197. }];
  198. }];
  199. }
  200. @end
  201. NS_ASSUME_NONNULL_END