Skip to content

Commit

Permalink
Implement advanced login
Browse files Browse the repository at this point in the history
  • Loading branch information
Piotr Suwala committed Oct 23, 2016
1 parent 113c702 commit 8d41684
Show file tree
Hide file tree
Showing 8 changed files with 146 additions and 111 deletions.
176 changes: 70 additions & 106 deletions .idea/workspace.xml

Large diffs are not rendered by default.

13 changes: 13 additions & 0 deletions app/assets/stylesheets/custom.scss
Original file line number Diff line number Diff line change
Expand Up @@ -175,3 +175,16 @@ input {
color: $state-danger-text;
}
}
.checkbox {
margin-top: -10px;
margin-bottom: 10px;
span {
margin-left: 20px;
font-weight: normal;
}
}

#session_remember_me {
width: auto;
margin-left: 0;
}
3 changes: 2 additions & 1 deletion app/controllers/sessions_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ def create
user = User.find_by(email: params[:session][:email].downcase)
if user && user.authenticate(params[:session][:password])
log_in user
params[:session][:remember_me] == '1' ? remember(user) : forget(user)
redirect_to user
else
flash.now[:danger] = 'Invalid email/password combination'
Expand All @@ -15,7 +16,7 @@ def create
end

def destroy
log_out
log_out if logged_in?
redirect_to root_url
end
end
27 changes: 25 additions & 2 deletions app/helpers/sessions_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,17 +5,40 @@ def log_in(user)
session[:user_id] = user.id
end

# Returns the current logged-in user (if any).
# Remembers a user in a persistent session.
def remember(user)
user.remember
cookies.permanent.signed[:user_id] = user.id
cookies.permanent[:remember_token] = user.remember_token
end

# Returns the user corresponding to the remember token cookie.
def current_user
@current_user ||= User.find_by(id: session[:user_id])
if (user_id = session[:user_id])
@current_user ||= User.find_by(id: user_id)
elsif (user_id = cookies.signed[:user_id])
user = User.find_by(id: user_id)
if user && user.authenticated?(cookies[:remember_token])
log_in user
@current_user = user
end
end
end

# Returns true if the user is logged in, false otherwise.
def logged_in?
!current_user.nil?
end

def forget(user)
user.forget
cookies.delete(:user_id)
cookies.delete(:remember_token)
end

# Logs out the current user.
def log_out
forget(current_user)
session.delete(:user_id)
@current_user = nil
end
Expand Down
23 changes: 23 additions & 0 deletions app/models/user.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
class User < ApplicationRecord
attr_accessor :remember_token
before_save { self.email = email.downcase }
validates :name, presence: true, length: { maximum: 50 }
VALID_EMAIL_REGEX = /\A[\w+\-.]+@[a-z\d\-.]+\.[a-z]+\z/i
Expand All @@ -14,4 +15,26 @@ def User.digest(string)
BCrypt::Engine.cost
BCrypt::Password.create(string, cost: cost)
end

# Returns a random token.
def User.new_token
SecureRandom.urlsafe_base64
end

# Remembers a user in the database for use in persistent sessions.
def remember
self.remember_token = User.new_token
update_attribute(:remember_digest, User.digest(remember_token))
end

# Returns true if the given token matches the digest.
def authenticated?(remember_token)
return false if remember_digest.nil?
BCrypt::Password.new(remember_digest).is_password?(remember_token)
end

# Forgets a user.
def forget
update_attribute(:remember_digest, nil)
end
end
7 changes: 6 additions & 1 deletion app/views/sessions/new.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,14 @@
<%= f.label :password %>
<%= f.password_field :password, class: 'form-control' %>

<%= f.label :remember_me, class: "checkbox inline" do %>
<%= f.check_box :remember_me %>
<span>Remember me on this computer</span>
<% end %>

<%= f.submit "Log in", class: "btn btn-primary" %>
<% end %>

<p>New user? <%= link_to "Sign up now!", signup_path %></p>
</div>
</div>
</div>
5 changes: 5 additions & 0 deletions db/migrate/20161023205010_add_remember_digest_to_users.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
class AddRememberDigestToUsers < ActiveRecord::Migration[5.0]
def change
add_column :users, :remember_digest, :string
end
end
3 changes: 2 additions & 1 deletion db/schema.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,15 @@
#
# It's strongly recommended that you check this file into your version control system.

ActiveRecord::Schema.define(version: 20161022112747) do
ActiveRecord::Schema.define(version: 20161023205010) do

create_table "users", force: :cascade do |t|
t.string "name"
t.string "email"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
t.string "password_digest"
t.string "remember_digest"
t.index ["email"], name: "index_users_on_email", unique: true
end

Expand Down

0 comments on commit 8d41684

Please sign in to comment.