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 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 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 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 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 %> 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/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 diff --git a/spec/features/session/login_spec.rb b/spec/features/session/login_spec.rb new file mode 100644 index 00000000..06afbe9c --- /dev/null +++ b/spec/features/session/login_spec.rb @@ -0,0 +1,22 @@ +require 'rails_helper' + +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: "bob@bob.com", password: "124355", + name: "bob", address:"123 bob st.", city: "bobton", state:"MA", zip: 28234) + + 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 +end \ No newline at end of file