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 #209

Merged
merged 7 commits into from
May 24, 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
6 changes: 0 additions & 6 deletions app/controllers/admin/admin_controller.rb

This file was deleted.

7 changes: 7 additions & 0 deletions app/controllers/admin/admins_controller.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
class Admin::AdminsController < ActionController::Base

def dashboard

end

end
Binary file not shown.
24 changes: 24 additions & 0 deletions app/controllers/sessions_controller.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
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
if user.default?
redirect_to profile_path
elsif user.merchant?
redirect_to dashboard_path
elsif user.admin?
redirect_to root_path
end
flash[:message] = "Logged in as #{user.name}"
else
redirect_to login_path
flash[:message] = "The email or password you entered was incorrect."
end
end

end
1 change: 1 addition & 0 deletions app/controllers/users_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ def show
end

def profile

if params[:new_id] != nil
@user = User.find(params[:new_id])
else
Expand Down
Empty file.
1 change: 1 addition & 0 deletions app/views/admin/admins/dashboard.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
<%= link_to "Admin Dashboard", admin_dashboard_path %>
8 changes: 8 additions & 0 deletions app/views/sessions/new.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<%= form_tag login_path do %>
<%= label_tag :email %>
<%= text_field_tag :email %>

<%= label_tag :password %>
<%= password_field_tag :password %>
<%= submit_tag "Login" %>
<% end %>
7 changes: 5 additions & 2 deletions config/routes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
# For details on the DSL available within this file, see http://guides.rubyonrails.org/routing.html
root to: 'welcome#index'

resources :merchants, only: [:index]
resources :merchants, only: [:index]

namespace :dashboard do
resources :items, only: [:index]
Expand All @@ -11,6 +11,9 @@
get '/dashboard', to: 'merchants#dashboard', as: 'dashboard'
# get '/merchants', to: 'merchants#index'

get '/login', to: 'sessions#new'
post '/login', to: 'sessions#create'

get '/logout', to: 'application#logout'

resources :items, only: [:index, :show]
Expand All @@ -20,7 +23,7 @@
resources :users, only: [:index, :new, :create, :show, :edit]

namespace :admin do
get '/dashboard', to: "admin#dashboard"
get '/dashboard', to: "admins#dashboard"
end

# resources :carts, only: [:create]
Expand Down
3 changes: 2 additions & 1 deletion spec/features/navigation/admin_sees_nav_bar_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,8 @@
visit root_path

expect(page).to have_link("Admin Dashboard")
click_on "Admin Dashboard"

click_link "Admin Dashboard"
expect(current_path).to eq(admin_dashboard_path)
end

Expand Down
94 changes: 94 additions & 0 deletions spec/features/users/user_can_login_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
require 'rails_helper'

RSpec.describe "As a visitor" do

before :each do
@user = User.create!(email: "[email protected]", password: "password", role: 0, active: true, name: "Yu Xer", address: "123 street", city: "News Userville", state:"US", zip: "80211")
@merchant = User.create!(email: "[email protected]", password: "password", role: 1, active: true, name: "Murr Chante", address: "123 street", city: "Merchantston", state:"MR", zip: "80211")
@admin = User.create!(email: "[email protected]", password: "password", role: 2, active: true, name: "Addie Munn", address: "123 street", city: "West Adminster", state:"AD", zip: "80211")
end

describe "when I visit the login path" do
it "I see a field to enter my email address and password" do

visit login_path

expect(page).to have_field("Email")
expect(page).to have_field("Password")
expect(page).to have_button("Login")
end

describe "when I submit valid information" do
it "if regular user, I am redirected to my profile page" do
visit login_path

fill_in "Email", with: "[email protected]"
fill_in "Password", with: "password"
click_button("Login")

expect(current_path).to eq(profile_path)
expect(page).to have_content("Logged in as #{@user.name}")
end

it "if merchant user, I am redirected to my merchant dashboard page" do
visit login_path

fill_in "Email", with: "[email protected]"
fill_in "Password", with: "password"
click_button("Login")

expect(current_path).to eq(dashboard_path)
expect(page).to have_content("Logged in as #{@merchant.name}")
end

it "if admin user, I am redirected to the root page" do
visit login_path

fill_in "Email", with: "[email protected]"
fill_in "Password", with: "password"
click_button("Login")

expect(current_path).to eq(root_path)
expect(page).to have_content("Logged in as #{@admin.name}")
end

describe "if invalid info given" do
it "redirects to login and tells me credentials were incorrect" do
visit login_path

fill_in "Email", with: "[email protected]"
fill_in "Password", with: "forgot"
click_button("Login")

expect(current_path).to eq(login_path)
expect(page).to have_content("The email or password you entered was incorrect.")
end
end


# As a visitor
# When I visit the login page ("/login")
# And I submit invalid information
# Then I am redirected to the login page
# And I see a flash message that tells me that my credentials were incorrect
# I am NOT told whether it was my email or password that was incorrect
end
end
end

# When I submit valid information
# If I am a regular user, I am redirected to my profile page
# If I am a merchant user, I am redirected to my merchant dashboard page
# If I am an admin user, I am redirected to the home page of the site
# And I see a flash message that I am logged in

# describe "when I click on the 'login' link in the nav bar" do
# describe "I am taken to a login page" do
# it "and as a User can login and go to my profile page" do
# end
# end
# end
#
# fill_in 'Email', with: '[email protected]'
# fill_in 'Password', with: 'password'
# click_link("Submit")
22 changes: 22 additions & 0 deletions spec/features/users/visitor_sees_nav_bar_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
require 'rails_helper'

RSpec.describe "as a visitor on the site" do
describe "a navigation bar is seen" do
it "has working links that redirect" do
visit root_path

click_link("Items")
expect(current_path).to eq(items_path)

click_link("Merchants")
expect(current_path).to eq(merchants_path)

click_link("Home")
expect(current_path).to eq(root_path)

expect(page).to have_link("Cart")
expect(page).to have_link("Login")
expect(page).to have_link("Register")
end
end
end