[RUBY] 28th day of an engineer who will become a full-fledged person in 100 days

28th day of an engineer who will become a full-fledged person in 100 days

This article is a continuation of Implementing Account BAN. If you like, please click here.

Implement session timeout

If the user does not access for a certain period of time This time I will implement a mechanism to automatically cancel the login status. First from session_controller. The heart of this time is session [: last_access_time] = Time.current part. Save the current time in session when you log in. The rest is a normal login process.

sessions_controller.rb


class User::SessionsController < User::Base

#abridgement...


    if User::Authenticator.new(user_member).authenticate(@form.password)
      if user_member.suspended?
        flash.now.alert = "Account is suspended"
        render action: "new"
      else
        session[:user_member_id] = user_member.id
        #Save the current time in session at login.
        session[:last_access_time] = Time.current
        flash.notice = "You are now logged"
        redirect_to :user_root
      end
    else
      flash.now.alert = "Incorrect email address or password"
      render action: "new"
    end
  end
end

And here is the implementation of session timeout. This time, if there is no login for 60 minutes or more, the session will time out.

controllers/user/base.rb


#Set timeout time to 60 minutes
TIMEOUT = 60.minutes

private def check_timeout

  if current_user
    #Session if last access was less than 60 minutes[:last_access_time]To the current time
    if session[:last_access_time] >= TIMEOUT.ago
      session[:last_access_time] = Time.current

    #If the last access is not within 60, delete the session as a session timeout and
    #Redirect to the top screen.
    else
      session.delete(user_id)
      flash.alert = "The session has timed out."
      redirect_to :staff_login
    end
  end
end

I was able to implement it like this. It was easier than I expected. Save the last access time in session. Is that the important part? That's all for today.

71 days to become a full-fledged engineer

Recommended Posts

28th day of an engineer who will become a full-fledged person in 100 days
26th day of engineer who will become full-fledged in 100 days
New engineer who will be one serving in 100 days (5th day)
New engineer who will be one serving in 100 days (6th day)
New engineer who will be one serving in 100 days (4th day)
New engineer who will be one serving in 100 days (day 0)
New engineer who will be one serving in 100 days (5th day)
New engineer who will be one serving in 100 days (6th day)
New engineer who will be one serving in 100 days (1st day)
New engineer who will be one serving in 100 days (4th day)
New engineer who will be one serving in 100 days (2nd day)
26th day of engineer who will become full-fledged in 100 days
28th day of an engineer who will become a full-fledged person in 100 days
Java beginners make poker games in 4 days (3rd day)
New engineer who will be one serving in 100 days (day 0)
New engineer who will be one serving in 100 days (1st day)
New engineer who will be one serving in 100 days (2nd day)
What an inexperienced engineer who took a leave of absence from college learned in 2020
《Inexperienced → web engineer》 5th day of practice
《Inexperienced → web engineer》 3rd day of practice
《Inexperienced → web engineer》 4th day of practice
《Inexperienced → web engineer》 1st day of practice
Write Code Every Day! ~ 2nd year Java engineer writes code every day ~
26th day of engineer who will become full-fledged in 100 days
27-year-old story of becoming a development engineer from inexperienced programming
《Inexperienced → web engineer》 5th day of practice
《Inexperienced → web engineer》 4th day of practice
Stuck in an enum in front of a blacksmith
Pursuing the mystery that the number of DB connections in Tomcat increases to only 8-A day of an OSS support engineer
Become an iOS engineer
Find the number of days in a month with Kotlin
Follow a mysterious event where the log level suddenly changes-A day of an OSS support engineer
Java beginners make poker games in 4 days (3rd day)
Make Blackjack in Java
Refactoring: Make Blackjack in Java
Rock-paper-scissors game for beginners in Java
[For beginners] Run Selenium in Java
New engineer who will be one serving in 100 days (2nd day)
New engineer who will be one serving in 100 days (day 0)
New engineer who will be one serving in 100 days (5th day)
New engineer who will be one serving in 100 days (6th day)
New engineer who will be one serving in 100 days (1st day)
New engineer who will be one serving in 100 days (4th day)
New engineer who will be one serving in 100 days (2nd day)
26th day of engineer who will become full-fledged in 100 days
28th day of an engineer who will become a full-fledged person in 100 days
Java beginners make poker games in 4 days (3rd day)
[Java] Beginners want to make dating. 1st
Java Day 2018
I wanted to make (a == 1 && a == 2 && a == 3) true in Java
Represents "next day" and "previous day" in Java / Android
[Java beginner's anguish] Hard-to-test code implemented in Junit