Commit 109d000

mo khan <mo@mokhan.ca>
2014-03-30 22:59:24
implement login action.
1 parent de63577
Changed files (6)
app
controllers
models
views
config
spec
app/controllers/logins_controller.rb
@@ -2,4 +2,13 @@ class LoginsController < ApplicationController
   def new
     render nothing: true
   end
+
+  def create
+    if User.find_by(email: params[:email]).authenticate(params[:password])
+      redirect_to dashboard_path
+    else
+      flash[:error] = I18n.translate(:invalid_credentials)
+      render :new
+    end
+  end
 end
app/models/user.rb
@@ -0,0 +1,3 @@
+class User
+
+end
app/views/logins/new.html.erb
config/locales/en.yml
@@ -21,3 +21,4 @@
 
 en:
   hello: "Hello world"
+  invalid_credentials: "Ooops... that's not right!"
config/routes.rb
@@ -1,5 +1,6 @@
 Mocode::Application.routes.draw do
   resources :logins, only: [:new, :create]
+  get 'dashboard', to: 'dashboard#index'
   # The priority is based upon order of creation: first created -> highest priority.
   # See how all your routes lay out with "rake routes".
 
spec/controllers/logins_controller_spec.rb
@@ -1,10 +1,34 @@
 require "spec_helper"
 
 describe LoginsController do
-  context "#new" do
+  describe "#new" do
     it "loads the login page" do
       get :new
       response.should be_ok
     end
   end
+
+  describe "#create" do
+    let(:user) { double(authenticate: false) }
+
+    before :each do
+      User.stub(:find_by).with(email: 'email@example.com').and_return(user)
+      user.stub(:authenticate).with('password').and_return(true)
+    end
+
+    context "when the email and password is incorrect" do
+      it "displays an error" do
+        post :create, email: 'email@example.com', password: 'wrong'
+        flash[:error].should == I18n.translate(:invalid_credentials)
+        response.should render_template(:new)
+      end
+    end
+
+    context "when the email and password is correct" do
+      it "redirects to the dashboard" do
+        post :create, email: 'email@example.com', password: 'password'
+        response.should redirect_to(dashboard_path)
+      end
+    end
+  end
 end