summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authormo khan <mo@mokhan.ca>2016-04-30 09:39:37 -0600
committermo khan <mo@mokhan.ca>2016-04-30 09:39:37 -0600
commitd352a8604b2f3cea2834367fe4fd697d9e92ee76 (patch)
treee6a5e10652a0a9b59b6ed547b47b62921beb026d
parentd723a6c93fc5f1086d596934e5e2362654a83f21 (diff)
start to move authentication logic to UserSession.
-rw-r--r--app/controllers/sessions_controller.rb4
-rw-r--r--app/models/user.rb11
-rw-r--r--app/models/user_session.rb12
-rw-r--r--spec/models/user_session.rb33
-rw-r--r--spec/models/user_spec.rb29
5 files changed, 47 insertions, 42 deletions
diff --git a/app/controllers/sessions_controller.rb b/app/controllers/sessions_controller.rb
index 5a20c27..3970e08 100644
--- a/app/controllers/sessions_controller.rb
+++ b/app/controllers/sessions_controller.rb
@@ -1,7 +1,7 @@
class SessionsController < PublicController
def create
- if user = User.authenticate(params[:user][:username], params[:user][:password])
- session[:user_id] = user.id
+ if user_session = UserSession.authenticate(params[:user][:username], params[:user][:password])
+ session[:user_id] = user_session.id
redirect_to dashboard_path
else
flash[:warning] = t("sessions.create.invalid_login")
diff --git a/app/models/user.rb b/app/models/user.rb
index 1f120e3..e762352 100644
--- a/app/models/user.rb
+++ b/app/models/user.rb
@@ -53,17 +53,6 @@ class User < ActiveRecord::Base
GoogleDrive.new(self)
end
- def self.authenticate(username, password)
- user = User.find_by(
- "email = :email OR username = :username",
- username: username.downcase,
- email: username.downcase
- )
- if user.present?
- user.authenticate(password)
- end
- end
-
private
def create_profile
diff --git a/app/models/user_session.rb b/app/models/user_session.rb
new file mode 100644
index 0000000..c413160
--- /dev/null
+++ b/app/models/user_session.rb
@@ -0,0 +1,12 @@
+class UserSession
+ def self.authenticate(username, password)
+ user = User.find_by(
+ "email = :email OR username = :username",
+ username: username.downcase,
+ email: username.downcase
+ )
+ if user.present?
+ user.authenticate(password)
+ end
+ end
+end
diff --git a/spec/models/user_session.rb b/spec/models/user_session.rb
new file mode 100644
index 0000000..bdbb4e2
--- /dev/null
+++ b/spec/models/user_session.rb
@@ -0,0 +1,33 @@
+require 'rails_helper'
+
+describe UserSession do
+ describe "#authenticate" do
+ context "when credentials are correct" do
+ it "returns true" do
+ user = create(:user, password: "password", password_confirmation: "password")
+ expect(UserSession.authenticate(user.email.upcase, "password")).to eql(user)
+ end
+
+ it "is case in-sensitive for username" do
+ user = create(:user,
+ username: "upcase",
+ password: "password",
+ password_confirmation: "password"
+ )
+ expect(UserSession.authenticate("UPcase", "password")).to eql(user)
+ end
+ end
+
+ context "when the email is not registered" do
+ it "returns nil" do
+ expect(UserSession.authenticate("sofake@noteven.com", "password")).to be_nil
+ end
+ end
+
+ context "when the username is not registered" do
+ it "returns nil" do
+ expect(UserSession.authenticate("sofake", "password")).to be_nil
+ end
+ end
+ end
+end
diff --git a/spec/models/user_spec.rb b/spec/models/user_spec.rb
index 88cd73c..84057b1 100644
--- a/spec/models/user_spec.rb
+++ b/spec/models/user_spec.rb
@@ -99,35 +99,6 @@ describe User do
end
end
- describe "#authenticate" do
- context "when credentials are correct" do
- it "returns true" do
- user = create(:user, password: "password", password_confirmation: "password")
- expect(User.authenticate(user.email.upcase, "password")).to eql(user)
- end
-
- it "is case in-sensitive for username" do
- user = create(:user,
- username: "upcase",
- password: "password",
- password_confirmation: "password"
- )
- expect(User.authenticate("UPcase", "password")).to eql(user)
- end
- end
-
- context "when the email is not registered" do
- it "returns nil" do
- expect(User.authenticate("sofake@noteven.com", "password")).to be_nil
- end
- end
-
- context "when the username is not registered" do
- it "returns nil" do
- expect(User.authenticate("sofake", "password")).to be_nil
- end
- end
- end
describe "#to_param" do
it "returns the username as the uniq identifier" do