So I’ve got this task where I’m supposed to output a list of the last five logins by the currently logged in user.
So like this:
Person A logs in at 12.00 12/12/12.
Person A logs in again at 12.00 12/01/13.
And so on.
This needs to work up to a list of five, once that limit is reached the first login (12.00 12/12/12 in this case) is to be taken out of view.
What I’ve got so far is a Login system with remember tokens and some simple authorization for the login itself (doing it from scratch and going back to some details fom Michael Hartl’s tutorial). Right now, I can list the current time that the user logged in, but not the previous ‘versions’.
So, this is my view:
<% if signed_in? %>
<h1>Welcome, <%= current_user.name %>!</h1>
<p>You were last logged in on:</p>
<ul>
<li><%= current_user.lastlogin %></li>
</ul>
<%= link_to "Sign out", signout_path, method: "delete", class: "btn btn-large" %>
<% else %>
... (state for non logged in users)
<% end %>
This is my user class:
class User < ActiveRecord::Base
attr_accessible :name, :email, :password, :password_confirmation, :lastlogin
has_secure_password
before_save { |user| user.email = email.downcase }
before_save :create_remember_token
...(validation following this)
And this is my sessions_controller
class SessionsController < ApplicationController
def new
end
def create
user = User.find_by_email(params[:session][:email].downcase)
if user && user.authenticate(params[:session][:password])
sign_in user
redirect_to root_path
else
flash.now[:error] = 'Invalid email/password combination'
render 'new'
end
end
And here we have the sign_in function
module SessionsHelper
def sign_in(user)
# user.update_attribute(:lastlogin, Time.now)
user.touch(:lastlogin)
cookies.permanent[:remember_token] = user.remember_token
self.current_user = user
end
end
I’m looking for ideas on how to tackle this problem.
Another thing that I’ve forgotten to mention is that the view layer needs to be modified to some extent so as to loop through the five login times and output them in order, as opposed to hard coding in five list items.
One thing that I’m thinking of is setting up a relationship between a table with five fields corresponding to one user. And then upon sign in (or sign out) take the :lastlogin value and push it into a field in the table.
Then when all fields have been filled, look at the first date and replace it with the new.
So what I’m thinking is something like
def sign_in
user.touch(:lastlogin)
# push :lastlogin value into field in LoginsTable
cookies.permanent[:remember_token] = user.remember_token
self.current_user = user
end
I am quite new to Ruby on Rails and might be way off here. I’ve tried searching for functionality like the one I’m explaining here, and all I’ve gotten is various plugins/full-out gems that handle this and everything else when it comes to Authentication and so forth, which isn’t really ‘needed’ in this case as it’s just a small time project for myself.
Cheers!
Edit:
The futile attempt at pushing into the user_logins table.
module SessionsHelper
def sign_in(user)
user.update_attribute(:lastlogin, Time.now)
cookies.permanent[:remember_token] = user.remember_token
self.current_user = user
login_time = UserLogins.create(:login_time Time.now, :user_id current_user)
end
end
Now this is obviously not working. I’m not too sure on exactly how to loop through the login_times either in the view layer. But I reckon I’ll get it working eventually, now that you put me on the right track 🙂
You’ll get yourself into a messy pickle if you hardcode 5 fields to hold the last 5 login dates, instead consider having a table to store login history for all users. A simple table “user_logins” with
id,user_idandlogin_timewould suffice.Write to this table every time the user logs in, then in your view just select the top 5 results ordered by
login_time, descending.Hope that makes sense!