node.js - 如何使用 REDIS + Node.js 注销我的应用程序

标签 node.js express redis mean-stack logout

我正在实现注销功能。我是第一次使用这项技术,所以请帮助我。 使用: <强>角JS Node .js Redis

AngularJS 代码

<md-button md-no-ink class="md-primary" ng-click="logoutCall()">logout</md-button>

Controller.js

$scope.logoutCall = function(){
      var req = $http.get("api/logout");
      console.log(" in side logout call function!");
      // some fun stuff ....

}

Redis代码

router.route("/logout")
    .get(
        function(req, res){
            verifyAuth(req, res, function(err, authorized){
                if(err){
                    res.json(err);
                    return;
                };
            // ---- fun stuff conti. -----
            console.log("regis api/logout");
            });
    });  

函数 verifyAuth()

//verify authorization
var verifyAuth = function(req, res, callBack){

    if(req.headers && req.headers.hasOwnProperty("x-auth-token")){
        // get the redis client and try to get the corresponding object using the auth_token as a key
        var redisClient = redis.getClient();
        redisClient.get('auth_token.' + req.headers["x-auth-token"], function(err, reply){
            if(err || !reply){
                callBack({error:"Unauthorized: Please sign in"});
                return;
            }

            // refresh the session expire to 20 mins
            redisClient.expire('auth_token.' + req.headers["x-auth-token"], 1200); 
            req.user = JSON.parse(reply);
            callBack(null, true);

        });
    }else{
       callBack({error:"Missing Authentication: Please sign in"});
       return;
    }
};

最佳答案

我找到了解决方案:

//登出路由 Redis代码

 router.route("/logout")
        .get(
            function(req, res){
                var redisClient = redis.getClient();
                redisClient.del('auth_token.' + req.headers["x-auth-token"], function(err, reply){
                    if(!err || reply){
                        console.log("del token ");
                        res.sendStatus(401);
                        return;
                }


            });
        });

使用 Http 拦截器捕获此错误代码。 Http拦截器:

function responseError( response ) {

    console.log("inside responseError" + response.status);
        switch ( response.status ) {
            //...can handle different error codes differently

            case 401:
                $rootScope.$broadcast( 'httpError', { message: 'logout successful' } );

                $state.go('login page'); 
                break;
        }

        return $q.reject( response );
    }

关于node.js - 如何使用 REDIS + Node.js 注销我的应用程序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37731961/

相关文章:

Javascript promise 链 - ES6

javascript - 如何在两个独立标签之间获取 HTML 元素

javascript - 如何通过 Node 网络套接字连接 ruby​​ TCP 服务器?

node.js - browserify bundle electron app 主进程文件

Express.js 应用程序无服务器,使用 Lambda 或函数 - 一个好主意?

node.js - 是否可以在 express + node js 中进行重定向?

Node.js : Express app. 获取多个查询参数

node.js - 如何创建多个 Nodejs 套接字 io 服务器客户端?

amazon-ec2 - 雷迪斯 | redis-cli INFO/avg_ttl 值

laravel-5 - laravel 对于实时应用程序有什么好处?