Skip to content

Commit

Permalink
When in memory token cache still valid callback directly
Browse files Browse the repository at this point in the history
  • Loading branch information
tonyli508 committed Dec 2, 2015
1 parent 19f5ab0 commit 68439c6
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 49 deletions.
2 changes: 1 addition & 1 deletion LinkedinSwift.podspec
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
Pod::Spec.new do |s|

s.name = "LinkedinSwift"
s.version = "1.4"
s.version = "1.5"
s.summary = "Linkedin Oauth Helper, depend on Linkedin Native App installed or not, using Linkdin IOS SDK or UIWebView to login, support Swift with iOS 7"

s.homepage = "https://github.com/tonyli508/LinkedinSwift.git"
Expand Down
Binary file not shown.
102 changes: 54 additions & 48 deletions LinkedinSwift/LinkedinSwift/sources/LinkedinSwiftHelper.m
Original file line number Diff line number Diff line change
Expand Up @@ -42,67 +42,73 @@ - (instancetype)initWithConfiguration:(LinkedinSwiftConfiguration*)_configuratio

- (void)authorizeSuccess:(LinkedinSwiftAuthRequestSuccessCallback)successCallback error:(LinkedinSwiftRequestErrorCallback)errorCallback cancel:(LinkedinSwiftRequestCancelCallback)cancelCallback {

__block LinkedinSwiftHelper *this = self;
if ([LinkedinSwiftHelper isLinkedinAppInstalled]) {

/**
* If Linkedin app installed, use Linkedin sdk
*/
__block LISDKSession *session = [[LISDKSessionManager sharedInstance] session];

// check if session is still cached
if (session && session.isValid) {
/**
* If previous token still in memory callback directly
*/
if (lsAccessToken != nil && [lsAccessToken.expireDate timeIntervalSinceNow] > 0) {
successCallback(lsAccessToken);
} else {
__block LinkedinSwiftHelper *this = self;
if ([LinkedinSwiftHelper isLinkedinAppInstalled]) {

this->lsAccessToken = [[LSLinkedinToken alloc] initWithAccessToken:session.accessToken.accessTokenValue expireDate:session.accessToken.expiration fromMobileSDK: YES];
successCallback(this->lsAccessToken);
} else {
/**
* If Linkedin app installed, use Linkedin sdk
*/
__block LISDKSession *session = [[LISDKSessionManager sharedInstance] session];

// no cache, create a new session
[LISDKSessionManager createSessionWithAuth:configuration.permissions state:@"GET-ACCESS-TOKEN" showGoToAppStoreDialog:YES successBlock:^(NSString *returnState) {
// check if session is still cached
if (session && session.isValid) {

// refresh session
session = [[LISDKSessionManager sharedInstance] session];
this->lsAccessToken = [[LSLinkedinToken alloc] initWithAccessToken:session.accessToken.accessTokenValue expireDate:session.accessToken.expiration fromMobileSDK: YES];
successCallback(this->lsAccessToken);
} else {

} errorBlock:^(NSError *error) {
// error code 3 means user cancelled, LISDKErrorCode.USER_CANCELLED doesn't work
if (error.code == 3) {
cancelCallback();
} else {
errorCallback(error);
}
}];
}
} else {

/**
* If Linkedin app is not installed, present a model webview to let use login
*
* WARNING: here we can check the cache save api call as well,
* but there is a problem when you login on other devices the accessToken you cached will invalid,
* and only you use this will be notice this, so I choose don't use this cache
*/
[httpClient getAuthorizationCode:^(NSString *code) {
// no cache, create a new session
[LISDKSessionManager createSessionWithAuth:configuration.permissions state:@"GET-ACCESS-TOKEN" showGoToAppStoreDialog:YES successBlock:^(NSString *returnState) {

// refresh session
session = [[LISDKSessionManager sharedInstance] session];
this->lsAccessToken = [[LSLinkedinToken alloc] initWithAccessToken:session.accessToken.accessTokenValue expireDate:session.accessToken.expiration fromMobileSDK: YES];
successCallback(this->lsAccessToken);

} errorBlock:^(NSError *error) {
// error code 3 means user cancelled, LISDKErrorCode.USER_CANCELLED doesn't work
if (error.code == 3) {
cancelCallback();
} else {
errorCallback(error);
}
}];
}
} else {

[this->httpClient getAccessToken:code success:^(NSDictionary *dictionary) {
/**
* If Linkedin app is not installed, present a model webview to let use login
*
* WARNING: here we can check the cache save api call as well,
* but there is a problem when you login on other devices the accessToken you cached will invalid,
* and only you use this will be notice this, so I choose don't use this cache
*/
[httpClient getAuthorizationCode:^(NSString *code) {

NSString *accessToken = [dictionary objectForKey:@"access_token"];
NSNumber *expiresInSec = [dictionary objectForKey:@"expires_in"];
[this->httpClient getAccessToken:code success:^(NSDictionary *dictionary) {

NSString *accessToken = [dictionary objectForKey:@"access_token"];
NSNumber *expiresInSec = [dictionary objectForKey:@"expires_in"];

this->lsAccessToken = [[LSLinkedinToken alloc] initWithAccessToken:accessToken expireDate:[NSDate dateWithTimeIntervalSinceNow:expiresInSec.doubleValue] fromMobileSDK: NO];
successCallback(this->lsAccessToken);
} failure:^(NSError *error) {
errorCallback(error);
}];

this->lsAccessToken = [[LSLinkedinToken alloc] initWithAccessToken:accessToken expireDate:[NSDate dateWithTimeIntervalSinceNow:expiresInSec.doubleValue] fromMobileSDK: NO];
successCallback(this->lsAccessToken);
} cancel:^{
cancelCallback();
} failure:^(NSError *error) {
errorCallback(error);
}];

} cancel:^{
cancelCallback();
} failure:^(NSError *error) {
errorCallback(error);
}];
}
}

}

#pragma mark -
Expand Down

0 comments on commit 68439c6

Please sign in to comment.