【问题标题】:Auto Logout in Angular jsAngular js中的自动注销
【发布时间】:2023-04-03 15:39:01
【问题描述】:

如何使用 Angularjs 的 ng-idle 模块在 30 分钟空闲时间后实现自动注销?

【问题讨论】:

    标签: angularjs logout ng-idle


    【解决方案1】:

    我对 angularjs 真的不太了解,我正在做一个关于它的课程,但我才刚刚开始。虽然我确实知道 github 上有人可以帮助你。我刚刚在此处包含了 sn-ps,但如果您想了解更多信息,请查看此站点:https://github.com/HackedByChinese/ng-idle

    不管怎样,这里是:

    在 angular.js 之后包含 angular-idle.js。您可以通过以下命令使用 Bower 进行安装:bower install --save ng-idle。

    简单的例子:

    // include the `ngIdle` module
    var app = angular.module('demo', ['ngIdle']);
    
    app
    .controller('EventsCtrl', function($scope, Idle) {
        $scope.events = [];
    
        $scope.$on('IdleStart', function() {
            // the user appears to have gone idle
        });
    
        $scope.$on('IdleWarn', function(e, countdown) {
            // follows after the IdleStart event, but includes a countdown until         the user is considered timed out
            // the countdown arg is the number of seconds remaining until then.
            // you can change the title or display a warning dialog from here.
            // you can let them resume their session by calling Idle.watch()
        });
    
        $scope.$on('IdleTimeout', function() {
            // the user has timed out (meaning idleDuration + timeout has passed     without any activity)
            // this is where you'd log them
        });
    
        $scope.$on('IdleEnd', function() {
            // the user has come back from AFK and is doing stuff. if you are     warning them, you can use this to hide the dialog
        });
    
        $scope.$on('Keepalive', function() {
            // do something to keep the user's session alive
        });
    
     })
    .config(function(IdleProvider, KeepaliveProvider) {
       // configure Idle settings
        IdleProvider.idle(5); // in seconds
        IdleProvider.timeout(5); // in seconds
        KeepaliveProvider.interval(2); // in seconds
    })
    .run(function(Idle){
        // start watching when the app runs. also starts the Keepalive service     by default.
        Idle.watch();
    });
    

    希望有所帮助:)

    【讨论】:

    • 感谢您的回答!这真的很有帮助。在我的情况下不起作用的一件事是,会话过期后,浏览器名称是“您的会话已过期”。但在登录后,该名称仍然没有改变,它显示您的会话已过期。
    • ng-idle 在第一次超时时工作正常。一旦超时,然后我们再次登录应用程序,之后它就不起作用了。有什么想法吗?
    • 知道了!登录后需要调用 Idle.watch() 再次激活空闲。
    • user1457957 能否给我详细的代码来实现 ngIdle ?
    猜你喜欢
    • 2016-05-11
    • 1970-01-01
    • 2018-02-05
    • 2011-07-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-04-03
    相关资源
    最近更新 更多