Skip to content

Commit c81b7af

Browse files
committed
Return OAuth access/id tokens after a successful sign-in operation
1 parent b0f11db commit c81b7af

File tree

6 files changed

+162
-16
lines changed

6 files changed

+162
-16
lines changed

FirebaseAuthUI/FIRAuthProviderUI.h

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,8 +91,21 @@ typedef void (^FIRAuthProviderSignInCompletionBlock)(FIRAuthCredential *_Nullabl
9191
*/
9292
- (void)signOutWithAuth:(FIRAuth *)auth;
9393

94+
/** @property accessToken
95+
@brief User Access Token obtained during sign in.
96+
*/
97+
@property(nonatomic, copy, readonly) NSString *accessToken;
98+
99+
94100
@optional;
95101

102+
103+
/** @property idToken
104+
@brief User Id Token obtained during sign in. Not all providers can return, thus it's optional
105+
*/
106+
@property(nonatomic, copy, readonly) NSString *idToken;
107+
108+
96109
/** @fn handleOpenURL:
97110
@brief May be used to help complete a sign-in flow which requires a callback from Safari.
98111
@param URL The URL which may be handled by the auth provider if an URL is expected.

FirebaseFacebookAuthUI/FIRFacebookAuthUI.m

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -115,6 +115,17 @@ - (NSString *)providerID {
115115
return FIRFacebookAuthProviderID;
116116
}
117117

118+
- (NSString *)accessToken {
119+
return [FBSDKAccessToken currentAccessToken].tokenString;
120+
}
121+
122+
/** @fn idToken:
123+
@brief Facebook doesn't provide User Id Token during sign in flow
124+
*/
125+
- (NSString *)idToken {
126+
return nil;
127+
}
128+
118129
- (NSString *)shortName {
119130
return @"Facebook";
120131
}

FirebaseGoogleAuthUI/FIRGoogleAuthUI.m

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -131,6 +131,14 @@ - (NSString *)providerID {
131131
return FIRGoogleAuthProviderID;
132132
}
133133

134+
- (NSString *)accessToken {
135+
return [GIDSignIn sharedInstance].currentUser.authentication.accessToken;
136+
}
137+
138+
- (NSString *)idToken {
139+
return [GIDSignIn sharedInstance].currentUser.authentication.idToken;
140+
}
141+
134142
- (NSString *)shortName {
135143
return @"Google";
136144
}

FirebaseTwitterAuthUI/FIRTwitterAuthUI.m

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,20 @@ - (NSString *)providerID {
8282
return FIRTwitterAuthProviderID;
8383
}
8484

85+
/** @fn accessToken:
86+
@brief Twitter Auth token is matched by FirebaseUI User Access Token
87+
*/
88+
- (NSString *)accessToken {
89+
return [Twitter sharedInstance].sessionStore.session.authToken;
90+
}
91+
92+
/** @fn idToken:
93+
@brief Twitter Auth Token Secret is matched by FirebaseUI User Id Token
94+
*/
95+
- (NSString *)idToken {
96+
return [Twitter sharedInstance].sessionStore.session.authTokenSecret;
97+
}
98+
8599
- (NSString *)shortName {
86100
return @"Twitter";
87101
}

samples/objc/FirebaseUIChat/Base.lproj/Main.storyboard

Lines changed: 46 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
22
<document type="com.apple.InterfaceBuilder3.CocoaTouch.Storyboard.XIB" version="3.0" toolsVersion="10117" systemVersion="15G1004" targetRuntime="iOS.CocoaTouch" propertyAccessControl="none" useAutolayout="YES" useTraitCollections="YES" initialViewController="7RW-VL-qNI">
33
<dependencies>
4+
<deployment identifier="iOS"/>
45
<plugIn identifier="com.apple.InterfaceBuilder.IBCocoaTouchPlugin" version="10085"/>
56
</dependencies>
67
<scenes>
@@ -124,7 +125,7 @@
124125
<autoresizingMask key="autoresizingMask" widthSizable="YES" heightSizable="YES"/>
125126
<color key="backgroundColor" red="0.93725490199999995" green="0.93725490199999995" blue="0.95686274510000002" alpha="1" colorSpace="calibratedRGB"/>
126127
<toolbar key="tableFooterView" opaque="NO" clearsContextBeforeDrawing="NO" contentMode="scaleToFill" id="83E-4Z-ptP">
127-
<rect key="frame" x="0.0" y="397" width="600" height="44"/>
128+
<rect key="frame" x="0.0" y="587" width="600" height="44"/>
128129
<autoresizingMask key="autoresizingMask" widthSizable="YES" flexibleMinY="YES"/>
129130
<items>
130131
<barButtonItem style="plain" systemItem="flexibleSpace" id="o3y-o4-Gpu"/>
@@ -194,6 +195,48 @@
194195
</tableViewCell>
195196
</cells>
196197
</tableViewSection>
198+
<tableViewSection headerTitle="Provider Access Token" id="E31-4c-QDa">
199+
<cells>
200+
<tableViewCell clipsSubviews="YES" contentMode="scaleToFill" selectionStyle="none" indentationWidth="10" textLabel="k9f-GH-UBQ" style="IBUITableViewCellStyleDefault" id="mdj-SS-dqJ">
201+
<rect key="frame" x="0.0" y="430" width="600" height="44"/>
202+
<autoresizingMask key="autoresizingMask"/>
203+
<tableViewCellContentView key="contentView" opaque="NO" clipsSubviews="YES" multipleTouchEnabled="YES" contentMode="center" tableViewCell="mdj-SS-dqJ" id="oCT-Ra-9l7">
204+
<rect key="frame" x="0.0" y="0.0" width="600" height="43"/>
205+
<autoresizingMask key="autoresizingMask"/>
206+
<subviews>
207+
<label opaque="NO" multipleTouchEnabled="YES" contentMode="left" text="Title" textAlignment="natural" lineBreakMode="tailTruncation" numberOfLines="0" baselineAdjustment="alignBaselines" adjustsFontSizeToFit="NO" id="k9f-GH-UBQ">
208+
<rect key="frame" x="15" y="0.0" width="570" height="43"/>
209+
<autoresizingMask key="autoresizingMask" flexibleMaxX="YES" flexibleMaxY="YES"/>
210+
<fontDescription key="fontDescription" type="system" pointSize="16"/>
211+
<color key="textColor" red="0.0" green="0.0" blue="0.0" alpha="1" colorSpace="calibratedRGB"/>
212+
<nil key="highlightedColor"/>
213+
</label>
214+
</subviews>
215+
</tableViewCellContentView>
216+
</tableViewCell>
217+
</cells>
218+
</tableViewSection>
219+
<tableViewSection headerTitle="Provider ID Token" id="GaU-wa-aFK">
220+
<cells>
221+
<tableViewCell clipsSubviews="YES" contentMode="scaleToFill" selectionStyle="none" indentationWidth="10" textLabel="M61-v5-jhZ" style="IBUITableViewCellStyleDefault" id="b45-X5-eC9">
222+
<rect key="frame" x="0.0" y="525" width="600" height="44"/>
223+
<autoresizingMask key="autoresizingMask"/>
224+
<tableViewCellContentView key="contentView" opaque="NO" clipsSubviews="YES" multipleTouchEnabled="YES" contentMode="center" tableViewCell="b45-X5-eC9" id="ofC-qT-5cz">
225+
<rect key="frame" x="0.0" y="0.0" width="600" height="43"/>
226+
<autoresizingMask key="autoresizingMask"/>
227+
<subviews>
228+
<label opaque="NO" multipleTouchEnabled="YES" contentMode="left" text="Title" textAlignment="natural" lineBreakMode="tailTruncation" numberOfLines="0" baselineAdjustment="alignBaselines" adjustsFontSizeToFit="NO" id="M61-v5-jhZ">
229+
<rect key="frame" x="15" y="0.0" width="570" height="43"/>
230+
<autoresizingMask key="autoresizingMask" flexibleMaxX="YES" flexibleMaxY="YES"/>
231+
<fontDescription key="fontDescription" type="system" pointSize="16"/>
232+
<color key="textColor" red="0.0" green="0.0" blue="0.0" alpha="1" colorSpace="calibratedRGB"/>
233+
<nil key="highlightedColor"/>
234+
</label>
235+
</subviews>
236+
</tableViewCellContentView>
237+
</tableViewCell>
238+
</cells>
239+
</tableViewSection>
197240
</sections>
198241
<connections>
199242
<outlet property="dataSource" destination="CNE-cz-Qwp" id="MrI-Jx-5UJ"/>
@@ -202,7 +245,9 @@
202245
</tableView>
203246
<connections>
204247
<outlet property="btnAuthorization" destination="bgV-bu-PcA" id="bBY-yq-fyj"/>
248+
<outlet property="cellAccessToken" destination="mdj-SS-dqJ" id="8WY-EZ-Gy6"/>
205249
<outlet property="cellEmail" destination="MCw-zF-UTn" id="sw3-8D-gut"/>
250+
<outlet property="cellIdToken" destination="b45-X5-eC9" id="Tyx-rd-dja"/>
206251
<outlet property="cellName" destination="kCl-ip-jc2" id="C8k-DT-KVQ"/>
207252
<outlet property="cellSignIn" destination="ORf-bf-Sq2" id="5Lp-Ik-8fw"/>
208253
<outlet property="cellUID" destination="EWm-GE-IZV" id="eTu-Vt-UCP"/>

samples/objc/FirebaseUIChat/Samples/Auth/FIRAuthViewController.m

Lines changed: 70 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,8 @@ @interface FIRAuthViewController () <FIRAuthUIDelegate>
3131
@property (weak, nonatomic) IBOutlet UITableViewCell *cellEmail;
3232
@property (weak, nonatomic) IBOutlet UITableViewCell *cellUID;
3333
@property (weak, nonatomic) IBOutlet UIBarButtonItem *btnAuthorization;
34+
@property (weak, nonatomic) IBOutlet UITableViewCell *cellAccessToken;
35+
@property (weak, nonatomic) IBOutlet UITableViewCell *cellIdToken;
3436

3537
@property (nonatomic) FIRAuth *auth;
3638
@property (nonatomic) FIRAuthUI *authUI;
@@ -41,9 +43,14 @@ @interface FIRAuthViewController () <FIRAuthUIDelegate>
4143

4244
@implementation FIRAuthViewController
4345

46+
#pragma mark - UIViewController methods
47+
4448
- (void)viewDidLoad {
4549
[super viewDidLoad];
4650

51+
self.tableView.rowHeight = UITableViewAutomaticDimension;
52+
self.tableView.estimatedRowHeight = 240;
53+
4754
self.auth = [FIRAuth auth];
4855
self.authUI = [FIRAuthUI defaultAuthUI];
4956
self.authUI.delegate = self;
@@ -74,6 +81,14 @@ -(void)viewDidDisappear:(BOOL)animated {
7481
[self.auth removeAuthStateDidChangeListener:self.authStateDidChangeHandle];
7582
}
7683

84+
#pragma mark - UITableViewController methods
85+
86+
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
87+
return UITableViewAutomaticDimension;
88+
}
89+
90+
#pragma mark - UI methods
91+
7792
- (void)updateUI:(FIRAuth * _Nonnull) auth withUser:(FIRUser * _Nullable) user {
7893
if (user) {
7994
self.cellSignIn.textLabel.text = @"YES";
@@ -91,35 +106,30 @@ - (void)updateUI:(FIRAuth * _Nonnull) auth withUser:(FIRUser * _Nullable) user {
91106
self.btnAuthorization.title = @"Sign In";
92107
}
93108

109+
self.cellAccessToken.textLabel.text = [self getAllAccessTokens];
110+
self.cellIdToken.textLabel.text = [self getAllIdTokens];
111+
112+
[self.tableView reloadData];
94113
}
95114

96115
- (IBAction)onAuthorization:(id)sender {
97116
if (!self.auth.currentUser) {
98117
UIViewController *controller = [self.authUI authViewController];
99118
[self presentViewController:controller animated:YES completion:nil];
100119
} else {
101-
NSError *error;
102-
[self.auth signOut:&error];
103-
if (error) {
104-
NSLog(@"Sign out error: %@", error);
105-
}
120+
[self signOut];
106121
}
107122
}
108123

109124
#pragma mark - FIRAuthUIDelegate methods
110125

111126
- (void)authUI:(FIRAuthUI *)authUI didSignInWithUser:(nullable FIRUser *)user error:(nullable NSError *)error {
112127
if (error) {
113-
NSError *originalError = error.userInfo[NSUnderlyingErrorKey];
114-
UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"Error"
115-
message:originalError.localizedDescription
116-
preferredStyle:UIAlertControllerStyleAlert];
117-
UIAlertAction* closeButton = [UIAlertAction
118-
actionWithTitle:@"Close"
119-
style:UIAlertActionStyleDefault
120-
handler:nil];
121-
[alert addAction:closeButton];
122-
[self presentViewController:alert animated:YES completion:nil];
128+
NSError *detailedError = error.userInfo[NSUnderlyingErrorKey];
129+
if (!detailedError) {
130+
detailedError = error;
131+
}
132+
[self showAlert:detailedError.localizedDescription];
123133
}
124134
}
125135

@@ -145,5 +155,50 @@ - (NSString *)readFacebookAppId {
145155
return facebookAppId;
146156
}
147157

158+
- (NSString *)getAllAccessTokens {
159+
NSMutableString *result = [NSMutableString new];
160+
for (id<FIRAuthProviderUI> provider in _authUI.providers) {
161+
[result appendFormat:@"%@: %@\n", provider.shortName, provider.accessToken];
162+
}
163+
164+
return result;
165+
}
166+
167+
- (NSString *)getAllIdTokens {
168+
NSMutableString *result = [NSMutableString new];
169+
for (id<FIRAuthProviderUI> provider in _authUI.providers) {
170+
[result appendFormat:@"%@: %@\n", provider.shortName, provider.idToken];
171+
}
172+
173+
return result;
174+
}
175+
176+
- (void)signOut {
177+
// sign out from Firebase
178+
NSError *error;
179+
[self.auth signOut:&error];
180+
if (error) {
181+
[self showAlert:error.localizedDescription];
182+
}
183+
184+
// sign out from all providers (wipes provider tokens too)
185+
for (id<FIRAuthProviderUI> provider in _authUI.providers) {
186+
[provider signOutWithAuth:self.auth];
187+
}
188+
189+
}
190+
191+
- (void)showAlert:(NSString *)message {
192+
UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"Error"
193+
message:message
194+
preferredStyle:UIAlertControllerStyleAlert];
195+
UIAlertAction* closeButton = [UIAlertAction
196+
actionWithTitle:@"Close"
197+
style:UIAlertActionStyleDefault
198+
handler:nil];
199+
[alert addAction:closeButton];
200+
[self presentViewController:alert animated:YES completion:nil];
201+
202+
}
148203

149204
@end

0 commit comments

Comments
 (0)