diff --git a/Gemfile b/Gemfile index b060714..fb8c546 100644 --- a/Gemfile +++ b/Gemfile @@ -34,7 +34,7 @@ gem "jbuilder" # gem "kredis" # Use Active Model has_secure_password [https://guides.rubyonrails.org/active_model_basics.html#securepassword] -# gem "bcrypt", "~> 3.1.7" +gem "bcrypt", "~> 3.1.7" # Windows does not include zoneinfo files, so bundle the tzinfo-data gem gem "tzinfo-data", platforms: %i[ mingw mswin x64_mingw jruby ] diff --git a/Gemfile.lock b/Gemfile.lock index e1b3208..b148e08 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -68,6 +68,7 @@ GEM tzinfo (~> 2.0) addressable (2.8.4) public_suffix (>= 2.0.2, < 6.0) + bcrypt (3.1.19) bindex (0.8.1) bootsnap (1.16.0) msgpack (~> 1.2) @@ -219,6 +220,7 @@ PLATFORMS x86_64-linux DEPENDENCIES + bcrypt (~> 3.1.7) bootsnap capybara debug diff --git a/app/controllers/sessions_controller.rb b/app/controllers/sessions_controller.rb index 8e0086e..cfca408 100644 --- a/app/controllers/sessions_controller.rb +++ b/app/controllers/sessions_controller.rb @@ -4,11 +4,12 @@ class SessionsController < ApplicationController def create user = User.find_by username: params[:username] - if user.nil? - redirect_to signin_path, notice: "User #{params[:username]} does not exist!" - else + + if user && user.authenticate(params[:password]) session[:user_id] = user.id if user - redirect_to user + redirect_to user_path user, notice: "Welcome back :)" + else + redirect_to signin_path, notice: "Incorrect username or password" end end diff --git a/app/controllers/users_controller.rb b/app/controllers/users_controller.rb index 71322bd..a3bb6ab 100644 --- a/app/controllers/users_controller.rb +++ b/app/controllers/users_controller.rb @@ -26,7 +26,7 @@ class UsersController < ApplicationController respond_to do |format| if @user.save - format.html { redirect_to user_url(@user), notice: "User was successfully created." } + format.html { redirect_to signin_path, notice: "User was successfully created." } format.json { render :show, status: :created, location: @user } else format.html { render :new, status: :unprocessable_entity } @@ -67,6 +67,6 @@ class UsersController < ApplicationController # Only allow a list of trusted parameters through. def user_params - params.require(:user).permit(:username) + params.require(:user).permit(:username, :password, :password_confirmation) end end diff --git a/app/models/user.rb b/app/models/user.rb index 1dc9d5d..4b9c322 100644 --- a/app/models/user.rb +++ b/app/models/user.rb @@ -1,10 +1,21 @@ class User < ApplicationRecord include RatingAverage + has_secure_password + validates :username, uniqueness: true, length: { minimum: 3, maximum: 30 } + validates :password, length: { minimum: 3 } + validate :has_proper_characters_in_password has_many :ratings has_many :beers, through: :ratings has_many :memberships, dependent: :destroy has_many :beer_clubs, through: :memberships + + def has_proper_characters_in_password + regex = "(?=.*[[:upper:]])(?=.*[0-9])" + if not password.match(regex) + errors.add :password, "doesn't contain uppercase letter and a number" + end + end end diff --git a/app/views/sessions/new.html.erb b/app/views/sessions/new.html.erb index a69aae7..4bf317d 100644 --- a/app/views/sessions/new.html.erb +++ b/app/views/sessions/new.html.erb @@ -1,8 +1,17 @@ -
<%= notice %>
-<%= notice %>
+ <%= form_with url: session_path, method: :post do |form| %> - <%= form.text_field :username %> +