From 250ec51a1cf3f239393d8a5e40027175a373ff2c Mon Sep 17 00:00:00 2001 From: Atho Simatupang Date: Fri, 26 Aug 2022 07:56:43 +0700 Subject: [PATCH] v1.2.0 - Feat: Enkripsi User Password --- Gemfile | 2 +- Gemfile.lock | 2 ++ README.md | 3 +++ app/controllers/users_controller.rb | 4 +++- app/models/user.rb | 3 ++- db/migrate/20220825145043_change_users_columns.rb | 6 ++++++ db/schema.rb | 4 ++-- 7 files changed, 19 insertions(+), 5 deletions(-) create mode 100644 db/migrate/20220825145043_change_users_columns.rb diff --git a/Gemfile b/Gemfile index 0e16bd2..3b563b6 100644 --- a/Gemfile +++ b/Gemfile @@ -31,7 +31,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 a62178e..e58eb2a 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -68,6 +68,7 @@ GEM tzinfo (~> 2.0) addressable (2.8.1) public_suffix (>= 2.0.2, < 6.0) + bcrypt (3.1.18) bindex (0.8.1) bootsnap (1.13.0) msgpack (~> 1.2) @@ -217,6 +218,7 @@ PLATFORMS x64-mingw-ucrt DEPENDENCIES + bcrypt (~> 3.1.7) bootsnap capybara debug diff --git a/README.md b/README.md index ec465ce..4c6663a 100644 --- a/README.md +++ b/README.md @@ -16,6 +16,9 @@ Yand dibutuhkan untuk menjalankan aplikasi ini: * Database SQLite (Development) & PostgreSQL (Production) # VERSI +v1.2.0 | 26 Agustus 2022 +- Feat: Enkripsi User Password + v1.1.0 | 25 Agustus 2022 - Feat: User - Feat: Login User diff --git a/app/controllers/users_controller.rb b/app/controllers/users_controller.rb index 92a87d6..fcaebb1 100644 --- a/app/controllers/users_controller.rb +++ b/app/controllers/users_controller.rb @@ -65,7 +65,9 @@ def login_form end def login - if @user = User.find_by(email: params[:email], password: params[:password]) + @user = User.find_by(email: params[:email]) + + if @user && @user.authenticate(params[:password]) session[:current_user_id] = @user.id flash[:just_signed_up] = "Welcome to our site" redirect_to @user diff --git a/app/models/user.rb b/app/models/user.rb index 21d63eb..0a08f4a 100644 --- a/app/models/user.rb +++ b/app/models/user.rb @@ -1,7 +1,8 @@ class User < ApplicationRecord + has_secure_password + validates :name, presence: true validates :email, presence: true, uniqueness: true - validates :password, presence: true def articles return Article.where(user_id: self.id) diff --git a/db/migrate/20220825145043_change_users_columns.rb b/db/migrate/20220825145043_change_users_columns.rb new file mode 100644 index 0000000..cea4371 --- /dev/null +++ b/db/migrate/20220825145043_change_users_columns.rb @@ -0,0 +1,6 @@ +class ChangeUsersColumns < ActiveRecord::Migration[7.0] + def change + add_column :users, :password_digest, :string + remove_column :users, :password, :string + end +end diff --git a/db/schema.rb b/db/schema.rb index 106b2e2..fd353db 100644 --- a/db/schema.rb +++ b/db/schema.rb @@ -10,7 +10,7 @@ # # It's strongly recommended that you check this file into your version control system. -ActiveRecord::Schema[7.0].define(version: 2022_08_25_052836) do +ActiveRecord::Schema[7.0].define(version: 2022_08_25_145043) do create_table "articles", force: :cascade do |t| t.string "title" t.text "content" @@ -36,7 +36,7 @@ t.datetime "created_at", null: false t.datetime "updated_at", null: false t.string "image_name" - t.string "password" + t.string "password_digest" end add_foreign_key "comments", "articles"