1

我真的想将 SimpleLogin 身份验证与 Angular js 的解析集成。我尝试执行以下操作:

app.config(function($routeProvider) {
$routeProvider.when("/login", {
    templateUrl: "templates/authentication.html",
    controller: "AuthenticationController",
    resolve: {
        authenticated_user: function($q, $timeout) {
            var deferred = $q.defer();
            var ref = new Firebase("https://theuy-angularfire.firebaseio.com/");

             var auth = new FirebaseSimpleLogin(ref, function(error, user) {
              if (error) {
                deferred.resolve(null);
              } else if (user) {
                console.log('User ID: ' + user.id + ', Provider: ' + user.provider);
                deferred.resolve(user);
              } else {
                deferred.resolve(null);
              }
            });

            return deferred.promise;
        }
    }
});

当我成功登录但我的模板未呈现时,我收到了回调。你能给我一个建议如何使用 Firebase SimpleLogin 身份验证和解析吗?

4

1 回答 1

1

我想知道问题是什么。

Angular 的 Resolve 功能是客户端操作,因此执行速度更快,而 SimpleLogin 必须连接到服务器并等待响应。问题是 Angular 试图在身份验证过程的结果之前解析和呈现模板。

所以我想出了一个解决方案,首先进行身份验证过程,然后手动引导 Angular 并最终运行解析功能。解决方案可能如下所示:

var app = angular.module("myapp", ["firebase"]);
app.user = null;
app.ref = null;
app.auth = null;
// when the dom is ready, so we sure we can bootstrap angular manually
angular.element(document).ready(function () {
    app.ref = new Firebase("https://theuy-angularfire.firebaseio.com/");
    app.auth = new FirebaseSimpleLogin(app.ref, function (error, user) {
        app.user = user;
        angular.bootstrap(document, ['myapp']);
    });
});
// this object is needed when we want to authenticate somewhere in our application with the use of resolve
var authentication = {
    app: function ($q, $location) {
        var deferred = $q.defer();

        // go to the dashboard when the user is logged in
        if (app.user != null) {
            $location.path("/dashboard");
            deferred.resolve(app.user);
        } else {
            // go to the login form when the user is logged logged out, or never had logged in
            $location.path("/login");
            deferred.resolve(null);
        }
        return deferred.promise;
    }
}

app.config(function ($routeProvider) {
    $routeProvider.when("/login", {
        templateUrl: "templates/loginform.html",
        resolve: authentication,
        controller: "LoginController"
    });

    $routeProvider.when("/dashboard", {
        templateUrl: "templates/dashboard.html",
        resolve: authentication,
        controller: "DashboardController"
    });
});
于 2013-11-02T09:04:37.307 回答