Skip to content

Commit 910a7cc

Browse files
authored
Apple signin (#796)
1 parent 8cbf70c commit 910a7cc

26 files changed

+278
-157
lines changed

Auth/FirebaseAuthUI/FUIAuthPickerViewController.m

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@
1616

1717
#import "FUIAuthPickerViewController.h"
1818

19+
#import <AuthenticationServices/AuthenticationServices.h>
20+
1921
#import <FirebaseAuth/FirebaseAuth.h>
2022
#import "FUIAuthBaseViewController_Internal.h"
2123
#import "FUIAuthSignInButton.h"

FirebaseUI.podspec

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -133,6 +133,9 @@ Pod::Spec.new do |s|
133133
oauth.public_header_files = 'OAuth/FirebaseOAuthUI/*.h'
134134
oauth.source_files = 'OAuth/FirebaseOAuthUI/*.{h,m}'
135135
oauth.dependency 'FirebaseUI/Auth'
136+
oauth.resource_bundle = {
137+
'FirebaseOAuthUI' => ['OAuth/FirebaseOAuthUI/**/*.{png,lproj}']
138+
}
136139
oauth.xcconfig = { 'HEADER_SEARCH_PATHS' => '$(PODS_ROOT)/FirebaseUI/FirebaseOAuthUI' }
137140
end
138141

OAuth/FirebaseOAuthUI/FUIOAuth.h

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,31 @@ NS_ASSUME_NONNULL_BEGIN
6161
loginHintKey:(nullable NSString *)loginHintKey
6262
NS_DESIGNATED_INITIALIZER;
6363

64+
/** @fn twitterAuthProvider
65+
@brief Built-in OAuth provider for Twitter.
66+
*/
67+
+ (FUIOAuth *)twitterAuthProvider;
68+
69+
/** @fn githubAuthProvider
70+
@brief Built-in OAuth provider for Github.
71+
*/
72+
+ (FUIOAuth *)githubAuthProvider;
73+
74+
/** @fn microsoftAuthProvider
75+
@brief Built-in OAuth provider for Microsoft.
76+
*/
77+
+ (FUIOAuth *)microsoftAuthProvider;
78+
79+
/** @fn yahooAuthProvider
80+
@brief Built-in OAuth provider for Yahoo.
81+
*/
82+
+ (FUIOAuth *)yahooAuthProvider;
83+
84+
/** @fn appleAuthProvider
85+
@brief Built-in OAuth provider for Apple.
86+
*/
87+
+ (FUIOAuth *)appleAuthProvider API_AVAILABLE(ios(13.0));
88+
6489
@end
6590

6691
NS_ASSUME_NONNULL_END

OAuth/FirebaseOAuthUI/FUIOAuth.m

Lines changed: 143 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@
1616

1717
#import <FirebaseUI/FirebaseAuthUI.h>
1818

19+
#import <AuthenticationServices/AuthenticationServices.h>
20+
1921
#import "FUIOAuth.h"
2022
#import <FirebaseUI/FUIAuthBaseViewController.h>
2123
#import <FirebaseUI/FUIAuthBaseViewController_Internal.h>
@@ -38,7 +40,9 @@
3840

3941
NS_ASSUME_NONNULL_BEGIN
4042

41-
@interface FUIOAuth ()
43+
@interface FUIOAuth () <ASAuthorizationControllerDelegate, ASAuthorizationControllerPresentationContextProviding> {
44+
FUIAuthProviderSignInCompletionBlock _providerSignInCompletion;
45+
}
4246

4347
/** @property authUI
4448
@brief FUIAuth instance of the application.
@@ -117,12 +121,82 @@ - (instancetype)initWithAuthUI:(FUIAuth *)authUI
117121
_icon = iconImage;
118122
_scopes = scopes;
119123
_customParameters = customParameters;
120-
_provider = [FIROAuthProvider providerWithProviderID:self.providerID];
121124
_loginHintKey = loginHintKey;
125+
if (![_providerID isEqualToString:@"facebook.com"] && ![_providerID isEqualToString:@"apple.com"]) {
126+
_provider = [FIROAuthProvider providerWithProviderID:self.providerID];
127+
}
122128
}
123129
return self;
124130
}
125131

132+
+ (FUIOAuth *)twitterAuthProvider {
133+
return [[FUIOAuth alloc] initWithAuthUI:[FUIAuth defaultAuthUI]
134+
providerID:@"twitter.com"
135+
buttonLabelText:@"Sign in with Twitter"
136+
shortName:@"Twitter"
137+
buttonColor:[UIColor colorWithRed:71.0f/255.0f
138+
green:154.0f/255.0f
139+
blue:234.0f/255.0f
140+
alpha:1.0f]
141+
iconImage:[FUIAuthUtils imageNamed:@"ic_twitter"
142+
fromBundleNameOrNil:@"FirebaseOAuthUI"]
143+
scopes:@[@"user.readwrite"]
144+
customParameters:@{@"prompt" : @"consent"}
145+
loginHintKey:nil];
146+
}
147+
148+
+ (FUIOAuth *)githubAuthProvider {
149+
return [[FUIOAuth alloc] initWithAuthUI:[FUIAuth defaultAuthUI]
150+
providerID:@"github.com"
151+
buttonLabelText:@"Sign in with GitHub"
152+
shortName:@"GitHub"
153+
buttonColor:[UIColor colorWithRed:0.2 green:0.2 blue:0.2 alpha:1.0]
154+
iconImage:[FUIAuthUtils imageNamed:@"ic_github"
155+
fromBundleNameOrNil:@"FirebaseOAuthUI"]
156+
scopes:nil
157+
customParameters:nil
158+
loginHintKey:nil];
159+
}
160+
161+
+ (FUIOAuth *)microsoftAuthProvider {
162+
return [[FUIOAuth alloc] initWithAuthUI:[FUIAuth defaultAuthUI]
163+
providerID:@"microsoft.com"
164+
buttonLabelText:@"Sign in with Microsoft"
165+
shortName:@"Microsoft"
166+
buttonColor:[UIColor colorWithRed:.18 green:.18 blue:.18 alpha:1.0]
167+
iconImage:[FUIAuthUtils imageNamed:@"ic_microsoft"
168+
fromBundleNameOrNil:@"FirebaseOAuthUI"]
169+
scopes:@[@"user.readwrite"]
170+
customParameters:@{@"prompt" : @"consent"}
171+
loginHintKey:@"login_hint"];
172+
}
173+
174+
+ (FUIOAuth *)yahooAuthProvider {
175+
return [[FUIOAuth alloc] initWithAuthUI:[FUIAuth defaultAuthUI]
176+
providerID:@"yahoo.com"
177+
buttonLabelText:@"Sign in with Yahoo"
178+
shortName:@"Yahoo"
179+
buttonColor:[UIColor colorWithRed:.45 green:.05 blue:.62 alpha:1.0]
180+
iconImage:[FUIAuthUtils imageNamed:@"ic_yahoo"
181+
fromBundleNameOrNil:@"FirebaseOAuthUI"]
182+
scopes:@[@"user.readwrite"]
183+
customParameters:@{@"prompt" : @"consent"}
184+
loginHintKey:nil];
185+
}
186+
187+
+ (FUIOAuth *)appleAuthProvider {
188+
return [[FUIOAuth alloc] initWithAuthUI:[FUIAuth defaultAuthUI]
189+
providerID:@"apple.com"
190+
buttonLabelText:@"Sign in with Apple"
191+
shortName:@"Apple"
192+
buttonColor:[UIColor colorWithRed:0.0 green:0.0 blue:0.0 alpha:1.0]
193+
iconImage:[FUIAuthUtils imageNamed:@"ic_apple"
194+
fromBundleNameOrNil:@"FirebaseOAuthUI"]
195+
scopes:@[@"name", @"email"]
196+
customParameters:nil
197+
loginHintKey:nil];
198+
}
199+
126200
#pragma mark - FUIAuthProvider
127201

128202
/** @fn accessToken:
@@ -158,41 +232,55 @@ - (void)signInWithDefaultValue:(nullable NSString *)defaultValue
158232
presentingViewController:(nullable UIViewController *)presentingViewController
159233
completion:(nullable FUIAuthProviderSignInCompletionBlock)completion {
160234
self.presentingViewController = presentingViewController;
161-
162235
FIROAuthProvider *provider = self.provider;
163-
provider.scopes = self.scopes;
164-
NSMutableDictionary *customParameters = [NSMutableDictionary dictionary];
165-
if (self.customParameters.count) {
166-
[customParameters addEntriesFromDictionary:self.customParameters];
167-
}
168-
if (self.loginHintKey.length && defaultValue.length) {
169-
customParameters[self.loginHintKey] = defaultValue;
170-
}
171-
provider.customParameters = [customParameters copy];
172-
173-
[self.provider getCredentialWithUIDelegate:nil
174-
completion:^(FIRAuthCredential *_Nullable credential,
175-
NSError *_Nullable error) {
176-
if (error) {
177-
[FUIAuthBaseViewController showAlertWithMessage:error.localizedDescription
178-
presentingViewController:presentingViewController];
179-
if (completion) {
180-
completion(nil, error, nil, nil);
181-
}
182-
return;
236+
_providerSignInCompletion = completion;
237+
238+
if ([self.providerID isEqualToString:@"apple.com"]) {
239+
if (@available(iOS 13.0, *)) {
240+
ASAuthorizationAppleIDRequest *request = [[[ASAuthorizationAppleIDProvider alloc] init] createRequest];
241+
request.requestedScopes = @[ASAuthorizationScopeFullName, ASAuthorizationScopeEmail];
242+
ASAuthorizationController* controller = [[ASAuthorizationController alloc] initWithAuthorizationRequests:@[request]];
243+
controller.delegate = self;
244+
controller.presentationContextProvider = self;
245+
[controller performRequests];
246+
} else {
247+
NSLog(@"Sign in with Apple is only available on iOS 13+.");
183248
}
184-
if (completion) {
185-
UIActivityIndicatorView *activityView =
186-
[FUIAuthBaseViewController addActivityIndicator:presentingViewController.view];
187-
[activityView startAnimating];
188-
FIRAuthResultCallback result = ^(FIRUser *_Nullable user,
189-
NSError *_Nullable error) {
190-
[activityView stopAnimating];
191-
[activityView removeFromSuperview];
192-
};
193-
completion(credential, nil, result, nil);
249+
} else {
250+
provider.scopes = self.scopes;
251+
NSMutableDictionary *customParameters = [NSMutableDictionary dictionary];
252+
if (self.customParameters.count) {
253+
[customParameters addEntriesFromDictionary:self.customParameters];
194254
}
195-
}];
255+
if (self.loginHintKey.length && defaultValue.length) {
256+
customParameters[self.loginHintKey] = defaultValue;
257+
}
258+
provider.customParameters = [customParameters copy];
259+
260+
[self.provider getCredentialWithUIDelegate:nil
261+
completion:^(FIRAuthCredential *_Nullable credential,
262+
NSError *_Nullable error) {
263+
if (error) {
264+
[FUIAuthBaseViewController showAlertWithMessage:error.localizedDescription
265+
presentingViewController:presentingViewController];
266+
if (completion) {
267+
completion(nil, error, nil, nil);
268+
}
269+
return;
270+
}
271+
if (completion) {
272+
UIActivityIndicatorView *activityView =
273+
[FUIAuthBaseViewController addActivityIndicator:presentingViewController.view];
274+
[activityView startAnimating];
275+
FIRAuthResultCallback result = ^(FIRUser *_Nullable user,
276+
NSError *_Nullable error) {
277+
[activityView stopAnimating];
278+
[activityView removeFromSuperview];
279+
};
280+
completion(credential, nil, result, nil);
281+
}
282+
}];
283+
}
196284
}
197285

198286
- (void)signOut {
@@ -212,6 +300,27 @@ - (BOOL)handleOpenURL:(NSURL *)URL sourceApplication:(nullable NSString *)source
212300
return NO;
213301
}
214302

303+
#pragma mark - ASAuthorizationControllerDelegate
304+
305+
- (void)authorizationController:(ASAuthorizationController *)controller didCompleteWithAuthorization:(ASAuthorization *)authorization API_AVAILABLE(ios(13.0)) {
306+
ASAuthorizationAppleIDCredential* appleIDCredential = authorization.credential;
307+
NSString *idToken = [NSString stringWithUTF8String:[appleIDCredential.identityToken bytes]];
308+
FIROAuthCredential *credential = [FIROAuthProvider credentialWithProviderID:@"apple.com"
309+
IDToken:idToken
310+
accessToken:nil];
311+
_providerSignInCompletion(credential, nil, nil, nil);
312+
}
313+
314+
- (void)authorizationController:(ASAuthorizationController *)controller didCompleteWithError:(NSError *)error API_AVAILABLE(ios(13.0)) {
315+
NSLog(@"%@", error.description);
316+
}
317+
318+
#pragma mark - ASAuthorizationControllerPresentationContextProviding
319+
320+
- (ASPresentationAnchor)presentationAnchorForAuthorizationController:(ASAuthorizationController *)controller API_AVAILABLE(ios(13.0)) {
321+
return self.presentingViewController.view.window;
322+
}
323+
215324
@end
216325

217326
NS_ASSUME_NONNULL_END
1.29 KB
Loading
Loading
Loading
800 Bytes
Loading
Loading
Loading
508 Bytes
Loading
619 Bytes
Loading
626 Bytes
Loading
1.5 KB
Loading
Loading
Loading
1.85 KB
Loading
Loading
Loading

samples/objc/FirebaseUI-demo-objc.xcodeproj/project.pbxproj

Lines changed: 0 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,6 @@
77
objects = {
88

99
/* Begin PBXBuildFile section */
10-
896400F7222F583100CEF7D7 /* mssymbol.png in Resources */ = {isa = PBXBuildFile; fileRef = 896400F6222F583100CEF7D7 /* mssymbol.png */; };
11-
89B822042256838500CEF7D7 /* twtrsymbol.png in Resources */ = {isa = PBXBuildFile; fileRef = 89B822032256838500CEF7D7 /* twtrsymbol.png */; };
12-
89D6278022AB14FD00CEF7D7 /* githubsymbol.png in Resources */ = {isa = PBXBuildFile; fileRef = 89D6277F22AB14FD00CEF7D7 /* githubsymbol.png */; };
1310
8D7D5DC11D9D9536006C1857 /* GoogleService-Info.plist in Resources */ = {isa = PBXBuildFile; fileRef = 8D7D5DC01D9D9536006C1857 /* GoogleService-Info.plist */; };
1411
8D7F86B51D9DAA0100C2A122 /* FUIStorageViewController.m in Sources */ = {isa = PBXBuildFile; fileRef = 8D7F86B41D9DAA0100C2A122 /* FUIStorageViewController.m */; };
1512
C30AEB0A1ED610740084E328 /* Main.storyboard in Resources */ = {isa = PBXBuildFile; fileRef = C30AEB071ED610740084E328 /* Main.storyboard */; };
@@ -41,9 +38,6 @@
4138
/* End PBXBuildFile section */
4239

4340
/* Begin PBXFileReference section */
44-
896400F6222F583100CEF7D7 /* mssymbol.png */ = {isa = PBXFileReference; lastKnownFileType = image.png; path = mssymbol.png; sourceTree = "<group>"; };
45-
89B822032256838500CEF7D7 /* twtrsymbol.png */ = {isa = PBXFileReference; lastKnownFileType = image.png; path = twtrsymbol.png; sourceTree = "<group>"; };
46-
89D6277F22AB14FD00CEF7D7 /* githubsymbol.png */ = {isa = PBXFileReference; lastKnownFileType = image.png; path = githubsymbol.png; sourceTree = "<group>"; };
4741
8D7D5DC01D9D9536006C1857 /* GoogleService-Info.plist */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text.plist.xml; path = "GoogleService-Info.plist"; sourceTree = "<group>"; };
4842
8D7F86B31D9DAA0100C2A122 /* FUIStorageViewController.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; name = FUIStorageViewController.h; path = Storage/FUIStorageViewController.h; sourceTree = "<group>"; };
4943
8D7F86B41D9DAA0100C2A122 /* FUIStorageViewController.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; name = FUIStorageViewController.m; path = Storage/FUIStorageViewController.m; sourceTree = "<group>"; };
@@ -194,16 +188,6 @@
194188
path = Pods;
195189
sourceTree = "<group>";
196190
};
197-
896400F5222F580400CEF7D7 /* Resources */ = {
198-
isa = PBXGroup;
199-
children = (
200-
89D6277F22AB14FD00CEF7D7 /* githubsymbol.png */,
201-
89B822032256838500CEF7D7 /* twtrsymbol.png */,
202-
896400F6222F583100CEF7D7 /* mssymbol.png */,
203-
);
204-
path = Resources;
205-
sourceTree = "<group>";
206-
};
207191
8D7D5DC51D9DA075006C1857 /* Storage */ = {
208192
isa = PBXGroup;
209193
children = (
@@ -216,7 +200,6 @@
216200
C30AEB041ED610740084E328 /* Resources */ = {
217201
isa = PBXGroup;
218202
children = (
219-
896400F5222F580400CEF7D7 /* Resources */,
220203
C30AEB0D1ED610780084E328 /* Localizable.strings */,
221204
C30AEB071ED610740084E328 /* Main.storyboard */,
222205
);
@@ -473,11 +456,8 @@
473456
buildActionMask = 2147483647;
474457
files = (
475458
C3A8B7C21DAF073400CDF0ED /* FUICustomPasswordSignInViewController.xib in Resources */,
476-
89D6278022AB14FD00CEF7D7 /* githubsymbol.png in Resources */,
477-
896400F7222F583100CEF7D7 /* mssymbol.png in Resources */,
478459
C30AEB0B1ED610780084E328 /* Localizable.strings in Resources */,
479460
C30AEB0A1ED610740084E328 /* Main.storyboard in Resources */,
480-
89B822042256838500CEF7D7 /* twtrsymbol.png in Resources */,
481461
C34FC1FF1DB17C8B009ED69A /* FUICustomPasswordVerificationViewController.xib in Resources */,
482462
C3AC67621D81FE6B00FC956D /* FUIChatMessageTableViewCell.xib in Resources */,
483463
C3A8B7CC1DB1454D00CDF0ED /* FUICustomPasswordRecoveryViewController.xib in Resources */,

samples/objc/FirebaseUI-demo-objc/FirebaseUI-demo-objc.entitlements

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,10 @@
44
<dict>
55
<key>aps-environment</key>
66
<string>development</string>
7+
<key>com.apple.developer.applesignin</key>
8+
<array>
9+
<string>Default</string>
10+
</array>
711
<key>com.apple.developer.associated-domains</key>
812
<array>
913
<string>applinks:fex9s.app.goo.gl</string>

0 commit comments

Comments
 (0)