如何在 React Native 中使用 Firebase Twitter 身份验证?
How to use Firebase Twitter Authentication with React Native?
如何在 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
时,没有任何反应。
我该如何解决这个问题?
还是不可能?
这是 网络 的 Firebase Twitter 集成。尽管 React Native 的起源和使用 JavaScript,但它绝不是网络;您没有 DOM,您没有浏览器,因此您无法重定向当前的 window,这似乎是这段代码试图做的。
因此,为了回答您的问题,按原样使用此库是不可能的。您可能会发现您必须在 Obj-C 中编写扩展才能在不使用浏览器样式流程的情况下执行您想执行的操作。
我想出了一个解决方案...我确信有一种更简洁的方法可以用来完成工作,但您可以在我已经完成的基础上再接再厉
/**
* 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
如何在 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
时,没有任何反应。
我该如何解决这个问题? 还是不可能?
这是 网络 的 Firebase Twitter 集成。尽管 React Native 的起源和使用 JavaScript,但它绝不是网络;您没有 DOM,您没有浏览器,因此您无法重定向当前的 window,这似乎是这段代码试图做的。
因此,为了回答您的问题,按原样使用此库是不可能的。您可能会发现您必须在 Obj-C 中编写扩展才能在不使用浏览器样式流程的情况下执行您想执行的操作。
我想出了一个解决方案...我确信有一种更简洁的方法可以用来完成工作,但您可以在我已经完成的基础上再接再厉
/**
* 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