diff --git a/app/controllers/sessions_controller.rb b/app/controllers/sessions_controller.rb new file mode 100644 index 0000000..197944a --- /dev/null +++ b/app/controllers/sessions_controller.rb @@ -0,0 +1,24 @@ +class SessionsController < ApplicationController + + def new + end + + def create + user = User.find_by(email: params[:session][:email]) + if user && user.authenticate(params[:session][:password]) + session[:user_id] = user.id + flash[:notice] = "Welcome!" + redirect_to bracks_path + else + flash[:error] = "Invalid Username or Password!" + redirect_to login_path + end + end + + def destroy + session.clear + flash[:notice] = "Goodbye!" + redirect_to login_path + end + +end diff --git a/app/views/layouts/_navbar.html.erb b/app/views/layouts/_navbar.html.erb index 593cacc..534fee7 100644 --- a/app/views/layouts/_navbar.html.erb +++ b/app/views/layouts/_navbar.html.erb @@ -17,7 +17,9 @@
  • <%= link_to "Bike Rack Map", bracks_path %>
  • Add New Bike
  • <% if current_user %> -
  • <%= link_to "Log Out", logout_path %>
  • +
  • <%= link_to logout_path, method: :delete do %> + Log Out +
  • <% end %> <% end %> diff --git a/app/views/sessions/new.html.erb b/app/views/sessions/new.html.erb new file mode 100644 index 0000000..63b6d5e --- /dev/null +++ b/app/views/sessions/new.html.erb @@ -0,0 +1,20 @@ +

    Login

    + +
    + <%= form_for :session, url: login_path do |f| %> +
    + <%= f.label :email %> + <%= f.text_field :email, class: 'form-control' %> +
    + +
    + <%= f.label :password %> + <%= f.password_field :password, class: 'form-control' %> +
    + +
    + <%= f.submit "Log In", class: 'btn btn-primary' %> +
    + <% end %> +
    Don't have an account? <%= link_to "Sign up by clicking here!", new_user_path %>
    +
    diff --git a/spec/features/users/user_logout_spec.rb b/spec/features/users/user_logout_spec.rb new file mode 100644 index 0000000..4214cc9 --- /dev/null +++ b/spec/features/users/user_logout_spec.rb @@ -0,0 +1,21 @@ +require 'rails_helper' + +feature "user can log out" do + let(:user) {create(:user)} + context "as a logged in user" do + scenario "user is logged in and clicks logout" do + visit login_path + + within ".well" do + fill_in 'session[email]', with: user.email + fill_in 'session[password]', with: user.password + click_on "Log In" + end + + click_on "Log Out" + expect(page).to have_content("Goodbye!") + expect(page).to have_content("Log In") + expect(page).to_not have_content("Log Out") + end + end +end diff --git a/spec/features/users/user_sign_in_spec.rb b/spec/features/users/user_sign_in_spec.rb new file mode 100644 index 0000000..fa4c279 --- /dev/null +++ b/spec/features/users/user_sign_in_spec.rb @@ -0,0 +1,94 @@ +require 'rails_helper' + +feature "login process" do + let(:user) {create(:user)} + context "as an unauthenticated user" do + scenario "can navigate to login path" do + visit root_path + + click_on "Log In" + + expect(current_path).to eq(login_path) + end + scenario "can login with email and password" do + visit login_path + + within ".well" do + fill_in "session[email]", with: user.email + fill_in "session[password]", with: user.password + click_on "Log In" + end + + expect(page).to have_content("Welcome!") + expect(current_path).to eq(bracks_path) + expect(page).to have_content("Log Out") + + expect(page).to_not have_content "Sign Up" + expect(page).to_not have_content "Log In" + end + scenario "they provide invalid email" do + visit login_path + + within ".well" do + fill_in "session[email]", with: "lollllll@lol.com" + fill_in "session[password]", with: user.password + click_button "Log In" + end + + expect(current_path).to eq(login_path) + expect(page).to have_content("Invalid Username or Password!") + + expect(page).to_not have_content "Log Out" + expect(page).to have_content "Sign Up" + expect(page).to have_content "Log In" + end + scenario "they provide invalid password" do + visit login_path + + within ".well" do + fill_in "session[email]", with: user.email + fill_in "session[password]", with: "blah" + click_on "Log In" + end + + expect(current_path).to eq(login_path) + expect(page).to have_content("Invalid Username or Password!") + + expect(page).to_not have_content "Log Out" + expect(page).to have_content "Sign Up" + expect(page).to have_content "Log In" + end + scenario "they provide blank email" do + visit login_path + + within ".well" do + fill_in "session[email]", with: "" + fill_in "session[password]", with: user.password + click_button "Log In" + end + + expect(current_path).to eq(login_path) + expect(page).to have_content("Invalid Username or Password!") + + expect(page).to_not have_content "Log Out" + expect(page).to have_content "Sign Up" + expect(page).to have_content "Log In" + end + scenario "they provide blank password" do + visit login_path + + within ".well" do + fill_in "session[email]", with: user.email + fill_in "session[password]", with: "" + click_button "Log In" + end + + expect(current_path).to eq(login_path) + expect(page).to have_content("Invalid Username or Password!") + + expect(page).to_not have_content "Log Out" + expect(page).to have_content "Sign Up" + expect(page).to have_content "Log In" + end + end +end