/** * Custom js file. */ jQuery( document ).ready( function(){ jQuery( '#user_login' ).attr( 'placeholder', 'Username' ); jQuery( '#lostpasswordform #user_login' ).attr( 'placeholder', 'Username Or Email' ); jQuery( '#user_pass' ).attr( 'placeholder', 'Password' ); /** * Apple Sign In functionality. * This function will initialize the `AppleID.auth` object with parameter we pass in. */ window.AppleID.auth.init({ clientId: "com.pmc.goldDerby.AppleSignInService", // This is the service ID we created on developer.apple.com in certificate section. scope: "name email", // To tell apple we want the user name and emails fields in the response it sends us. redirectURI: "https://www.goldderby.com/apple-sign-in/", // As registered along with our service ID state: "origin:web", // Any string of your choice that you may use for some logic. It's optional and you may omit it. usePopup: true, // Important if we want to capture the data apple sends on the client side. }); //Listen for authorization failures document.addEventListener('AppleIDSignInOnFailure', (error) => { //handle error. console.log(error); // Logs output to dev tools console. }); //Listen for authorization success document.addEventListener('AppleIDSignInOnSuccess', (data) => { var id_token = ''; var identifier = ''; var email = ''; var firstName = ''; var lastName = ''; // Check id_token console.log( "Data=>", data ); if ( typeof data.detail === "undefined" || typeof data.detail.authorization === "undefined" || typeof data.detail.authorization.id_token === "undefined" ) { // To Do: display error return; } // Get identifier. (decode jwt token). id_token = data.detail.authorization.id_token; var output = JSON.parse(atob(id_token.split('.')[1])); if ( typeof output.sub !== "undefined" ) { identifier = output.sub; } // For first time users, we'll have data.detail.user available. if ( typeof data.detail.user !== "undefined" ) { if ( typeof data.detail.user.email !== "undefined" ) { email = data.detail.user.email; } if ( typeof data.detail.user.firstName !== "undefined" ) { firstName = data.detail.user.name.firstName; } if ( typeof data.detail.user.lastName !== "undefined" ) { lastName = data.detail.user.name.lastName; } } // if email does not exist (for returning users), get from jwt token. if ( '' === email ) { if ( typeof output.email !== "undefined" ) { email = output.email; } } var nonce = jQuery('#apple-sign-in-nonce').val(); if ( '' !== identifier ) { jQuery.ajax( { method: "POST", url: '/wp-json/gameplay/v1/apple_sign_in/', data: { user_identifier:identifier, email:email, firstName:firstName, lastName:lastName }, beforeSend: function (xhr) { xhr.setRequestHeader('X-WP-Nonce', nonce); }, success: function (response) { console.log( "success=>", response ); if ( 'success' === response.message && typeof response.user_info !== "undefined" && response.user_info.error !== true ) { // Success window.location = '/leagues/'; } else { // Error alert("There was an error while processing your request, please try again!"); } }, fail: function (response) { // Error alert("There was an error while processing your request, please try again!"); } } ); } }); } );