Cordova Firebase如何通过本机facebook应用程序验证用户身份
firebase身份验证api使用浏览器弹出窗口(新api中的firebase.authWithOAuthPopup()。然而,在手机上,大多数人使用原生的facebook应用程序。对于cordova手机应用程序,通过fb本机应用程序进行身份验证具有不需要用户重新输入facebook用户名和密码的优势 如何使用firebase api实现fb本机应用程序身份验证Cordova Firebase如何通过本机facebook应用程序验证用户身份,cordova,firebase,firebase-security,Cordova,Firebase,Firebase Security,firebase身份验证api使用浏览器弹出窗口(新api中的firebase.authWithOAuthPopup()。然而,在手机上,大多数人使用原生的facebook应用程序。对于cordova手机应用程序,通过fb本机应用程序进行身份验证具有不需要用户重新输入facebook用户名和密码的优势 如何使用firebase api实现fb本机应用程序身份验证 如果firebase本身不支持fb本机应用程序身份验证,是否可以将firebase与结合使用,后者似乎支持本机fb应用程序身份验证。如
如果firebase本身不支持fb本机应用程序身份验证,是否可以将firebase与结合使用,后者似乎支持本机fb应用程序身份验证。如何做到这一点?方法authWithOAuthPopup()不支持本机身份验证流,但是,使用Firebase引用的方法,您可以使用Cordova Facebook插件返回的OAuth令牌登录Firebase 下面是一个例子:
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);
});
var dataRef=new Firebase('https://.firebaseio.com');
facebookConnectPlugin.login(['public_info'],函数(状态){
facebookConnectPlugin.getAccessToken(函数(令牌){
//使用现有OAuth 2.0访问令牌向Facebook进行身份验证
dataRef.authWithOAuthToken(“facebook”,标记,函数(错误,authData){
如果(错误){
console.log('Firebase登录失败!',错误);
}否则{
log('使用有效负载成功验证',authData);
}
});
},函数(错误){
console.log('无法获取访问令牌',错误);
});
},函数(错误){
log('登录用户时出错',错误);
});
请注意,Firebase 3略有变化。使用:
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.
});
}
});
var user={};
变量配置={
apiKey:“感谢您的回复和提供示例代码!