diff --git a/assets/scripts/libs/apple.client.js b/assets/scripts/libs/apple.client.js new file mode 100644 index 0000000..ad7fbe5 --- /dev/null +++ b/assets/scripts/libs/apple.client.js @@ -0,0 +1,45 @@ +const scripts = ['https://appleid.cdn-apple.com/appleauth/static/jsapi/appleid/1/en_US/appleid.auth.js']; +const CLIENT_ID = 'wallet.cebggame.com'; +const REDIRECT_URI = location.href; + +// Authorization scopes required by the API; multiple scopes can be included, separated by spaces. +const SCOPES = 'name email'; +export class AppleClient { + initClient() { + let state = Math.random().toString(36).substring(2, 15) + Math.random().toString(36).substring(2, 15); + let nonce = Math.random().toString(36).substring(2, 15) + Math.random().toString(36).substring(2, 15); + AppleID.auth.init({ + clientId : CLIENT_ID, + scope : SCOPES, + redirectURI : REDIRECT_URI, + state, + nonce, + usePopup : true + }); + } + async initAppleClient() { + await loadSingleScript([scripts[0]]); + this.initClient(); + } + async login(funid) { + let result = {}; + try { + const data = await AppleID.auth.signIn({state: funid+''}) + // Handle successful response. + console.log("apple login success: ", JSON.stringify(data)) + result = { + data: data.authorization.id_token, + errcode: 0, + }; + } catch ( error ) { + // Handle error. + console.log('apple login error: ', JSON.stringify(error)) + result = { + errcode: 1, + errmessage: error.error || error.message || error, + } + } + jc.wallet.nativeSvr.handleNativeCallback(funid, JSON.stringify(result)); + } +} + diff --git a/assets/scripts/libs/native_bridge.js b/assets/scripts/libs/native_bridge.js index cb9ecbe..960e9c3 100644 --- a/assets/scripts/libs/native_bridge.js +++ b/assets/scripts/libs/native_bridge.js @@ -11,9 +11,11 @@ import init, { } from '../../wasm/rustwallet.js'; import { QrScanner, showQr } from './qr_reading.js'; import { GoogleClient } from './google.client.js'; +import { AppleClient } from './apple.client.js'; // call native method let googleClient = null; +let appleClient = null; let walletCache = []; async function initWasm() { @@ -21,6 +23,8 @@ async function initWasm() { googleClient = new GoogleClient(); await init(); await googleClient.initGoolgeClient(); + appleClient = new AppleClient(); + await appleClient.initAppleClient(); window.dispatchEvent(new CustomEvent('envready')); } } @@ -61,7 +65,8 @@ window.jsb = { }, // BEGIN:: only for web signWithApple: function (funid) { - callNative({ action: 'signWithApple', funid }); + //callNative({ action: 'signWithApple', funid }); + appleClient.login(funid); }, signWithGoogle: function (funid) { googleClient.login(funid); diff --git a/assets/scripts/run_sample.js b/assets/scripts/run_sample.js index fa766bd..e3e25c5 100644 --- a/assets/scripts/run_sample.js +++ b/assets/scripts/run_sample.js @@ -3,10 +3,10 @@ const pages = { closePage: async function () { callNative({ action: 'closepage' }); }, - //loginWithApple: async function () { - // let res = await callMethod('walletLogin', '1', 'dev'); - // console.log(res); - //}, + loginWithApple: async function () { + let res = await callMethod('walletLogin', '1', 'dev'); + console.log(res); + }, loginWithGoogle: async function () { let res = await callMethod('walletLogin', '0', 'dev'); console.log(res);