如何使用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
时,什么也没发生。
我该如何解决这个问题? 或者是不可能的?
答案 0 :(得分:3)
这是针对网络的Firebase Twitter集成。尽管它的祖先和它使用JavaScript,React Native绝不是网络;你没有DOM,你没有浏览器,所以你没有能力重定向当前窗口,这似乎是这段代码试图做的。
因此,要回答您的问题,将无法使用此库。您可能会发现必须在Obj-C中编写扩展来执行您想要执行的操作而不使用浏览器样式的流程。
答案 1 :(得分: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