Javascript 在单页应用程序中管理用户身份验证

Javascript 在单页应用程序中管理用户身份验证,javascript,jquery,facebook,angularjs,facebook-graph-api,Javascript,Jquery,Facebook,Angularjs,Facebook Graph Api,我很难理解如何在AngularJS中应用我的场景。 我的应用程序的某些页面必须执行与以下页面类似的操作(所有操作都是客户端的): 通过内部API调用授权用户(例如/user/auth)。如果用户以前使用过Facebook授权,它也会给我FB的access\u令牌 通过Facebook Graph API获取用户为管理员的FB页面 用这些页面填充表格 从某种意义上说,这些页面都是小的一页web应用程序,它们都具有类似的结构(获取用户身份验证,然后从FB或内部API获取数据,并在DOM中执行某些操作

我很难理解如何在AngularJS中应用我的场景。 我的应用程序的某些页面必须执行与以下页面类似的操作(所有操作都是客户端的):

  • 通过内部API调用授权用户(例如
    /user/auth
    )。如果用户以前使用过Facebook授权,它也会给我FB的
    access\u令牌
  • 通过Facebook Graph API获取用户为管理员的FB页面
  • 用这些页面填充表格
  • 从某种意义上说,这些页面都是小的一页web应用程序,它们都具有类似的结构(获取用户身份验证,然后从FB或内部API获取数据,并在DOM中执行某些操作)

    假设我有一个控制器,比如:

    <ul ng-controller='PagesController'>
        <li ng-repeat='p in pages'> ... </li>
    </ul>
    
    这不是优雅的,不是模块化的,我必须使用一些Javascript魔法访问
    PagesController
    的作用域:

    function getThePages() { 
        var d = $.Deferred();
        // etc etc, make call to graph and resolve the deferred if ok and put
        // the result in MyApp.pages
        return d.promise();
    }
    
    function populateTable() {
        MyApp.applyToScope("#pages","pages",MyApp.pages.data);
    );
    
    // definition of MyApp.applytoScope()....
    function(controllerElement, model, value) {
        var scope = Splashmood.angular.getScope(controllerElement);
        scope.$apply(function ($scope) {$scope[model] = value;});
    }
    
    我就是不知道如何使用服务(工厂)在AngularJS中移植这些功能

    我的想法是创造类似的东西:

    app.factory("UserAuthFactory", function ($http, $q) {
        // do something
    });
    
    app.controller("MainController", function ($scope, $http, UserAuthFactory) {
        // do something
    });
    
    app.factory("FbUserPages", function ($http, $q) {
        var endpoint = "me/accounts";
        var dataFactory = {};
    
        dataFactory.getPages = function() {
            return ....; // $http calling Facebook Graph API
        }
        return dataFactory;
    });
    
    app.controller('MyFbPagesController', function ($scope, $http, FbUserPages) {
    
        var handleSuccess = function (data, status) {
            $scope.pages = data;
        }
    
        $scope.pages = [];
        FbUserPages.getPages().success(handleSuccess);
    });
    
    但我不知道如何使进程同步(仅在用户授权的情况下运行此控制器和其他控制器)

    app.factory("UserAuthFactory", function ($http, $q) {
        // do something
    });
    
    app.controller("MainController", function ($scope, $http, UserAuthFactory) {
        // do something
    });
    
    app.factory("FbUserPages", function ($http, $q) {
        var endpoint = "me/accounts";
        var dataFactory = {};
    
        dataFactory.getPages = function() {
            return ....; // $http calling Facebook Graph API
        }
        return dataFactory;
    });
    
    app.controller('MyFbPagesController', function ($scope, $http, FbUserPages) {
    
        var handleSuccess = function (data, status) {
            $scope.pages = data;
        }
    
        $scope.pages = [];
        FbUserPages.getPages().success(handleSuccess);
    });