has_secure_password :recovery_password
This commit is contained in:
parent
49998ee5b7
commit
639eb2ba04
5 changed files with 14 additions and 7 deletions
|
@ -24,10 +24,10 @@ class UsersController < ApplicationController
|
||||||
|
|
||||||
def recover_password
|
def recover_password
|
||||||
user = User.find(params[:user_id])
|
user = User.find(params[:user_id])
|
||||||
if user.password_recovery_code == params[:recovery_code]
|
if user.authenticate_recovery_password(params[:recovery_code])
|
||||||
user.password = params[:password]
|
user.password = params[:password]
|
||||||
user.password_confirmation = params[:repeated_password]
|
user.password_confirmation = params[:repeated_password]
|
||||||
user.password_recovery_code = nil
|
user.recovery_password_digest = nil
|
||||||
if user.save
|
if user.save
|
||||||
redirect_to '/welcome'
|
redirect_to '/welcome'
|
||||||
end
|
end
|
||||||
|
|
|
@ -1,9 +1,10 @@
|
||||||
class UserMailer < ApplicationMailer
|
class UserMailer < ApplicationMailer
|
||||||
def password_recovery
|
def password_recovery
|
||||||
@user = params[:user]
|
@user = params[:user]
|
||||||
recovery_code = ('a'..'z').to_a.shuffle[0,8].join
|
recovery_password = ('a'..'z').to_a.shuffle[0,8].join
|
||||||
@user.update(password_recovery_code: recovery_code)
|
@user.recovery_password = recovery_password
|
||||||
@url = "http://localhost:18210/password_recovery/#{@user.id}/#{recovery_code}"
|
@user.save
|
||||||
|
@url = "http://localhost:18210/password_recovery/#{@user.id}/#{recovery_password}"
|
||||||
mail(to: @user.email, subject: 'Password recovery')
|
mail(to: @user.email, subject: 'Password recovery')
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
|
@ -1,3 +1,4 @@
|
||||||
class User < ApplicationRecord
|
class User < ApplicationRecord
|
||||||
has_secure_password
|
has_secure_password
|
||||||
|
has_secure_password :recovery_password, validations: false
|
||||||
end
|
end
|
||||||
|
|
|
@ -0,0 +1,5 @@
|
||||||
|
class ChangePasswordRecoveryCodeToRecoveryPasswordDigest < ActiveRecord::Migration[6.1]
|
||||||
|
def change
|
||||||
|
rename_column :users, :password_recovery_code, :recovery_password_digest
|
||||||
|
end
|
||||||
|
end
|
4
db/schema.rb
generated
4
db/schema.rb
generated
|
@ -10,7 +10,7 @@
|
||||||
#
|
#
|
||||||
# It's strongly recommended that you check this file into your version control system.
|
# It's strongly recommended that you check this file into your version control system.
|
||||||
|
|
||||||
ActiveRecord::Schema.define(version: 2021_03_20_233401) do
|
ActiveRecord::Schema.define(version: 2021_03_21_093857) do
|
||||||
|
|
||||||
create_table "authors", force: :cascade do |t|
|
create_table "authors", force: :cascade do |t|
|
||||||
t.string "first_name"
|
t.string "first_name"
|
||||||
|
@ -43,7 +43,7 @@ ActiveRecord::Schema.define(version: 2021_03_20_233401) do
|
||||||
t.string "password_digest"
|
t.string "password_digest"
|
||||||
t.datetime "created_at", precision: 6, null: false
|
t.datetime "created_at", precision: 6, null: false
|
||||||
t.datetime "updated_at", precision: 6, null: false
|
t.datetime "updated_at", precision: 6, null: false
|
||||||
t.string "password_recovery_code"
|
t.string "recovery_password_digest"
|
||||||
end
|
end
|
||||||
|
|
||||||
end
|
end
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue