Skip to content

Commit

Permalink
Merge pull request #89 from alexander-mathieu/user-login
Browse files Browse the repository at this point in the history
User Login
  • Loading branch information
kylecornelissen authored May 21, 2019
2 parents a458444 + 1a8739c commit f63175e
Show file tree
Hide file tree
Showing 8 changed files with 45 additions and 1 deletion.
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

0 comments on commit f63175e

Please sign in to comment.