upto 3.12: added password + validation

This commit is contained in:
Aaro Koivukangas
2023-08-10 21:45:01 +03:00
parent 9ff0b5acc5
commit 737992c27b
9 changed files with 49 additions and 11 deletions
+1 -1
View File
@@ -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 ]
+2
View File
@@ -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
+5 -4
View File
@@ -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
+2 -2
View File
@@ -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
+11
View File
@@ -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
+12 -3
View File
@@ -1,8 +1,17 @@
<p style="color: red"><%= notice %></p>
<h1>Sign in</h1>
<p style="color: red"><%= notice %></p>
<%= form_with url: session_path, method: :post do |form| %>
<%= form.text_field :username %>
<div>
<%= form.label :username, style: "display: block" %>
<%= form.text_field :username %>
</div>
<div>
<%= form.label :password, style: "display: block" %>
<%= form.password_field :password %>
</div>
<%= form.submit "Log in" %>
<% end %>
+9
View File
@@ -16,6 +16,15 @@
<%= form.text_field :username %>
</div>
<div>
<%= form.label :password, style: "display: block" %>
<%= form.password_field :password %>
</div>
<div>
<%= form.label :password_confirmation, style: "display: block" %>
<%= form.password_field :password_confirmation %>
</div>
<div>
<%= form.submit %>
</div>
@@ -0,0 +1,5 @@
class AddPasswordDigestToUser < ActiveRecord::Migration[7.0]
def change
add_column :users, :password_digest, :string
end
end
Generated
+2 -1
View File
@@ -10,7 +10,7 @@
#
# It's strongly recommended that you check this file into your version control system.
ActiveRecord::Schema[7.0].define(version: 2023_08_10_162557) do
ActiveRecord::Schema[7.0].define(version: 2023_08_10_180821) do
create_table "beerclubs", force: :cascade do |t|
t.string "name"
t.integer "founded"
@@ -53,6 +53,7 @@ ActiveRecord::Schema[7.0].define(version: 2023_08_10_162557) do
t.string "username"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
t.string "password_digest"
end
end