Javascript 如何在收到OAuth响应时有效地获得通知?
我正在使用with让Facebook和Google登录。我有两个服务Javascript 如何在收到OAuth响应时有效地获得通知?,javascript,angularjs,oauth,ionic-framework,firebase,Javascript,Angularjs,Oauth,Ionic Framework,Firebase,我正在使用with让Facebook和Google登录。我有两个服务firebaservice.js,AppService.js和一个控制器dashboard.js。我需要在这个控制器中获得OAuth响应 最好的方法是什么?我在其他几个地方也遇到过这种情况,所以我正在寻找一种有效的方法 我在其他地方使用的方法是使用通知程序(当数据可用时调用通知程序)和侦听器 /** notify for Auth data response */ this.authDataAvailable = functio
firebaservice.js
,AppService.js
和一个控制器dashboard.js
。我需要在这个控制器中获得OAuth响应
最好的方法是什么?我在其他几个地方也遇到过这种情况,所以我正在寻找一种有效的方法
我在其他地方使用的方法是使用通知程序(当数据可用时调用通知程序)和侦听器
/** notify for Auth data response */
this.authDataAvailable = function() {
$rootScope.$emit("auth-data-available");
}
/** subscribe to this listener when the aggregated data response is available */
this.authDataAvailableListener = function(scope, callback, isDestroy) {
var handler = $rootScope.$on("auth-data-available", callback);
if (isDestroy)
scope.$on("$destroy", handler);
}
/** subscribing to the above listener */
$scope.authDataResponseListener($scope, function responseAvailable() {
//auth response available at this point
}, true);
FirebaseService.js
function FirebaseService($q) {
this.appRef;
this.authData; //.provider: ["facebook"||"google"], facebook.displayName || google.displayName, facebook.id || google.id, facebook.profileImageURL || google.profileImageURL
this.getRef = function() {
return this.appRef;
}
this.authWithOAuthPopup = function(type) {
var deferred = $q.defer();
this.appRef = new Firebase("https://lola.firebaseio.com");
this.appRef.authWithOAuthPopup(type, function(error, authData) {
if (error) {
this.authError = error;
switch (error.code) {
case "INVALID_EMAIL":
console.log("The specified user account email is invalid.");
break;
case "INVALID_PASSWORD":
console.log("The specified user account password is incorrect.");
break;
case "INVALID_USER":
console.log("The specified user account does not exist.");
break;
default:
console.log("Error logging user in:", error);
}
deferred.resolve(this.authError);
} else {
this.authData = authData;
console.log("Authenticated successfully with payload:", authData);
deferred.resolve(this.authData);
}
});
deferred.promise;
}
/**
* Write or replace data to a defined path, like messages/users/<username>
*/
this.set = function(child, obj) {
var childRef = this.appRef.child(child);
childRef.set(obj);
}
return {
getRef: this.getRef,
authWithOAuthPopup: this.authWithOAuthPopup
}
}
angular.module("starter.services", []).service('FirebaseService', FirebaseService);
function AppService(FirebaseService, $rootScope) {
this.authData; //.provider: ["facebook"||"google"], facebook.displayName || google.displayName, facebook.id || google.id, facebook.profileImageURL || google.profileImageURL
/** notify for Auth data response */
this.authDataAvailable = function() {
$rootScope.$emit("auth-data-available");
}
/** subscribe to this listener when the aggregated data response is available */
this.authDataAvailableListener = function() {
var handler = $rootScope.$on("auth-data-response", callback);
if (isDestroy)
scope.$on("$destroy", handler);
}
this.authenticateWithGoogle = function() {
this.authData = FirebaseService.authWithOAuthPopup("google");
this.authDataAvailable();
console.log(this.authData.google.displayName);
}
this.authenticateWithFacebook = function() {
this.authData = FirebaseService.authWithOAuthPopup("facebook");
this.authDataAvailable();
console.log(this.authData.facebook.displayName);
}
this.getAuthData = function() {
return this.authData;
}
return {
authenticateWithGoogle: this.authenticateWithGoogle,
authenticateWithFacebook: this.authenticateWithFacebook,
getAuthData: this.getAuthData
}
}
angular.module('starter.services').service('AppService', AppService);
this.authenticateWithFacebook = function() {
FirebaseService.authWithOAuthPopup("facebook")
.then(function(data) {
service.authData = data;
console.log(this.authData);
service.authDataAvailable();
console.log(this.authData.facebook.displayName);
}, function(err) {
console.log(err)
});
}
dashboard.js
function DashCtrl($scope, AppService) {
$scope.user = "";
$scope.openBrowser = function() {
AppService.authenticateWithFacebook();
/*var authData = AppService.getAuthData();
$scope.user = authData.facebook.displayName;
console.log(authData);
console.log($scope.user);*/
}
}
angular.module("starter.controllers", []).controller('DashCtrl', DashCtrl);
在我的方法中,我得到this.authDataAvailable不是AppService.js
语句this.authDataAvailable()中的函数
错误代码>在下。使用Facebook
功能进行身份验证
请帮助我了解实现此类场景的有效方法或最佳实践
更新
因此,在mJunaidSalaat的帮助下,我遇到了两件帮助我解决问题的事情
在my AppService.js中使用如下内容
function FirebaseService($q) {
this.appRef;
this.authData; //.provider: ["facebook"||"google"], facebook.displayName || google.displayName, facebook.id || google.id, facebook.profileImageURL || google.profileImageURL
this.getRef = function() {
return this.appRef;
}
this.authWithOAuthPopup = function(type) {
var deferred = $q.defer();
this.appRef = new Firebase("https://lola.firebaseio.com");
this.appRef.authWithOAuthPopup(type, function(error, authData) {
if (error) {
this.authError = error;
switch (error.code) {
case "INVALID_EMAIL":
console.log("The specified user account email is invalid.");
break;
case "INVALID_PASSWORD":
console.log("The specified user account password is incorrect.");
break;
case "INVALID_USER":
console.log("The specified user account does not exist.");
break;
default:
console.log("Error logging user in:", error);
}
deferred.resolve(this.authError);
} else {
this.authData = authData;
console.log("Authenticated successfully with payload:", authData);
deferred.resolve(this.authData);
}
});
deferred.promise;
}
/**
* Write or replace data to a defined path, like messages/users/<username>
*/
this.set = function(child, obj) {
var childRef = this.appRef.child(child);
childRef.set(obj);
}
return {
getRef: this.getRef,
authWithOAuthPopup: this.authWithOAuthPopup
}
}
angular.module("starter.services", []).service('FirebaseService', FirebaseService);
function AppService(FirebaseService, $rootScope) {
this.authData; //.provider: ["facebook"||"google"], facebook.displayName || google.displayName, facebook.id || google.id, facebook.profileImageURL || google.profileImageURL
/** notify for Auth data response */
this.authDataAvailable = function() {
$rootScope.$emit("auth-data-available");
}
/** subscribe to this listener when the aggregated data response is available */
this.authDataAvailableListener = function() {
var handler = $rootScope.$on("auth-data-response", callback);
if (isDestroy)
scope.$on("$destroy", handler);
}
this.authenticateWithGoogle = function() {
this.authData = FirebaseService.authWithOAuthPopup("google");
this.authDataAvailable();
console.log(this.authData.google.displayName);
}
this.authenticateWithFacebook = function() {
this.authData = FirebaseService.authWithOAuthPopup("facebook");
this.authDataAvailable();
console.log(this.authData.facebook.displayName);
}
this.getAuthData = function() {
return this.authData;
}
return {
authenticateWithGoogle: this.authenticateWithGoogle,
authenticateWithFacebook: this.authenticateWithFacebook,
getAuthData: this.getAuthData
}
}
angular.module('starter.services').service('AppService', AppService);
this.authenticateWithFacebook = function() {
FirebaseService.authWithOAuthPopup("facebook")
.then(function(data) {
service.authData = data;
console.log(this.authData);
service.authDataAvailable();
console.log(this.authData.facebook.displayName);
}, function(err) {
console.log(err)
});
}
使用此
的标识符,就像在任何附件中一样,它会更改上下文。所以在我的例子中使用了service
在firebaservice.js
中,回调函数不返回接收到的数据。尝试添加
this.authWithOAuthPopup = function(type) {
var deferred = $q.defer();
this.appRef = new Firebase("https://lola.firebaseio.com");
this.appRef.authWithOAuthPopup(type, function(error, authData) {
if (error) {
this.authError = error;
switch (error.code) {
case "INVALID_EMAIL":
console.log("The specified user account email is invalid.");
break;
case "INVALID_PASSWORD":
console.log("The specified user account password is incorrect.");
break;
case "INVALID_USER":
console.log("The specified user account does not exist.");
break;
default:
console.log("Error logging user in:", error);
}
deferred.resolve(this.authError);
} else {
this.authData = authData;
console.log("Authenticated successfully with payload:", authData);
deferred.resolve(this.authData);
}
});
return defered.promise;//this returns the promise rejected or resolve with the supplied data
}
在AppService.js
中,像这样处理承诺
this.authenticateWithFacebook = function() {
var service = this;
FirebaseService.authWithOAuthPopup("facebook")
.then(function(data){
this.authData = data
console.log(this.authData.facebook.displayName);
service.authDataAvailable();
},function(err){
console.log(err)
});
}
希望有帮助。在此.authenticateWithFacebook
功能中,您是否收到此.authData
?我的意思是试着像console.log(this.authData)
@mJunaidSalaat:no我得到console.log(this.authData)的未定义的
在中。使用Facebook验证功能我添加了return deferred.promise代码>在}之后代码>并返回一个空承诺,状态为0。我也在更新我的问题。是的,很抱歉这是我的错误。更正了。Thanks@Ricky告诉我这个。您的console.log(使用有效负载成功验证:,authData)是什么代码>日志?你在这里得到的是authData
对象吗?我得到的验证成功,有效载荷为:object{provider:“facebook”,uid:“facebook:1324428977082662”,facebook:object,token:“eyj0exaioyjkv4qiljbgjbgcioijiuzi1nij9.eyJwcm92aWRlc…sifX0.Re0LR7EzhX1VpqsnM1-z31mjHynEt4cqthZ8DjkGE90”,auth:object…)
但无法在此功能中获取此信息。请使用Facebook
功能进行身份验证。另外ionic.bundle.js:26771 TypeError:this.authDataAvailable不是一个函数?错误ionic.bundle.js:26771 TypeError:this.authDataAvailable不是它后面的函数
?