Skip to content

Commit a739cdf

Browse files
author
hakonk
committed
refactor: handle auth session from within RNAppAuth instance. Make use of delegate pattern for handling application:openURL:options:
1 parent 8ef0213 commit a739cdf

File tree

4 files changed

+27
-14
lines changed

4 files changed

+27
-14
lines changed

ios/RNAppAuth.m

Lines changed: 18 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,17 @@
44
#import <React/RCTConvert.h>
55
#import "RNAppAuthAuthorizationFlowManager.h"
66

7+
@interface RNAppAuth()<RNAppAuthAuthorizationFlowManagerDelegate> {
8+
id<OIDExternalUserAgentSession> _currentSession;
9+
}
10+
@end
11+
712
@implementation RNAppAuth
813

14+
-(BOOL)resumeExternalUserAgentFlowWithURL:(NSURL *)url {
15+
return [_currentSession resumeExternalUserAgentFlowWithURL:url];
16+
}
17+
918
- (dispatch_queue_t)methodQueue
1019
{
1120
return dispatch_get_main_queue();
@@ -141,26 +150,24 @@ - (void)authorizeWithConfiguration: (OIDServiceConfiguration *) configuration
141150

142151
// performs authentication request
143152
id<UIApplicationDelegate, RNAppAuthAuthorizationFlowManager> appDelegate = (id<UIApplicationDelegate, RNAppAuthAuthorizationFlowManager>)[UIApplication sharedApplication].delegate;
144-
145-
id<OIDAuthorizationFlowSession> currentSession =
146-
[OIDAuthState authStateByPresentingAuthorizationRequest:request
153+
if (![[appDelegate class] conformsToProtocol:@protocol(RNAppAuthAuthorizationFlowManager)]) {
154+
[NSException raise:@"RNAppAuth Missing protocol conformance"
155+
format:@"%@ does not conform to RNAppAuthAuthorizationFlowManager", appDelegate];
156+
}
157+
appDelegate.authorizationFlowManagerDelegate = self;
158+
__weak typeof(self) weakSelf = self;
159+
_currentSession = [OIDAuthState authStateByPresentingAuthorizationRequest:request
147160
presentingViewController:appDelegate.window.rootViewController
148161
callback:^(OIDAuthState *_Nullable authState,
149162
NSError *_Nullable error) {
163+
typeof(self) strongSelf = weakSelf;
164+
strongSelf->_currentSession = nil;
150165
if (authState) {
151166
resolve([self formatResponse:authState.lastTokenResponse]);
152167
} else {
153168
reject(@"RNAppAuth Error", [error localizedDescription], error);
154169
}
155-
156170
}]; // end [OIDAuthState authStateByPresentingAuthorizationRequest:request
157-
if ([[appDelegate class] conformsToProtocol:@protocol(RNAppAuthAuthorizationFlowManager)]
158-
&& [appDelegate respondsToSelector: @selector(setCurrentAuthorizationFlowSession:)]) {
159-
[appDelegate setCurrentAuthorizationFlowSession:currentSession];
160-
} else {
161-
[NSException raise:@"RNAppAuth Missing protocol conformance"
162-
format:@"%@ does not conform to RNAppAuthAuthorizationFlowManager", appDelegate];
163-
}
164171
}
165172

166173

ios/RNAppAuth.podspec

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,5 +16,5 @@ Pod::Spec.new do |s|
1616
s.requires_arc = true
1717

1818
s.dependency "React"
19-
s.dependency "AppAuth"
19+
s.dependency "AppAuth", "0.94.0"
2020
end
Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
#import <Foundation/Foundation.h>
2-
#import <AppAuth/AppAuth.h>
2+
#import "RNAppAuthAuthorizationFlowManagerDelegate.h"
33

44
@protocol RNAppAuthAuthorizationFlowManager <NSObject>
55
@required
6-
-(void)setCurrentAuthorizationFlowSession:(id<OIDAuthorizationFlowSession>)session;
6+
@property(nonatomic, weak)id<RNAppAuthAuthorizationFlowManagerDelegate>authorizationFlowManagerDelegate;
77
@end
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
#import <Foundation/Foundation.h>
2+
3+
@protocol RNAppAuthAuthorizationFlowManagerDelegate <NSObject>
4+
@required
5+
-(BOOL)resumeExternalUserAgentFlowWithURL:(NSURL *)url;
6+
@end

0 commit comments

Comments
 (0)