Commit d352a86

mo khan <mo@mokhan.ca>
2016-04-30 15:39:37
start to move authentication logic to UserSession.
1 parent d723a6c
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")
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
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
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
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