我应该如何使 AngularFire/Firebase 中的用户访问令牌无效?

How should I invalidate user access tokens in AngularFire/Firebase?

上下文

使用说明注销用户发现 here and here

我将 AngularFire 与 AngularJS 一起使用,并使用电子邮件和密码方法对用户进行身份验证。

问题

调用 $unauth 不会使用户令牌无效:后续调用 $authWithPassword return 相同的身份验证令牌。

注意:令牌在服务器上设置的到期时间后正确失效。

代码

angular
    .module('app')
    // Auth Factory
    .factory("Auth", ["$firebaseAuth", "FIREBASE_URI",
        function($firebaseAuth, FIREBASE_URI) {
            var ref = new Firebase(FIREBASE_URI);
            return $firebaseAuth(ref);
        }
    ])

控制器:

function authenticationCtrl($scope, $state, authService) {
    var authenticationCtrl = this;

    // LOGIN
    var login = function (userObject) {
        authService.loginWithPassword(userObject, function () {
            $state.go('[...]');
        },
        function (errorText) {
            // ERROR
            console.error("Error logging in user:", errorText)
        });
    };

    // LOGOUT
    var logout = function () {
        // Clear locally logged in user
        $scope.authData = null;
        authService.logout();
    };

    // PUBLIC
    return {
        login: login,
        logout: logout
    };
};

angular
    .module('app')
    .controller('authenticationCtrl', authenticationCtrl)

服务:

function authService($state, FIREBASE_URI, Auth) {
    var model = this,
        ref = new Firebase(FIREBASE_URI);

    // LOGIN
    model.loginWithPassword = function(credentials, callBack, errorCallBack) {
        Auth.$authWithPassword(credentials)
            .then(function(authData) {
                model.cachedUser = authData;
                callBack();
            }).catch(function(error) {
                console.error("Authentication failed:", error);
            });
    };

    // LOGOUT
    model.logout = function() {
        Auth.$unauth();
        model.cachedUser = null;
        $state.go('common.login');
    };
};

angular
    .module('app')
    .service('authService', authService)

如果您使用的是 Firebase 的电子邮件和密码身份验证提供商,则无法手动使令牌失效。唯一的方法是使用 Custom Authentication implementation 自己生成令牌。然后,您可以通过撤销用于生成令牌的 Firebase 机密来使令牌无效。