我应该如何在 AngularFire/Firebase 中使用户访问令牌失效

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

本文关键字:用户 访问令牌 失效 Firebase AngularFire 我应该      更新时间:2023-09-26

>上下文

使用在此处和此处找到的注销用户的说明。

我正在使用AngularFire和AngularJS,并通过电子邮件和密码方法对用户进行身份验证。

问题所在

调用$unauth不会使用户的令牌失效:对$authWithPassword的后续调用将返回相同的身份验证令牌。

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

《守则》

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 的电子邮件和密码身份验证提供程序,则无法手动使令牌失效。执行此操作的唯一方法是使用自定义身份验证实现自行生成令牌。然后,您可以通过撤销用于生成令牌的 Firebase 机密来使令牌失效。