tessguefen
8/4/2015 - 5:36 PM

Facebook OAUTH

Facebook OAUTH

<script type="text/javascript" src="//ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js"></script>
<script type="text/javascript">
$( document ).ready( function(){

var return_url = '&mvt:global:secure_sessionurl;&Screen=ACLN&Store_Code=&mvte:store:code;';

if (window.opener.document.URL.indexOf("LOGN") == -1 ){
	var return_url = '&mvt:global:secure_sessionurl;Screen=OCST&Store_Code=&mvte:store:code;';
}

<mvt:if expr = "NOT g.Basket:cust_id">
		$.ajax({
			url:		'&mvt:global:json_url;Function=Module&Module_Function=OAuth_Request&Module_Code=oauth&Store_Code=&mvte:store:code;&Session_Type=runtime',
			type:		'POST',
			dataType:	'json',	
			data:		'Code=facebook&OAuth_URL=https://graph.facebook.com/me&OAuth_Method=GET&Access_Token=&mvta:oauth:facebook;&parameters:fields=id,first_name,last_name,email',
			success: function( response )
			{
				if ( !response.success )
				{
					return alert( response.error_message );
				}

				if ( ( typeof JSON !== 'undefined' ) && ( typeof JSON.parse !== 'undefined' ) )
				{
					data = JSON.parse( response.data.response );
				}else{
					data = eval( '(' + response.data.response + ')' );
				}
                                        
				$.fn.login( data );
			}
		});

		

		$.fn.login = function( data )
		{
			$.ajax( {
				url:		'&mvt:global:json_url;Function=Module&Module_Function=OAuth_Login&Module_Code=oauth&Store_Code=&mvte:store:code;&Session_Type=runtime',
				type:		'POST',
				dataType:	'json',	
				data:		'Code=facebook&Access_Token=&mvta:oauth:facebook;&OAuth_ID=' + data.id,
				success: function( response )
				{
					if ( !response.success )
					{
						if ( response.error_code == 'invalid_customer' )
						{							
							
							var store_name = escape("&mvte:store:name;");
							$('#name').html('Welcome ' + data.first_name + '! Do you already have existing account at ' + unescape(store_name) + '?');
							$('#login-form').show();

							
						} else {
							return alert( response.error_message );
						}
					}else{

						
						window.opener.document.location = return_url;
						
						//change screen
						$('#login-form').hide();
						$('#password-prompt').hide();
						$("#progress-bar").show();

						setTimeout(
						  function() 
						  {
						    window.close();
						  }, 3000);
						
					}
					
				}
			} );
		}

		$.fn.prompt_password = function( data )
		{
			$('#login-form').hide();
			$('#password-prompt').show();
			$('#Change_Customer_LoginEmail').val( data.email );
		}

		$.fn.create = function( data )
		{
			
			//change screen
			$('#login-form').hide();
			$('#password-prompt').hide();
			$("#progress-bar").show();

			//get email address and password from customer
			customer_email = $('#Change_Customer_LoginEmail').val();
			user_supplied_password = $('#Change_Customer_Password').val();
			
			if(customer_email == ""){
				customer_email = data.email;
			}

			$.ajax( {
				url:		'&mvt:global:json_url;Function=Module&Module_Function=OAuthCustomer_Create&Module_Code=oauth&Store_Code=&mvte:store:code;&Session_Type=runtime',
				type:		'POST',
				dataType:	'json',
				data:		'Code=facebook' +
							'&OAuth_ID=' + data.id + 
							'&Access_Token=&mvta:oauth:facebook;' + 
							'&Customer_ShipFirstName=' 	+ data.first_name +
							'&Customer_ShipLastName=' 	+ data.last_name +
							'&Customer_ShipEmail=' 		+ customer_email +
							'&Customer_BillFirstName=' 	+ data.first_name +
							'&Customer_BillLastName=' 	+ data.last_name +
							'&Customer_BillEmail=' 		+ customer_email +
							'&Customer_PasswordEmail=' 	+ customer_email +
							'&Customer_Password=' 		+ user_supplied_password,
				success: function( response )
				{
					if ( !response.success )
					{
						return alert( response.error_message );
					}
					
					
					$.fn.login( data );
				}
			} );
		}

		$.fn.miva_login = function( data )
		{
			var username = $("#Customer_LoginEmail");
			var password = $("#Customer_Password");

			$.ajax( {
				url:		'&mvt:global:secure_sessionurl;Action=LOGN&Store_Code=&mvte:store:code;&Screen=facebook-redirect',
				type:		'POST',
				dataType:	'html',

				data:		'&Customer_LoginEmail=' + username.val() + 
							'&Customer_Password=' + password.val(), 
				success: function( response )
				{
					 if (response.indexOf("LOGN:1") == -1 ){

					 	$("#status-message").html("Incorrect login. Please try again.");
		

					 	username.val("");
					 	password.val("");
					 }else{

					 	window.opener.document.location = return_url;
					 	$("#status-message").html("Login Successful");
					 	$.fn.createAssociation(data.id)

					 }
				}
			} );
		}


		$.fn.createAssociation = function( oauth_id )
		{
			$.ajax( {
				url:		'&mvt:global:json_url;Function=Module&Module_Function=OAuthCustomer_Create_Association&Module_Code=oauth&Store_Code=&mvte:store:code;&Session_Type=runtime',
				type:		'POST',
				dataType:	'json',
				data:		'Code=facebook&Access_Token=&mvta:oauth:facebook;&OAuth_ID=' + oauth_id,
				success: function( response )
				{
					if ( !response.success )
					{
						return alert( response.error_message );
					}
					
					$('#login-form').hide();
					$('#name').hide();
					$('#success-message').html("Linking Facebook to your existing account.")
					$("#progress-bar").show();
						
					setTimeout(
						function() 
						 {
						    window.close();
						 }, 3000);
					
				}
			} );
		}
	<mvt:else>
		window.opener.document.location = return_url;
		window.close();
	</mvt:if>
} );
</script>