8

如何在 React Native 中使用 Firebase Twitter 身份验证?

我参考https://www.firebase.com/docs/web/guide/login/twitter.html尝试了以下两个代码

var Firebase = require("firebase");

var App = React.createClass({

  render: function() {
    return (
      <View>
        <Text onPress={this._handlePress}>
          Twitter login
        </Text>
      </View>
    );
  },

  _handlePress: function () {
    var myApp = new Firebase("https://<myapp>.firebaseio.com");

    myApp.authWithOAuthRedirect("twitter", function(error) {
      if (error) {
        console.log("Login Failed!", error);
      } else {
        // We'll never get here, as the page will redirect on success.
      }
    });

  }

});

var Firebase = require("firebase");

var App = React.createClass({

  render: function() {
    return (
      <View>
        <Text onPress={this._handlePress}>
          Twitter login
        </Text>
      </View>
    );
  },

  _handlePress: function () {
    var myApp = new Firebase("https://<myapp>.firebaseio.com");

    myApp.authWithOAuthPopup("twitter", function(error, authData) {
      if (error) {
        console.log("Login Failed!", error);
      } else {
        console.log("Authenticated successfully with payload:", authData);
      }
    });

  }

});

当我使用authWithOAuthRedirect时,发生了一个错误,例如

undefined is not an object (evaluating 'window.location.href')

当我使用authWithOAuthPopup时,什么也没发生。

我该如何解决这个问题?还是不可能?

4

2 回答 2

3

这是Web的 Firebase Twitter 集成。尽管它的祖先和使用 JavaScript,React Native 绝不是网络。你没有 DOM,你没有浏览器,所以你没有能力重定向当前窗口,这似乎是这段代码试图做的事情。

因此,要回答您的问题,使用这个库是不可能的。您可能会发现您必须在 Obj-C 中编写一个扩展程序才能在不使用浏览器样式流程的情况下完成您想做的事情。

于 2015-06-12T09:49:38.537 回答
1

我拼凑了一个解决方案......我确信有一种更清洁的方法可以用来完成工作,但你可以在我已经完成的基础上再接再厉

/**
* login in the user with the credentials, gets the whole process 
* started, [NOTE] probably can just construct the url myself?
*/
_doGitHubLogin() {
    this.props.fbRef.authWithOAuthRedirect("github", function (error) {
        if (error) {
            console.log("Authentication Failed!", error);
        } else {
            console.log("Authenticated successfully with payload:", authData);
        }
    });
}

componentDidMount() {

    // set this interval to check for me trying to redirect the window...
    var that = this
    that.inter = setInterval(function () {
        if (window.location && window.location.split) {

            // set the redirect on the url..
            var newLocation = window.location.split("redirectTo=null")[0]
            newLocation = newLocation + "redirectTo=https://auth.firebase.com/v2/clearlyinnovative-firebasestarterapp/auth/github/callback"

            // open the browser...
            that.setState({ url: newLocation })

            // clear the interval to stop waiting for the location to be set..
            clearInterval(that.inter)
        }
    }, 3000);


    this.props.fbRef.onAuth((_auth) => {
        console.log(_auth)
        this.setState({ auth: _auth })
    });
}

在此处查看完整说明... https://github.com/aaronksaunders/rn-firebase-demo

于 2016-05-17T04:02:28.297 回答