2014-10-14 21 views
8

api xác thực Firebase sử dụng trình duyệt bật lên (Firebase.authWithOAuthPopup() trong api cordova example mới). Tuy nhiên, trên điện thoại di động, hầu hết mọi người sử dụng ứng dụng facebook gốc thay thế. cho các ứng dụng điện thoại cordova, xác thực thông qua ứng dụng gốc fb có lợi thế là không yêu cầu người dùng phải nhập lại tên người dùng và mật khẩu facebook.Firebase cách xác thực người dùng thông qua ứng dụng facebook gốc

Làm thế nào để đạt được xác thực ứng dụng gốc fb với api firebase?

Nếu Firebase vốn không hỗ trợ xác thực ứng dụng gốc fb, có thể sử dụng Firebase kết hợp với cordova facebook plugin, xuất hiện để hỗ trợ xác thực ứng dụng gốc fb. Làm thế nào điều này có thể được thực hiện?

Trả lời

22

Phương thức authWithOAuthPopup() không hỗ trợ quy trình xác thực gốc, tuy nhiên, sử dụng phương thức authWithOAuthToken() của tham chiếu Firebase, bạn có thể sử dụng mã thông báo OAuth mà plugin Cordova Facebook trả về để đăng nhập vào Firebase.

Dưới đây là một ví dụ:

var dataRef = new Firebase('https://<your-firebase>.firebaseio.com'); 

facebookConnectPlugin.login(['public_info'], function(status) { 
    facebookConnectPlugin.getAccessToken(function(token) { 
    // Authenticate with Facebook using an existing OAuth 2.0 access token 
    dataRef.authWithOAuthToken("facebook", token, function(error, authData) { 
     if (error) { 
     console.log('Firebase login failed!', error); 
     } else { 
     console.log('Authenticated successfully with payload:', authData); 
     } 
    }); 
    }, function(error) { 
    console.log('Could not get access token', error); 
    }); 
}, function(error) { 
    console.log('An error occurred logging the user in', error); 
}); 
+0

Cảm ơn bạn đã trả lời và cung cấp mã ví dụ! – Jarnal

2

Chỉ cần một lưu ý rằng căn cứ hỏa lực 3 đã thay đổi một chút. Sử dụng:

var user = {}; 
    var config = { 
     apiKey: "<Your API Key", 
     authDomain: "<yourapp>.firebaseapp.com", 
     databaseURL: "https://<yourapp>.firebaseio.com", 
     storageBucket: "<yourapp>.appspot.com" 
    }; 
    firebase.initializeApp(config); 

    // Sign in with Token obtained from facebookConnectPlugin.getAccessToken 

    var credential = firebase.auth.FacebookAuthProvider.credential(token); 

    firebase.auth().signInWithCredential(credential).then(function(result) { 
     // The firebase.User instance: 
     user = result; 
     console.log('User :'+JSON.stringify(user)); 
     //Can now user the 'user' here 
    }, function(error) { 
     // Check error.code and error.message 
     // Possible error is auth/account-exists-with-different-credential to fetch the providers ??? 
     // In case of auth/account-exists-with-different-credential error, 
     // you can fetch the providers using this: 
     if (error.code === 'auth/account-exists-with-different-credential') { 
      firebase.auth().fetchProvidersForEmail(error.email).then(function(providers) { 
       // The returned 'providers' is a list of the available providers 
       // linked to the email address. Please refer to the guide for a more 
       // complete explanation on how to recover from this error. 
      }); 
     } 
    }); 
Các vấn đề liên quan