automatic logout after inactivity/idle

前端 未结 4 2020
半阙折子戏
半阙折子戏 2020-12-29 05:21

How to set up in the rails application that if any user is idle for 30 minutes or a specific period of time he should be automatically get logged out. Can any one give any s

4条回答
  •  不思量自难忘°
    2020-12-29 06:07

    Using Devise Gem:

    We can use inbuilt feature of the devise gem but it will not automatically redirect to the sign in page after timeout, redirection will be done after we perform any action.

    We can Perform automatically sign out:

    By using gem "auto-session-timeout"

    https://github.com/pelargir/auto-session-timeout

    The disadvantage of using this gem is that it will logout automatically if user is only typing(performing key press event) till the timeout time.

    we can override the disadvantage by using Javascript:

    Step 1: Define the routes

    get 'application/session_time'
    

    Step 2: JavaScript will contain

    $(document).ready(function(){
    if($("#user_logged").is(":visible") == true )
    {
        $(document).on( "keypress keydown", function () {
            console.log("hello");
            $.ajax({
                type:"GET",
                url:"/application/session_time",
                dataType:"html",
            });
        });
    }
    });
    

    Step 3: application controller will contain:

    @session_time = 5.minute
    auto_session_timeout @session_time
    def session_time
      @session_time = 5.minute
    end
    

    Step 4: div to find it is sign in page or not

    <% if user_signed_in? %>
      
    <% end %>

    The blank div is kept because we have to load JavaScript only when user is logged in ,so instead of finding current user is nil or not.

    I have done with the blank div,it will be available if user is loggedin,so in beginning of JavaScript it is checked that div_id "user_loged" is present or not.

提交回复
热议问题