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

时间:2016-05-18 05:39:29

标签: javascript angularjs oauth ionic-framework firebase

我正在使用ionicFirebase让Facebook和Google登录。我有2个服务FirebaseService.jsAppService.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);

AppService.js

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);

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 is not a function语句AppService.js this.authDataAvailable();函数下的this.authenticateWithFacebook错误导致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) }); } 错误。

请帮助我了解实现这些方案的有效方法或最佳做法。

更新

所以我在mJunaidSalaat的帮助下遇到了两件有助于解决问题的事情。

  1. 在我的AppService.js中使用以下内容

    this
  2. 在任何机箱内使用service的标识符,它会更改上下文。在我的案例中使用了@pyqtSlot(int, int, result=QModelIndex) @pyqtSlot(int, int, QModelIndex, result=QModelIndex) def index(self, row, column, parent=QModelIndex()): return super().index(row, column, parent) @pyqtSlot(QModelIndex, int, result=QVariant) def data(self, index, role=Qt.DisplayRole): return super().data(index, role)

1 个答案:

答案 0 :(得分:0)

FirebaseService.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)
          });
    }

希望它有所帮助。