Skip to content

Commit

Permalink
improvements on return values
Browse files Browse the repository at this point in the history
  • Loading branch information
Saimis committed Sep 24, 2020
1 parent badc8fd commit c59294b
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 31 deletions.
22 changes: 8 additions & 14 deletions AppleLogin.m
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ -(id)initWithWindow:(NSWindow *)window {
return self;
}

- (void)initiateLoginProcess:(void (^)(NSDictionary *result))completionHandler errorHandler:(void (^)(NSError *error))errorHandler {
- (void)initiateLoginProcess:(void (^)(NSDictionary<NSString *, NSString *> *result))completionHandler errorHandler:(void (^)(NSError *error))errorHandler {
self.successBlock = completionHandler;
self.errorBlock = errorHandler;

Expand All @@ -31,21 +31,15 @@ - (void)authorizationController:(ASAuthorizationController *)controller didCompl
ASAuthorizationAppleIDCredential *appleIDCredential = [authorization credential];

if(appleIDCredential) {
NSString *idToken = [[NSString alloc]initWithData:appleIDCredential.identityToken encoding:NSUTF8StringEncoding];

NSString *email = [appleIDCredential valueForKey:@"email"] ?: @"";

NSDictionary *fullName = [appleIDCredential valueForKeyPath:@"fullName"] ?: [[NSDictionary alloc] initWithObjectsAndKeys:
@"", @"givenName", @"", @"familyName", nil];

NSString *firstName = [fullName valueForKeyPath:@"givenName"] ?: @"";
NSString *lastName = [fullName valueForKeyPath:@"familyName"] ?: @"";

NSString *idToken = [[NSString alloc] initWithData:appleIDCredential.identityToken encoding:NSUTF8StringEncoding];

NSPersonNameComponents *fullName = appleIDCredential.fullName;
NSDictionary *userDetails = @{
@"userIdentifier": [appleIDCredential user],
@"firstName": firstName, @"lastName": lastName,
@"email" : email, @"identityToken" : idToken
@"firstName": fullName.givenName ?: nil,
@"middleName": fullName.middleName ?: nil,
@"lastName": fullName.familyName ?: nil,
@"email" : appleIDCredential.email,
@"idToken" : idToken,
};

self.successBlock(userDetails);
Expand Down
33 changes: 16 additions & 17 deletions main.mm
Original file line number Diff line number Diff line change
Expand Up @@ -32,28 +32,27 @@
NSDictionary *result = [appleLogin initiateLoginProcess:^(NSDictionary * _Nonnull result) {
Napi::Object obj = Napi::Object::New(env);

if ([result objectForKey:@"identityToken"] != nil) {
obj.Set("idToken", std::string([[result objectForKey:@"identityToken"] UTF8String]));
}

if ([result objectForKey:@"firstName"] != nil) {
obj.Set("firstName", std::string([[result objectForKey:@"firstName"] UTF8String]));
}
if ([result objectForKey:@"lastName"] != nil) {
obj.Set("lastName", std::string([[result objectForKey:@"lastName"] UTF8String]));
}

if ([result objectForKey:@"email"] != nil) {
obj.Set("email", std::string([[result objectForKey:@"email"] UTF8String]));
for (NSString* key in result) {
NSString *value = result[key];
std::string napiKey = std::string([key UTF8String]);
Napi::Value napiValue = Napi::Value::From(env, [[NSString stringWithFormat:@"%@", value] UTF8String]);
obj.Set(napiKey, napiValue);
}

deferred.Resolve(obj);
} errorHandler:^(NSError * _Nonnull error) {
// NSString *nsErr = error.localizedDescription;
NSString *nsErr = [NSString stringWithFormat:@"%@", error];
std::string errMsg = std::string([nsErr UTF8String]);
Napi::Object errorObj = Napi::Object::New(env);

errorObj.Set("code", Napi::Value::From(env, error.code));
errorObj.Set("message", Napi::Value::From(env, [error.description UTF8String]));
for (NSString* key in error.userInfo) {
NSString *value = error.userInfo[key];
std::string napiKey = std::string([key UTF8String]);
Napi::Value napiValue = Napi::Value::From(env, [[NSString stringWithFormat:@"%@", value] UTF8String]);
errorObj.Set(napiKey, napiValue);
}

Napi::Error::New(info.Env(), errMsg).ThrowAsJavaScriptException();
deferred.Reject(errorObj);
}];

return deferred.Promise();
Expand Down

0 comments on commit c59294b

Please sign in to comment.