Skip to content

Commit 820f89f

Browse files
Implement Anonymous Login as a Provider (#505)
1 parent 573883e commit 820f89f

File tree

25 files changed

+1817
-37
lines changed

25 files changed

+1817
-37
lines changed
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
//
2+
// Copyright (c) 2016 Google Inc.
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+
@class FUIAuth;
17+
#import "FUIAuthProvider.h"
18+
19+
NS_ASSUME_NONNULL_BEGIN
20+
21+
/** @class FUIAnonymousAuth
22+
@brief AuthUI components for Anonymous Sign In.
23+
*/
24+
@interface FUIAnonymousAuth : NSObject <FUIAuthProvider>
25+
26+
/** @fn init
27+
@brief Initialize the instance with the default AuthUI.
28+
*/
29+
- (instancetype)init;
30+
31+
/** @fn initWithAuthUI:
32+
@param authUI The @c FUIAuth instance that manages controllers of this provider.
33+
*/
34+
- (instancetype)initWithAuthUI:(FUIAuth *)authUI NS_DESIGNATED_INITIALIZER;
35+
36+
@end
37+
38+
NS_ASSUME_NONNULL_END
Lines changed: 150 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,150 @@
1+
//
2+
// Copyright (c) 2016 Google Inc.
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+
17+
#import "FUIAnonymousAuth.h"
18+
#import "FUIAuth_Internal.h"
19+
#import "FUIAuthBaseViewController.h"
20+
#import "FUIAuthErrorUtils.h"
21+
#import "FUIAuthBaseViewController_Internal.h"
22+
#import "FUIAuthStrings.h"
23+
#import "FUIAuthUtils.h"
24+
25+
/** @var kTableName
26+
@brief The name of the strings table to search for localized strings.
27+
*/
28+
static NSString *const kTableName = @"FirebaseAnonymousAuthUI";
29+
30+
/** @var kBundleName
31+
@brief The name of the bundle to search for resources.
32+
*/
33+
static NSString *const kBundleName = @"FirebaseAnonymousAuthUI";
34+
35+
/** @var kSignInAsGuest
36+
@brief The string key for localized button text.
37+
*/
38+
static NSString *const kSignInAsGuest = @"SignInAsGuest";
39+
40+
NS_ASSUME_NONNULL_BEGIN
41+
42+
@implementation FUIAnonymousAuth {
43+
/** The @c FUIAuth instance of the application. */
44+
FUIAuth *_authUI;
45+
46+
/** @var _presentingViewController
47+
@brief The presenting view controller for interactive sign-in.
48+
*/
49+
UIViewController *_presentingViewController;
50+
}
51+
52+
- (instancetype)init {
53+
return [self initWithAuthUI:[FUIAuth defaultAuthUI]];
54+
}
55+
56+
- (instancetype)initWithAuthUI:(FUIAuth *)authUI {
57+
if (self = [super init]) {
58+
_authUI = authUI;
59+
}
60+
return self;
61+
}
62+
63+
#pragma mark - FUIAuthProvider
64+
65+
- (nullable NSString *)providerID {
66+
return nil;
67+
}
68+
69+
/** @fn accessToken:
70+
@brief Anonymous Auth token is matched by FirebaseUI User Access Token
71+
*/
72+
- (nullable NSString *)accessToken {
73+
return nil;
74+
}
75+
76+
/** @fn idToken:
77+
@brief Anonymous Auth Token Secret is matched by FirebaseUI User Id Token
78+
*/
79+
- (nullable NSString *)idToken {
80+
return nil;
81+
}
82+
83+
- (NSString *)shortName {
84+
return @"Anonymous";
85+
}
86+
87+
- (NSString *)signInLabel {
88+
return FUILocalizedStringFromTableInBundle(kSignInAsGuest, kTableName, kBundleName);
89+
}
90+
91+
- (UIImage *)icon {
92+
return [FUIAuthUtils imageNamed:@"ic_anonymous" fromBundle:kBundleName];
93+
}
94+
95+
- (UIColor *)buttonBackgroundColor {
96+
return [UIColor colorWithRed:244.0f/255.0f green:180.0f/255.0f blue:0.0f/255.0f alpha:1.0f];
97+
}
98+
99+
- (UIColor *)buttonTextColor {
100+
return [UIColor whiteColor];
101+
}
102+
103+
#pragma clang diagnostic push
104+
#pragma clang diagnostic ignored "-Wdeprecated-implementations"
105+
- (void)signInWithEmail:(nullable NSString *)email
106+
presentingViewController:(nullable UIViewController *)presentingViewController
107+
completion:(nullable FIRAuthProviderSignInCompletionBlock)completion {
108+
[self signInWithDefaultValue:email
109+
presentingViewController:presentingViewController
110+
completion:completion];
111+
}
112+
#pragma clang diagnostic pop
113+
114+
- (void)signInWithDefaultValue:(nullable NSString *)defaultValue
115+
presentingViewController:(nullable UIViewController *)presentingViewController
116+
completion:(nullable FIRAuthProviderSignInCompletionBlock)completion {
117+
[_authUI.auth signInAnonymouslyWithCompletion:^(FIRAuthDataResult * _Nullable authResult,
118+
NSError * _Nullable error) {
119+
if (error) {
120+
[FUIAuthBaseViewController showAlertWithMessage:error.localizedDescription
121+
presentingViewController:presentingViewController];
122+
if (completion) {
123+
completion(nil, error, nil);
124+
}
125+
return;
126+
}
127+
if (completion) {
128+
completion(nil, error, nil);
129+
}
130+
}];
131+
}
132+
133+
- (void)signOut {
134+
FIRUser *user = _authUI.auth.currentUser;
135+
__weak UIViewController *weakController = _presentingViewController;
136+
if (user.isAnonymous) {
137+
[user deleteWithCompletion:^(NSError * _Nullable error) {
138+
if (error) {
139+
__strong UIViewController *presentingViewController = weakController;
140+
[FUIAuthBaseViewController showAlertWithMessage:error.localizedDescription
141+
presentingViewController:presentingViewController];
142+
return;
143+
}
144+
}];
145+
}
146+
}
147+
148+
@end
149+
150+
NS_ASSUME_NONNULL_END
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
//
2+
// Copyright (c) 2016 Google Inc.
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+
17+
#import <UIKit/UIKit.h>
18+
19+
//! Project version number for FirebaseAnonymousAuthUI.
20+
FOUNDATION_EXPORT double FirebaseAnonymousAuthUIVersionNumber;
21+
22+
//! Project version string for FirebaseAnonymousAuthUI.
23+
FOUNDATION_EXPORT const unsigned char FirebaseAnonymousAuthUIVersionString[];
24+
25+
#import "FUIAnonymousAuth.h"
26+
27+

FirebaseAnonymousAuthUI/Info.plist

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
3+
<plist version="1.0">
4+
<dict>
5+
<key>CFBundleDevelopmentRegion</key>
6+
<string>en</string>
7+
<key>CFBundleExecutable</key>
8+
<string>$(EXECUTABLE_NAME)</string>
9+
<key>CFBundleIdentifier</key>
10+
<string>$(PRODUCT_BUNDLE_IDENTIFIER)</string>
11+
<key>CFBundleInfoDictionaryVersion</key>
12+
<string>6.0</string>
13+
<key>CFBundleName</key>
14+
<string>$(PRODUCT_NAME)</string>
15+
<key>CFBundlePackageType</key>
16+
<string>FMWK</string>
17+
<key>CFBundleShortVersionString</key>
18+
<string>1.0</string>
19+
<key>CFBundleSignature</key>
20+
<string>????</string>
21+
<key>CFBundleVersion</key>
22+
<string>$(CURRENT_PROJECT_VERSION)</string>
23+
<key>NSPrincipalClass</key>
24+
<string></string>
25+
</dict>
26+
</plist>
254 Bytes
Loading
455 Bytes
Loading
654 Bytes
Loading
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
/* The text of the button used to sign-in with guest. */
2+
"SignInAsGuest" = "Sign in as guest";
Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
//
2+
// Copyright (c) 2016 Google Inc.
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+
17+
#import "FUIAnonymousAuth.h"
18+
#import <FirebaseAuth/FirebaseAuth.h>
19+
#import <FirebaseAuthUI/FUIAuthErrorUtils.h>
20+
#import "FUIAuthUtils.h"
21+
#import <FirebaseAuthUI/FUIAuth.h>
22+
#import <FirebaseAnonymousAuthUI/FirebaseAnonymousAuthUI.h>
23+
#import <OCMock/OCMock.h>
24+
#import <XCTest/XCTest.h>
25+
26+
@interface FirebaseAnonymousAuthUITests : XCTestCase
27+
@property (nonatomic, strong) FUIAnonymousAuth *provider;
28+
@end
29+
30+
@implementation FirebaseAnonymousAuthUITests
31+
32+
- (void)setUp {
33+
[super setUp];
34+
35+
id mockUtilsClass = OCMClassMock([FUIAuthUtils class]);
36+
OCMStub(ClassMethod([mockUtilsClass bundleNamed:OCMOCK_ANY])).
37+
andReturn([NSBundle bundleForClass:[FUIAnonymousAuth class]]);
38+
39+
id authUIClass = OCMClassMock([FUIAuth class]);
40+
OCMStub(ClassMethod([authUIClass authUIWithAuth:OCMOCK_ANY])).
41+
andReturn(authUIClass);
42+
43+
id authClass = OCMClassMock([FIRAuth class]);
44+
OCMStub(ClassMethod([authClass auth])).
45+
andReturn(authClass);
46+
47+
FIRAuth *auth = [FIRAuth auth];
48+
FUIAuth *authUI = [FUIAuth authUIWithAuth:auth];
49+
self.provider = [[FUIAnonymousAuth alloc] initWithAuthUI:authUI];
50+
}
51+
52+
- (void)tearDown {
53+
self.provider = nil;
54+
[super tearDown];
55+
}
56+
57+
- (void)testProviderValidity {
58+
XCTAssertNotNil(self.provider);
59+
XCTAssertNotNil(self.provider.icon);
60+
XCTAssertNotNil(self.provider.signInLabel);
61+
XCTAssertNotNil(self.provider.buttonBackgroundColor);
62+
XCTAssertNotNil(self.provider.buttonTextColor);
63+
XCTAssertNil(self.provider.providerID);
64+
XCTAssertNotNil(self.provider.shortName);
65+
XCTAssertTrue(self.provider.signInLabel.length != 0);
66+
XCTAssertNil(self.provider.accessToken);
67+
XCTAssertNil(self.provider.idToken);
68+
}
69+
70+
@end
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
3+
<plist version="1.0">
4+
<dict>
5+
<key>CFBundleDevelopmentRegion</key>
6+
<string>en</string>
7+
<key>CFBundleExecutable</key>
8+
<string>$(EXECUTABLE_NAME)</string>
9+
<key>CFBundleIdentifier</key>
10+
<string>$(PRODUCT_BUNDLE_IDENTIFIER)</string>
11+
<key>CFBundleInfoDictionaryVersion</key>
12+
<string>6.0</string>
13+
<key>CFBundleName</key>
14+
<string>$(PRODUCT_NAME)</string>
15+
<key>CFBundlePackageType</key>
16+
<string>BNDL</string>
17+
<key>CFBundleShortVersionString</key>
18+
<string>1.0</string>
19+
<key>CFBundleSignature</key>
20+
<string>????</string>
21+
<key>CFBundleVersion</key>
22+
<string>1</string>
23+
</dict>
24+
</plist>

0 commit comments

Comments
 (0)