Skip to content

Commit

Permalink
improve error handling
Browse files Browse the repository at this point in the history
  • Loading branch information
siacomuzzi committed Mar 31, 2014
1 parent 7dae0af commit 39d121a
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 16 deletions.
5 changes: 3 additions & 2 deletions Auth0Client/Auth0Client.m
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ - (Auth0WebViewController*)getAuthenticator:(NSString *)connection scope:(NSStri
callback, connection];
}

Auth0WebViewController *webController = [[Auth0WebViewController alloc] initWithAuthorizeUrl:[NSURL URLWithString:url] returnUrl:callback allowsClose:YES withCompletionHandler:^(NSString *token, NSString * jwtToken){
Auth0WebViewController *webController = [[Auth0WebViewController alloc] initWithAuthorizeUrl:[NSURL URLWithString:url] returnUrl:callback allowsClose:YES withCompletionHandler:^(NSString *token, NSString * jwtToken, NSString * error){
if (token) {

[self getUserInfo:token withCompletionHandler:^(NSMutableDictionary* profile) {
Expand All @@ -91,7 +91,8 @@ - (Auth0WebViewController*)getAuthenticator:(NSString *)connection scope:(NSStri
}];
}
else {
block([[NSMutableDictionary alloc] initWithObjectsAndKeys: @"error", @"Authentication Failed", nil]);
NSString * errorMsg = error ? error : @"Authentication Failed";
block([[NSMutableDictionary alloc] initWithObjectsAndKeys: errorMsg, @"error", nil]);
}
}];

Expand Down
4 changes: 2 additions & 2 deletions Auth0Client/Auth0WebViewController.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,11 @@
NSURL * _url;
NSURL * _authzUrl;
NSString * _returnUrl;
void (^ _block)(NSString* token, NSString * jwtToken);
void (^ _block)(NSString* token, NSString * jwtToken, NSString * error);
BOOL _allowsClose;
}

- (id)initWithAuthorizeUrl:(NSURL *)authzUrl returnUrl:(NSString*)returnUrl allowsClose:(BOOL)allowsClose withCompletionHandler:(void (^)(NSString * token, NSString * jwtToken))block;
- (id)initWithAuthorizeUrl:(NSURL *)authzUrl returnUrl:(NSString*)returnUrl allowsClose:(BOOL)allowsClose withCompletionHandler:(void (^)(NSString * token, NSString * jwtToken, NSString * error))block;

+ (void)clearCookies;

Expand Down
27 changes: 15 additions & 12 deletions Auth0Client/Auth0WebViewController.m
Original file line number Diff line number Diff line change
Expand Up @@ -107,21 +107,24 @@ - (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)
NSDictionary* queryString = [self queryStringForUrl:request.URL];
NSString * accessToken = queryString[@"access_token"];
NSString * jwtToken = queryString[@"id_token"];
NSString * error = queryString[@"denied"] ? @"access_denied" : queryString[@"error"];

if (!accessToken) {
NSLog(@"Error: accessToken missing");
[self Cancel:nil];
return NO;
}

if (!jwtToken) {
NSLog(@"Error: jwtToken missing");
[self Cancel:nil];
return NO;
}
if (!error) {
if (!accessToken) {
NSLog(@"Error: accessToken missing");
[self Cancel:nil];
return NO;
}

if (!jwtToken) {
NSLog(@"Error: jwtToken missing");
[self Cancel:nil];
return NO;
}
}

//Notify caller
_block(accessToken, jwtToken);
_block(accessToken, jwtToken, error);

return NO;
}
Expand Down

0 comments on commit 39d121a

Please sign in to comment.