-
Notifications
You must be signed in to change notification settings - Fork 4
/
main.mm
89 lines (66 loc) · 2.6 KB
/
main.mm
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
#import <AuthenticationServices/AuthenticationServices.h>
#import "./AppleLogin.h"
#include <map>
#include <napi.h>
using namespace Napi;
ThreadSafeFunction tsfn;
static NSWindow *windowFromBuffer(const Napi::Buffer<uint8_t> &buffer) {
auto data = (NSView **)buffer.Data();
auto view = data[0];
return view.window;
}
Value SignInWithApple( const CallbackInfo& info )
{
Napi::Env env = info.Env();
if (info.Length() != 2) {
Error::New(info.Env(), "Wrong number of arguments")
.ThrowAsJavaScriptException();
}
tsfn = ThreadSafeFunction::New(env, info[0].As<Function>(),
"Thread Safe Function", 0, 1, [](Napi::Env) {});
Napi::Buffer<uint8_t> buffer = info[1].As<Napi::Buffer<uint8_t>>();
NSWindow *win = windowFromBuffer(buffer);
AppleLogin *appleLogin = [[AppleLogin alloc] initWithWindow:win];
auto callback = [](Napi::Env env, Function jsCallback, std::map<std::string, std::string> *values) {
Napi::Object obj = Napi::Object::New(env);
for (auto const& v : (*values)) {
obj.Set(v.first, Napi::Value::From(env, v.second));
}
jsCallback.Call({Value::From(env, obj)});
delete values;
};
[appleLogin initiateLoginProcess:^(NSDictionary * _Nonnull result) {
std::map<std::string, std::string> *values = new std::map<std::string, std::string>();
(*values)["is_error"] = "false";
for (NSString* key in result) {
NSString *value = result[key];
if (value != nil && [value length] > 0) {
std::string stringKey = std::string([key UTF8String]);
std::string stringValue = std::string([[NSString stringWithFormat:@"%@", value] UTF8String]);
(*values)[stringKey] = stringValue;
}
}
napi_status status = tsfn.BlockingCall(values, callback);
if (status != napi_ok) {
NSLog(@"Error occured when running callback on the event loop.\n");
}
tsfn.Release();
} errorHandler:^(NSError * _Nonnull error) {
std::map<std::string, std::string> *values = new std::map<std::string, std::string>();
(*values)["is_error"] = "true";
(*values)["code"] = std::string([[@(error.code) stringValue] UTF8String]);
(*values)["message"] = std::string([error.description UTF8String]);
napi_status status = tsfn.BlockingCall(values, callback);
if (status != napi_ok) {
NSLog(@"Error occured when running callback on the event loop.\n");
}
tsfn.Release();
}];
return Napi::Boolean::New(env, true);
}
Object Init(Env env, Object exports) {
exports.Set(String::New(env, "signInWithApple"),
Function::New(env, SignInWithApple));
return exports;
}
NODE_API_MODULE(main, Init)