Skip to content

Commit

Permalink
Fix #19: Get logged in user when starting new sessions
Browse files Browse the repository at this point in the history
  • Loading branch information
rothacr committed Jun 13, 2014
1 parent 6adefd8 commit ba75a19
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 10 deletions.
6 changes: 4 additions & 2 deletions Examples/APIExplorer/APIExplorer/JBLandingViewController.m
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@

#import "UP.h"

NSString *const kAPIExplorerID = @"3ZYR1YjGd3Q";
NSString *const kAPIExplorerSecret = @"4dd5b10b3a3a16dbf3082c86d5faff09e11a682b";
NSString *const kAPIExplorerID = @"pHekFTFd7qA";
NSString *const kAPIExplorerSecret = @"93ff020364207ebe820c4215c8f3a694b1b4ff63";

@interface JBLandingViewController ()

Expand All @@ -37,6 +37,7 @@ - (void)viewDidLoad

if (session != nil)
{
NSLog(@"Continued session for %@ %@", session.currentUser.firstName, session.currentUser.lastName);
[self performSegueWithIdentifier:@"LoggedIn" sender:nil];
}
}];
Expand All @@ -55,6 +56,7 @@ - (IBAction)login:(id)sender

if (session != nil)
{
NSLog(@"Started session for %@ %@", session.currentUser.firstName, session.currentUser.lastName);
[self performSegueWithIdentifier:@"LoggedIn" sender:nil];
}
else
Expand Down
43 changes: 35 additions & 8 deletions UPPlatformSDK/UPPlatformSDK/UPPlatform.m
Original file line number Diff line number Diff line change
Expand Up @@ -311,7 +311,16 @@ - (void)startSessionWithClientID:(NSString *)clientID clientSecret:(NSString *)c
if (token != nil)
{
self.currentSession = [[UPSession alloc] initWithToken:token];
self.sessionCompletion(self.currentSession, nil);

[UPUserAPI getCurrentUserWithCompletion:^(UPUser *user, UPURLResponse *response, NSError *error) {
if (user != nil)
{
self.currentSession.currentUser = user;
}

self.sessionCompletion(self.currentSession, nil);
}];

return;
}

Expand Down Expand Up @@ -371,14 +380,32 @@ - (void)authViewController:(UPAuthViewController *)viewController didCompleteWit
if (error == nil && data.length)
{
NSDictionary *responseJSON = [NSJSONSerialization JSONObjectWithData:data options:0 error:nil];
NSString *authToken = responseJSON[@"access_token"];
NSString *refreshToken = responseJSON[@"refresh_token"];

[self setExistingAuthToken:authToken];
[self setRefreshToken:refreshToken];

self.currentSession = [[UPSession alloc] initWithToken:authToken];
self.sessionCompletion(self.currentSession, nil);
NSString *jsonError = responseJSON[@"error"];
if (jsonError.length == 0)
{
NSString *authToken = responseJSON[@"access_token"];
NSString *refreshToken = responseJSON[@"refresh_token"];

[self setExistingAuthToken:authToken];
[self setRefreshToken:refreshToken];

self.currentSession = [[UPSession alloc] initWithToken:authToken];

[UPUserAPI getCurrentUserWithCompletion:^(UPUser *user, UPURLResponse *response, NSError *error) {
if (user != nil)
{
self.currentSession.currentUser = user;
}

self.sessionCompletion(self.currentSession, nil);
}];
}
else
{
NSString *errorDescription = responseJSON[@"error_description"];
self.sessionCompletion(nil, [NSError errorWithDomain:@"com.jawbone.up" code:0 userInfo:@{ NSLocalizedDescriptionKey : errorDescription }]);
}
}
else
{
Expand Down

0 comments on commit ba75a19

Please sign in to comment.