Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/383.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 如何在收到OAuth响应时有效地获得通知?_Javascript_Angularjs_Oauth_Ionic Framework_Firebase - Fatal编程技术网

Javascript 如何在收到OAuth响应时有效地获得通知?

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

我正在使用with让Facebook和Google登录。我有两个服务
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不是它后面的函数