Last active
December 10, 2015 12:08
-
-
Save joshtwist/4431724 to your computer and use it in GitHub Desktop.
Objective C Category to add custom identity to Mobile Services.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#import <WindowsAzureMobileServices/WindowsAzureMobileServices.h> | |
@interface MSClient (CustomId) <MSFilter> | |
- (void) registerUsername:(NSString *) username withPassword: (NSString *) password withCompletion: (MSItemBlock) completion; | |
- (void) loginUsername:(NSString *) username withPassword: (NSString *) password completion: (MSClientLoginBlock) completion; | |
@end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#import "MSClient+CustomId.h" | |
@implementation MSClient (CustomId) | |
- (void)registerUsername:(NSString *)username withPassword:(NSString *)password withCompletion:(MSItemBlock)completion | |
{ | |
MSTable *accounts = [self tableWithName:@"accounts"]; | |
NSDictionary *account = @{ | |
@"username" : username, | |
@"password" : password | |
// you should include and collect any additional data here | |
}; | |
[accounts insert:account completion:completion]; | |
} | |
- (void)loginUsername:(NSString *)username withPassword:(NSString *)password completion:(MSClientLoginBlock)completion | |
{ | |
MSClient *loginClient = [self clientWithFilter:self]; | |
MSTable *accounts = [loginClient tableWithName:@"accounts"]; | |
NSDictionary *credentials = @{ | |
@"username" : username, | |
@"password" : password | |
// you should include and collect any additional data here | |
}; | |
[accounts insert:credentials completion:^(NSDictionary *item, NSError *error) { | |
if (error) { | |
completion(nil, error); | |
} | |
else { | |
MSUser *user = [[MSUser alloc] initWithUserId: [[item valueForKey:@"user"] valueForKey:@"userId"]]; | |
user.mobileServiceAuthenticationToken = [item valueForKey:@"token"]; | |
self.currentUser = user; | |
completion(user, error); | |
} | |
}]; | |
} | |
// This filter simply augments the outgoing URL to add a parameter login=true | |
// This will be much easier in the next release of the iOS SDK which support | |
// specifying parameters on insert | |
- (void) handleRequest:(NSURLRequest *)request | |
onNext:(MSFilterNextBlock)onNext | |
onResponse:(MSFilterResponseBlock)onResponse | |
{ | |
// just add a parameter on the outbound request | |
NSMutableURLRequest *req = [request mutableCopy]; | |
NSURL *newUrl; | |
if ([req.URL.absoluteString rangeOfString:@"?"].location != NSNotFound) { | |
newUrl = [NSURL URLWithString:[NSString stringWithFormat:@"%@%@", req.URL.absoluteString, @"&login=true"]]; | |
} | |
else { | |
newUrl = [NSURL URLWithString:[NSString stringWithFormat:@"%@%@", req.URL.absoluteString, @"?login=true"]]; | |
} | |
req.URL = newUrl; | |
onNext(req, onResponse); | |
} | |
@end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment