【问题标题】:Angularjs Sessions to automatically Logout an user after a fixed timeAngularjs 会话在固定时间后自动注销用户
【发布时间】:2015-09-11 15:57:16
【问题描述】:

我在 angularjs 中创建了一个网站。 现在我需要确保如果用户在登录超过 10 分钟后没有使用该网站,则应该进行自动注销。还应清除与网站关联的本地存储。 而且我还需要避免 URL 重写。 我是 angularjs 的新手。 我听说那届会议可以完成这些目标。 我需要一个好的建议以及获取参考的链接。

【问题讨论】:

标签: angularjs session


【解决方案1】:

【讨论】:

    【解决方案2】:

    在.config运行后在app.js中声明这段代码,10分钟后它会重定向到登录页面

    var d = new Date();
    var n = d.getTime();  //n in ms
    
    $rootScope.idleEndTime = n+(10*60*1000); //set end time to 10 min from now
    //console.log(idletime);
    $document.find('body').on('mousemove keydown DOMMouseScroll mousewheel mousedown touchstart', checkAndResetIdle); //monitor events
    
    function checkAndResetIdle() //user did something
    {
        var d = new Date();
        var n = d.getTime();  //n in ms
    
        if (n>$rootScope.idleEndTime)
        {
             $document.find('body').off('mousemove keydown DOMMouseScroll mousewheel mousedown touchstart'); //un-monitor events
    
             //$location.search('IntendedURL',$location.absUrl()).path('/login'); //terminate by sending to login page
    
             alert('Session ended due to inactivity');
             $location.path("/login");
             //console.log($location.path());
                           $rootScope.$apply();
        }
        else
        {
             $rootScope.idleEndTime = n+(10*60*1000); //reset end time
        }
    }
    

    【讨论】:

      【解决方案3】:

      您也可以使用angular-activity-monitor 来完成。

      [ActivityMonitor] 是一个简单的服务,它会根据用户的 DOM 活动发出几个事件。只要用户被认为是“活跃的”,它还允许您在后台“让项目保持活跃”。

      angular.module('myModule', ['ActivityMonitor']);
      
      MyController.$inject = ['ActivityMonitor'];
      function MyController(ActivityMonitor) {
        // how long (in seconds) until user is considered inactive
        ActivityMonitor.options.inactive = 900; 
      
        // What to do once user is considered inactive
        ActivityMonitor.on('inactive', function() {
          alert("You're inactive");
        });
      }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2019-09-30
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2015-10-18
        • 2015-04-12
        • 2016-11-03
        • 2022-01-06
        相关资源
        最近更新 更多