2016-07-27 8 views
9

मैं एक है जहाँ मैं देशी फेसबुक लॉगिन लागू एक ईओण एप्लिकेशन काम कर रहा हूँ के साथ MySQL डाटाबेस के लिए फेसबुक उपयोगकर्ता डेटा को बचाने के लिए (इस ट्यूटोरियल का पालन ->https://ionicthemes.com/tutorials/about/native-facebook-login-with-ionic-framework)। जैसा कि आप देख सकते हैं कि फेसबुक डेटा अब स्थानीय स्टोरेज में संग्रहीत हो जाता है। मुझे इस डेटा को अपने MySQL डेटाबेस में सहेजने की आवश्यकता है।कैसे कोणीय और आयोनिक

मुझे यह किसी भी मुद्दे के बिना काम करने के लिए मिला। अब मैं फेसबुक उपयोगकर्ता डेटा को अपने MySQL डेटाबेस में संग्रहीत करना चाहता हूं।

असल में मुझे यकीन है कि जहाँ मेरे डेटाबेस या कैसे भी यह बुद्धिमान कोड है करने के लिए के लिए साथ डेटा पास करने के लिए मेरे http अनुरोध जगह नहीं हूँ।

मुझे लगता है कि मैं एक बैकएंड पहले से ही सेटअप (जो बूटस्ट्रैप, एचटीएमएल, सीएसएस, जेएस PHP और MySQL के साथ कोडित है) है उल्लेख करना चाहिए। http://www.xxxxx.com/user.php

मेरी नियंत्रक कोड के भाग:

तो अपने उपयोगकर्ताओं के लिए यूआरएल इस होगा

app.controller('LoginCtrl', function($scope, $state, $q, UserService, $ionicLoading) { 
    // This is the success callback from the login method 
    var fbLoginSuccess = function(response) { 
    if (!response.authResponse){ 
     fbLoginError("Cannot find the authResponse"); 
     return; 
    } 

    var authResponse = response.authResponse; 

    getFacebookProfileInfo(authResponse) 
    .then(function(profileInfo) { 
     // For the purpose of this example I will store user data on local storage 
     UserService.setUser({ 
     authResponse: authResponse, 
       userID: profileInfo.id, 
       name: profileInfo.name, 
       email: profileInfo.email, 
     picture : "http://graph.facebook.com/" + authResponse.userID + "/picture?type=large" 
     }); 
     $ionicLoading.hide(); 
     $state.go('app.dashboard'); 
    }, function(fail){ 
     // Fail get profile info 
     console.log('profile info fail', fail); 
    }); 
    }; 

    // This is the fail callback from the login method 
    var fbLoginError = function(error){ 
    console.log('fbLoginError', error); 
    $ionicLoading.hide(); 
    }; 

    // This method is to get the user profile info from the facebook api 
    var getFacebookProfileInfo = function (authResponse) { 
    var info = $q.defer(); 

    facebookConnectPlugin.api('/me?fields=email,name&access_token=' + authResponse.accessToken, null, 
     function (response) { 
       console.log('logging facebook response',response); 
     info.resolve(response); 
     }, 
     function (response) { 
       console.log(response); 
     info.reject(response); 
     } 
    ); 
    return info.promise; 
    }; 

    //This method is executed when the user press the "Login with facebook" button 
    $scope.facebookSignIn = function() { 
    facebookConnectPlugin.getLoginStatus(function(success){ 
     if(success.status === 'connected'){ 
     // The user is logged in and has authenticated your app, and response.authResponse supplies 
     // the user's ID, a valid access token, a signed request, and the time the access token 
     // and signed request each expire 
     console.log('getLoginStatus', success.status); 

      // Check if we have our user saved 
      var user = UserService.getUser('facebook'); 

      if(!user.userID){ 
        getFacebookProfileInfo(success.authResponse) 
        .then(function(profileInfo) { 
         // For the purpose of this example I will store user data on local storage 
         UserService.setUser({ 
          authResponse: success.authResponse, 
          userID: profileInfo.id, 
          name: profileInfo.name, 
          email: profileInfo.email, 
          picture : "http://graph.facebook.com/" + success.authResponse.userID + "/picture?type=large" 
         }); 
         $state.go('app.dashboard'); 
        }, function(fail){ 
         // Fail get profile info 
         console.log('profile info fail', fail); 
        }); 
       }else{ 
        $state.go('app.dashboard'); 
       } 
     } else { 
     // If (success.status === 'not_authorized') the user is logged in to Facebook, 
       // but has not authenticated your app 
     // Else the person is not logged into Facebook, 
       // so we're not sure if they are logged into this app or not. 

       console.log('getLoginStatus', success.status); 

       $ionicLoading.show({ 
      template: 'Logging in...' 
     }); 

       // Ask the permissions you need. You can learn more about 
       // FB permissions here: https://developers.facebook.com/docs/facebook-login/permissions/v2.4 
     facebookConnectPlugin.login(['email', 'public_profile'], fbLoginSuccess, fbLoginError); 
     } 
    }); 
    }; 
}) 

मेरे service.js कोड (स्थानीय संग्रहण)

angular.module (' Challenger.services ', [])

.service('UserService', function() { 
    // For the purpose of this example I will store user data on ionic local storage but you should save it on a database 
    var setUser = function(user_data) { 
    window.localStorage.starter_facebook_user = JSON.stringify(user_data); 
    }; 

    var getUser = function(){ 
    return JSON.parse(window.localStorage.starter_facebook_user || '{}'); 
    }; 

    return { 
    getUser: getUser, 
    setUser: setUser 
    }; 
}); 
+1

बिल्कुल नहीं। क्योंकि दोनों ढांचे फ्रंटेंड फ्रेमवर्क हैं और क्लाइंट पर चल रहे हैं। मैंने एक जावास्क्रिप्ट mysql ड्राइवर के बारे में सुना है जो आगे बढ़ने में उपयुक्त है, नोडज के लिए संभव है। एक बैकएंड एप्लिकेशन है जो आपके लिए हैडल करता है! –

+0

@ रेने एम, हाँ, मुझे शायद यह उल्लेख करना चाहिए कि मेरे पास बैकएंड पहले से सेटअप है (जो बूटस्ट्रैप, एचटीएमएल, सीएसएस, जेएस PHP और mysql के साथ कोड है)। जेसन एंडपॉइंट्स मैं पहले से ही है। – GY22

+0

बिल्कुल सही है कि आपका रास्ता –

उत्तर

0

मुझे लगता है कि आपके सभी कोड हैं तैयार है, लेकिन यह सुनिश्चित नहीं है कि आपके कोड का पता लगाने के लिए सबसे अच्छी जगह कहां है। एक अच्छा लिंकर है जहां आपके PHP परियोजना संरचना को लेआउट करने के बारे में स्पष्ट निर्देश है: http://davidshariff.com/blog/php-project-structure/, उम्मीद है कि यह एक प्रकार की सहायता दे सकता है।

4

मेरे सिफारिश बस एक JSON ajax रख सकते हैं या जावास्क्रिप्ट से पोस्ट का प्रयोग है। उदाहरण के लिए, example.com के बैकएंड मेजबान संभालने

करने के लिए एक CSP जोड़े आयोनिक एचटीएमएल जैसे:

<meta http-equiv="Content-Security-Policy" content="default-src http://example.com; style-src 'self' 'unsafe-inline'; script-src 'self' 'unsafe-inline' 'unsafe-eval'"> 

whitelist लिए डोमेन जोड़ें Cordova config.xml में:

<access origin="http://example.com" /> 

फिर आप अपने कोणीय नियंत्रक में AJAX के साथ जावास्क्रिप्ट से PHP को कॉल कर सकते हैं (मैंने यहां jQuery का उपयोग किया है लेकिन आप किसी भी जावास्क्रिप्ट AJAX लाइब्रेरी का उपयोग कर सकते हैं):

var data = { 
     authResponse: authResponse, 
       userID: profileInfo.id, 
       name: profileInfo.name, 
       email: profileInfo.email, 
     picture : "http://graph.facebook.com/" + authResponse.userID + "/picture?type=large" 
     }; 

$.post("http://example.com/login.php", data, function(returnData, status) { 
    console.log('PHP returned HTTP status code', status); 
}); 

अंत में, पीएचपी तरफ — उदा login.php — पहुँच पद डेटा $_POST['userId'], $_POST['email'] साथ, आदि

+0

मिमीएम में इसे कैसे कार्यान्वित किया जाए/मैं आपकी व्याख्या के साथ-साथ त्रुटियां प्राप्त करने का प्रयास कर रहा हूं। https://gyazo.com/aaa1044216b82a8bd251dda3821e2241 – GY22

+0

ऐसा लगता है कि त्रुटियों हो रही है क्योंकि इस तरह फ़ॉन्ट भयानक और फेसबुक के रूप में एप्लिकेशन द्वारा उपयोग किए गए सभी बाहरी डोमेन अनुमति दी गई है नहीं - http: // stackoverflow।कॉम/प्रश्न/31256348/सामग्री-सुरक्षा-नीति-त्रुटि-लेकिन-मेटा-टैग-इन-यूआरएल और http://stackoverflow.com/questions/38766985/http-equiv-content-security-policy-works-in- ब्राउज़र-लेकिन-ऑन-ऑन-एंड्रॉइड-डिवाइस/38771277 – Lightbeard

+0

भी मुझे यकीन नहीं है कि AJAX कॉल कहां रखना है ... – GY22

संबंधित मुद्दे