Firebase如何通过本机Facebook应用验证用户

时间:2014-10-14 20:17:58

标签: cordova firebase firebase-security

firebase身份验证API使用浏览器弹出窗口(新api cordova example中的Firebase.authWithOAuthPopup())。但是,在手机上,大多数人使用原生的Facebook应用程序。对于Cordova手机应用程序,通过fb本机应用程序进行身份验证的优点是不需要用户重新输入Facebook用户名和密码。

如何使用firebase api实现fb本机应用程序身份验证?

如果firebase本身不支持fb本机应用程序身份验证,是否可以将firebase与cordova facebook plugin结合使用,这似乎支持本机fb app auth。怎么可以这样做?

2 个答案:

答案 0 :(得分:22)

authWithOAuthPopup()方法不支持本机身份验证流,但是,使用Firebase引用的authWithOAuthToken()方法,您可以使用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);
});

答案 1 :(得分:2)

注意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.
            });
        }
    });