From bb762354b0046212e6544a1c1cfa95a9ea07e889 Mon Sep 17 00:00:00 2001 From: Aurie Date: Mon, 20 May 2019 21:23:44 -0600 Subject: [PATCH 01/10] Add login form --- app/views/sessions/new.html.erb | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/app/views/sessions/new.html.erb b/app/views/sessions/new.html.erb index e69de29b..5ec56da1 100644 --- a/app/views/sessions/new.html.erb +++ b/app/views/sessions/new.html.erb @@ -0,0 +1,7 @@ +<%= form_tag login_path do %> + <%= label_tag :email %> + <%= text_field_tag :email %> + <%= label_tag :password %> + <%= password_field_tag :password %> + <%= submit_tag "Log In" %> +<% end %> \ No newline at end of file From 407679bb298af6ac3515e3271a50cdfec15e5871 Mon Sep 17 00:00:00 2001 From: Aurie Date: Mon, 20 May 2019 21:23:51 -0600 Subject: [PATCH 02/10] Add login controller --- app/controllers/sessions_controller.rb | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/app/controllers/sessions_controller.rb b/app/controllers/sessions_controller.rb index f990b351..e2483d85 100644 --- a/app/controllers/sessions_controller.rb +++ b/app/controllers/sessions_controller.rb @@ -1,4 +1,14 @@ class SessionsController < ApplicationController def new end + + def create + user = User.find_by(email: params[:email]) + if user && user.authenticate(params[:password]) + session[:user_id] = user.id + redirect_to user_path(user) + else + render :new + end + end end From 776d8a7a0da982c36d71bad20edcacb1fe295cdb Mon Sep 17 00:00:00 2001 From: Aurie Date: Mon, 20 May 2019 21:24:12 -0600 Subject: [PATCH 03/10] Add session post route --- config/routes.rb | 1 + 1 file changed, 1 insertion(+) diff --git a/config/routes.rb b/config/routes.rb index 98b7d0a5..e01f0444 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -6,4 +6,5 @@ resources :users, only: [:show, :new, :create] get '/login', to: 'sessions#new' + post '/login', to: 'sessions#create' end From 6d52bb36eb75b83a73531b00d157aa7c713a71a8 Mon Sep 17 00:00:00 2001 From: Aurie Date: Mon, 20 May 2019 21:26:49 -0600 Subject: [PATCH 04/10] Create login spec --- app/views/welcome/index.html.erb | 1 + spec/features/session/login_spec.rb | 19 +++++++++++++++++++ 2 files changed, 20 insertions(+) create mode 100644 spec/features/session/login_spec.rb diff --git a/app/views/welcome/index.html.erb b/app/views/welcome/index.html.erb index e69de29b..b15072f3 100644 --- a/app/views/welcome/index.html.erb +++ b/app/views/welcome/index.html.erb @@ -0,0 +1 @@ +<%= link_to "login?", login_path %> \ No newline at end of file diff --git a/spec/features/session/login_spec.rb b/spec/features/session/login_spec.rb new file mode 100644 index 00000000..f2fd84b2 --- /dev/null +++ b/spec/features/session/login_spec.rb @@ -0,0 +1,19 @@ +require 'rails_helper' + +RSpec.describe "As a visitor, " do + describe "I can login on the welcome page" do + user = User.create(email: "help@gmail.com", password: "12345") + + visit root_path + + click_on "login?" + + expect(current_path).to eq(login_path) + fill_in "email", with: user.email + fill_in "password", with: user.password + + click_on "Log In" + + expect(current_path).to eq(user_path(user)) + end +end \ No newline at end of file From 47110a1122b47a38e7746e81e50439b5350996a1 Mon Sep 17 00:00:00 2001 From: Aurie Date: Mon, 20 May 2019 21:33:33 -0600 Subject: [PATCH 05/10] Add secure password helper to user --- .ruby-version | 1 + 1 file changed, 1 insertion(+) create mode 100644 .ruby-version diff --git a/.ruby-version b/.ruby-version new file mode 100644 index 00000000..005119ba --- /dev/null +++ b/.ruby-version @@ -0,0 +1 @@ +2.4.1 From 0bbf0909d096bbfa7a628ab5bce64088a2b80763 Mon Sep 17 00:00:00 2001 From: Aurie Date: Mon, 20 May 2019 21:34:58 -0600 Subject: [PATCH 06/10] Fixed spec --- spec/features/session/login_spec.rb | 18 ++++++++++-------- 1 file changed, 10 insertions(+), 8 deletions(-) diff --git a/spec/features/session/login_spec.rb b/spec/features/session/login_spec.rb index f2fd84b2..913e3925 100644 --- a/spec/features/session/login_spec.rb +++ b/spec/features/session/login_spec.rb @@ -2,18 +2,20 @@ RSpec.describe "As a visitor, " do describe "I can login on the welcome page" do - user = User.create(email: "help@gmail.com", password: "12345") + it "can login" do + user = User.create(email: "help@gmail.com", password: "12345") - visit root_path + visit root_path - click_on "login?" + click_on "login?" - expect(current_path).to eq(login_path) - fill_in "email", with: user.email - fill_in "password", with: user.password + expect(current_path).to eq(login_path) + fill_in "email", with: user.email + fill_in "password", with: user.password - click_on "Log In" + click_on "Log In" - expect(current_path).to eq(user_path(user)) + expect(current_path).to eq(user_path(user)) + end end end \ No newline at end of file From 886b79ded2408413a8a91df4d8975d12ca658b67 Mon Sep 17 00:00:00 2001 From: Aurie Date: Mon, 20 May 2019 21:37:38 -0600 Subject: [PATCH 07/10] Add user show skeleton --- app/views/users/show.html.erb | 1 + 1 file changed, 1 insertion(+) diff --git a/app/views/users/show.html.erb b/app/views/users/show.html.erb index beb1c899..e8952b2d 100644 --- a/app/views/users/show.html.erb +++ b/app/views/users/show.html.erb @@ -1,3 +1,4 @@ +

Logged in as: <%= current_user.email %>

<%= @user.name %> <%= @user.email %> <%= @user.address %> From 685fd7d6f5cd1f795739fae47da3c099014f0763 Mon Sep 17 00:00:00 2001 From: Aurie Date: Mon, 20 May 2019 21:38:13 -0600 Subject: [PATCH 08/10] Add the thing from the lesson to the application controller --- app/controllers/application_controller.rb | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/app/controllers/application_controller.rb b/app/controllers/application_controller.rb index 00f91bb2..543296fe 100644 --- a/app/controllers/application_controller.rb +++ b/app/controllers/application_controller.rb @@ -1,7 +1,8 @@ class ApplicationController < ActionController::Base protect_from_forgery with: :exception + helper_method :current_user def current_user - + @current_user ||= User.find(session[:user_id]) if session[:user_id] end end From f761721a145ba18242239289c9718ec1601e027b Mon Sep 17 00:00:00 2001 From: Aurie Date: Mon, 20 May 2019 21:43:22 -0600 Subject: [PATCH 09/10] Fix spec? --- spec/features/session/login_spec.rb | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/spec/features/session/login_spec.rb b/spec/features/session/login_spec.rb index 913e3925..34880327 100644 --- a/spec/features/session/login_spec.rb +++ b/spec/features/session/login_spec.rb @@ -1,9 +1,9 @@ require 'rails_helper' -RSpec.describe "As a visitor, " do +RSpec.describe "As a visitor," do describe "I can login on the welcome page" do - it "can login" do - user = User.create(email: "help@gmail.com", password: "12345") + it "and it works I guess" do + user = User.create!(email: "help@gmail.com", password: "12345") visit root_path From 1a8739c5f59ecc10bbb7dab31972a64de7d6c450 Mon Sep 17 00:00:00 2001 From: Aurie Date: Tue, 21 May 2019 17:02:59 -0600 Subject: [PATCH 10/10] Fix spoof user in test --- spec/features/session/login_spec.rb | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/spec/features/session/login_spec.rb b/spec/features/session/login_spec.rb index 34880327..06afbe9c 100644 --- a/spec/features/session/login_spec.rb +++ b/spec/features/session/login_spec.rb @@ -3,7 +3,8 @@ RSpec.describe "As a visitor," do describe "I can login on the welcome page" do it "and it works I guess" do - user = User.create!(email: "help@gmail.com", password: "12345") + user = User.create!(email: "bob@bob.com", password: "124355", + name: "bob", address:"123 bob st.", city: "bobton", state:"MA", zip: 28234) visit root_path