Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

User Login #89

Merged
merged 10 commits into from
May 21, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .ruby-version
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
2.4.1
3 changes: 2 additions & 1 deletion app/controllers/application_controller.rb
Original file line number Diff line number Diff line change
@@ -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
10 changes: 10 additions & 0 deletions app/controllers/sessions_controller.rb
Original file line number Diff line number Diff line change
@@ -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
7 changes: 7 additions & 0 deletions app/views/sessions/new.html.erb
Original file line number Diff line number Diff line change
@@ -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 %>
1 change: 1 addition & 0 deletions app/views/users/show.html.erb
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
<h1>Logged in as: <%= current_user.email %></h1>
<%= @user.name %>
<%= @user.email %>
<%= @user.address %>
Expand Down
1 change: 1 addition & 0 deletions app/views/welcome/index.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
<%= link_to "login?", login_path %>
1 change: 1 addition & 0 deletions config/routes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,5 @@
resources :users, only: [:show, :new, :create]

get '/login', to: 'sessions#new'
post '/login', to: 'sessions#create'
end
22 changes: 22 additions & 0 deletions spec/features/session/login_spec.rb
Original file line number Diff line number Diff line change
@@ -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: "[email protected]", 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