Skip to content

Commit

Permalink
Add initial sign in with apple
Browse files Browse the repository at this point in the history
  • Loading branch information
jpalumickas committed Sep 23, 2020
0 parents commit 7464ad5
Show file tree
Hide file tree
Showing 8 changed files with 956 additions and 0 deletions.
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
node_modules
*.log
build
6 changes: 6 additions & 0 deletions AppleLogin.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
#import <AuthenticationServices/AuthenticationServices.h>

@interface AppleLogin : NSObject<ASAuthorizationControllerDelegate, ASAuthorizationControllerPresentationContextProviding>
- (instancetype) initWithWindow: (NSWindow*) window;
- (void)initiateLoginProcess;//:(void (^)(NSDictionary *result))completionHandler errorHandler:(void (^)(NSError *error))errorHandler;
@end
76 changes: 76 additions & 0 deletions AppleLogin.m
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
#import "AppleLogin.h"
@interface AppleLogin ()

@property (nonatomic, strong) NSWindow *window;
@end

@implementation AppleLogin
-(id)initWithWindow:(NSWindow *)window
{
NSLog(@"Initializing AppleLogin: %@", window);
if(self = [super init]) {
NSLog(@"Assigning self main");
self.window = window;
}
return self;
}

- (void)initiateLoginProcess {

NSLog(@"PROCESS INITIATED");
// self.successBlock = completionHandler;
// self.errorBlock = errorHandler;

ASAuthorizationAppleIDProvider *appleIDProvider = [[ASAuthorizationAppleIDProvider alloc]init];
NSLog(@"Apple ID provider INITIATED");
ASAuthorizationAppleIDRequest *request = [appleIDProvider createRequest];
NSLog(@"Request INITIATED");
request.requestedScopes = @[ASAuthorizationScopeFullName, ASAuthorizationScopeEmail];

ASAuthorizationController *authorizationController = [[ASAuthorizationController alloc]initWithAuthorizationRequests:@[request]];
NSLog(@"AuthorizationController INITIATED");
authorizationController.delegate = self;
authorizationController.presentationContextProvider = self;
NSLog(@"Calling to perform request");
[authorizationController performRequests];
}

#pragma Authorization Delegates

- (void)authorizationController:(ASAuthorizationController *)controller didCompleteWithAuthorization:(ASAuthorization *)authorization {
NSLog(@"authorizationController called");
ASAuthorizationAppleIDCredential *appleIDCredential = [authorization credential];

if(appleIDCredential) {
NSLog(@"appleIDCredential exists");
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"] ?: @"";


NSDictionary *userDetails = @{@"userIdentifier": [appleIDCredential user], @"firstName": firstName, @"lastName": lastName, @"email" : email, @"identityToken" : idToken};

NSLog(@"VIETA LOGIN - idToken %s firstname %s lasname %s email %s", idToken, firstName, lastName, email);

// self.successBlock(userDetails);
}
}

- (void)authorizationController:(ASAuthorizationController *)controller didCompleteWithError:(NSError *)error {
NSLog(@"----------------------------%@",error);
// self.errorBlock(error);
}

#pragma PresentationAnchorForAuthorizationController Delegate

-(ASPresentationAnchor)presentationAnchorForAuthorizationController:(ASAuthorizationController *)controller {
NSLog(@"----------------------------WINDOW");
return self.window;
}
@end
27 changes: 27 additions & 0 deletions binding.gyp
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
{
"targets": [{
"target_name": "main",
"sources": [ ],
"conditions": [
['OS=="mac"', {
"sources": [
"main.mm",
"AppleLogin.h",
"AppleLogin.m"
],
}]
],
'include_dirs': [
"<!@(node -p \"require('node-addon-api').include\")"
],
'libraries': [],
'dependencies': [
"<!(node -p \"require('node-addon-api').gyp\")"
],
'defines': [ 'NAPI_DISABLE_CPP_EXCEPTIONS' ],
"xcode_settings": {
"OTHER_CPLUSPLUSFLAGS": ["-std=c++14", "-stdlib=libc++", "-mmacosx-version-min=10.15"],
"OTHER_LDFLAGS": ["-framework CoreFoundation -framework AppKit -framework AuthenticationServices"]
}
}]
}
10 changes: 10 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
const appleLogin = require('bindings')('main.node')

const signInWithApple = (mainWindow) => {
console.log('main window', mainWindow);
appleLogin.signInWithApple(mainWindow);
}

module.exports = {
signInWithApple
}
70 changes: 70 additions & 0 deletions main.mm
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
#import <AuthenticationServices/AuthenticationServices.h>
#import "./AppleLogin.h"
#include <napi.h>

Napi::ThreadSafeFunction ts_fn;

/***** HELPERS *****/

static NSWindow *windowFromBuffer(const Napi::Buffer<uint8_t> &buffer) {
NSLog(@"window from buffer 1----------------------------");
auto data = (NSView **)buffer.Data();
NSLog(@"window from buffer 2----------------------------");
auto view = data[0];
NSLog(@"window from buffer 3----------------------------");
NSLog(@"window from buffer 3---------------------------- %@", @(view != nil));
return view.window;
}

Napi::Boolean SignInWithApple(const Napi::CallbackInfo &info) {
Napi::Env env = info.Env();
NSLog(@"VIEW----------------------------");
if (info.Length() < 1) {
Napi::Error::New(info.Env(), "Wrong number of arguments")
.ThrowAsJavaScriptException();
return Napi::Boolean::New(env, false);
}

NSLog(@"VIEW2----------------------------");

NSLog(@"VIEW2----------------------------%@", @(info[0] != nil));
Napi::Buffer<uint8_t> buffer = info[0].As<Napi::Buffer<uint8_t>>();
if (buffer.Length() != 8) {
Napi::Error::New(info.Env(), "Pointer buffer is invalid")
.ThrowAsJavaScriptException();
return Napi::Boolean::New(env, false);
}
NSLog(@"VIEW3----------------------------");
NSLog(@"VIEW3----------------------------%@", @(buffer != nil));

NSWindow *win = windowFromBuffer(buffer);
NSLog(@"VIEW4----------------------------");
NSLog(@"VIEW4----------------------------%@", win);


// Napi::Buffer<void*> winHandle = info[0].As<Napi::Buffer<void*>>();
// NSView *view = *reinterpret_cast<NSView **>(winHandle.Data());

// NSView* view = static_cast<NSView*>(*reinterpret_cast<void **>(winHandle.Data()));

// NSWindow *originalWindow = view.window;


AppleLogin *appleLogin = [[AppleLogin alloc] initWithWindow: win];
[appleLogin initiateLoginProcess];


return Napi::Boolean::New(env, true);
}

// Initializes all functions exposed to JS.
Napi::Object Init(Napi::Env env, Napi::Object exports) {
exports.Set(Napi::String::New(env, "signInWithApple"),
Napi::Function::New(env, SignInWithApple));



return exports;
}

NODE_API_MODULE(main, Init)
42 changes: 42 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
{
"name": "node-mac-sign-in-with-apple",
"version": "0.1.0",
"description": "A native module that allows you to Sign in with Apple in macOS",
"main": "index.js",
"scripts": {
"build": "node-gyp build",
"build:dev": "node-gyp build --debug",
"clean": "node-gyp clean",
"lint": "prettier --check '**/*.js'",
"format": "clang-format -i main.mm && prettier --write '**/*.js'",
"rebuild": "node-gyp rebuild",
"rebuild:dev": "node-gyp rebuild --debug",
"test": "./node_modules/.bin/mocha --reporter spec"
},
"repository": {
"type": "git",
"url": "git+https://github.com/vietaapp/node-mac-sign-in-with-apple.git"
},
"keywords": [
"apple",
"login",
"sign-in",
"macos",
"node",
"electron",
"native"
],
"license": "MIT",
"bugs": {
"url": "https://github.com/codebytere/node-mac-sign-in-with-apple/issues"
},
"homepage": "https://github.com/vietaapp/node-mac-sign-in-with-apple#readme",
"devDependencies": {
"node-gyp": "^7.1.0",
"prettier": "^2.0.4"
},
"dependencies": {
"bindings": "^1.5.0",
"node-addon-api": "^3.0.2"
}
}
Loading

0 comments on commit 7464ad5

Please sign in to comment.