質問編集履歴
2
「試したこと」の追加
title
CHANGED
File without changes
|
body
CHANGED
@@ -82,4 +82,95 @@
|
|
82
82
|
end
|
83
83
|
end
|
84
84
|
|
85
|
+
```
|
86
|
+
### 追記2
|
87
|
+
ログにあったpassword_resets_controller.rbの13行目にあたる @user.send_password_reset_email
|
88
|
+
のsend_password_reset_emailメソッドに問題があるのでは?と考え、それを定義しているapp/models/user.rbのファイル(下記コード)を確認しましたが特に問題は見当たりませんでした。
|
89
|
+
|
90
|
+
```ここに言語を入力
|
91
|
+
class User < ApplicationRecord
|
92
|
+
attr_accessor :remember_token, :activation_token, :reset_token
|
93
|
+
before_save :downcase_email
|
94
|
+
before_create :create_activation_digest
|
95
|
+
validates :name, presence: true, length: { maximum: 50 }
|
96
|
+
VALID_EMAIL_REGEX = /\A[\w+\-.]+@[a-z\d\-]+(.[a-z\d\-]+)*.[a-z]+\z/i
|
97
|
+
validates :email, presence: true, length: { maximum: 255 },
|
98
|
+
format: { with: VALID_EMAIL_REGEX },
|
99
|
+
uniqueness: { case_sensitive: false }
|
100
|
+
has_secure_password
|
101
|
+
validates :password, presence: true, length: { minimum: 6 }, allow_nil: true
|
102
|
+
# 渡された文字列のハッシュ値を返す
|
103
|
+
def User.digest(string)
|
104
|
+
cost = ActiveModel::SecurePassword.min_cost ? BCrypt::Engine::MIN_COST :
|
105
|
+
BCrypt::Engine.cost
|
106
|
+
BCrypt::Password.create(string, cost: cost)
|
107
|
+
end
|
108
|
+
|
109
|
+
# ランダムなトークンを返す
|
110
|
+
def User.new_token
|
111
|
+
SecureRandom.urlsafe_base64
|
112
|
+
end
|
113
|
+
|
114
|
+
# 永続セッションのためにユーザーをデータベースに記憶する
|
115
|
+
def remember
|
116
|
+
self.remember_token = User.new_token
|
117
|
+
update_attribute(:remember_digest, User.digest(remember_token))
|
118
|
+
end
|
119
|
+
|
120
|
+
# トークンがダイジェストと一致したらtrueを返す
|
121
|
+
def authenticated?(attribute, token)
|
122
|
+
digest = send("#{attribute}_digest")
|
123
|
+
return false if digest.nil?
|
124
|
+
BCrypt::Password.new(digest).is_password?(token)
|
125
|
+
end
|
126
|
+
|
127
|
+
# ユーザーのログイン情報を破棄する
|
128
|
+
def forget
|
129
|
+
update_attribute(:remember_digest, nil)
|
130
|
+
end
|
131
|
+
|
132
|
+
# アカウントを有効にする
|
133
|
+
def activate
|
134
|
+
update_attribute(:activated, true)
|
135
|
+
update_attribute(:activated_at, Time.zone.now)
|
136
|
+
end
|
137
|
+
|
138
|
+
# 有効化用のメールを送信する
|
139
|
+
def send_activation_email
|
140
|
+
UserMailer.account_activation(self).deliver_now
|
141
|
+
end
|
142
|
+
|
143
|
+
# パスワード再設定の属性を設定する
|
144
|
+
def create_reset_digest
|
145
|
+
self.reset_token = User.new_token
|
146
|
+
update_attribute(:reset_digest, User.digest(reset_token))
|
147
|
+
update_attribute(:reset_sent_at, Time.zone.now)
|
148
|
+
end
|
149
|
+
|
150
|
+
# パスワード再設定のメールを送信する
|
151
|
+
def send_password_reset_email
|
152
|
+
UserMailer.password_reset(self).deliver_now
|
153
|
+
end
|
154
|
+
|
155
|
+
# パスワード再設定の期限が切れている場合はtrueを返す
|
156
|
+
def password_reset_expired?
|
157
|
+
reset_sent_at < 2.hours.ago
|
158
|
+
end
|
159
|
+
|
160
|
+
private
|
161
|
+
|
162
|
+
# メールアドレスをすべて小文字にする
|
163
|
+
def downcase_email
|
164
|
+
self.email = email.downcase
|
165
|
+
end
|
166
|
+
|
167
|
+
# 有効化トークンとダイジェストを作成および代入する
|
168
|
+
def create_activation_digest
|
169
|
+
self.activation_token = User.new_token
|
170
|
+
self.activation_digest = User.digest(activation_token)
|
171
|
+
end
|
172
|
+
|
173
|
+
end
|
174
|
+
|
175
|
+
|
85
176
|
```
|
1
app/controllers/password_resets_controller.rb の追記
title
CHANGED
File without changes
|
body
CHANGED
@@ -9,4 +9,77 @@
|
|
9
9
|
|
10
10
|
|
11
11
|
しかし、勉強不足がゆえにこれ以上先、何をすればよいのか分からず立ち往生しております。
|
12
|
-
原因や解決策をご教示頂ければ幸いです。
|
12
|
+
原因や解決策をご教示頂ければ幸いです。
|
13
|
+
|
14
|
+
### 追記
|
15
|
+
|
16
|
+
app/controllers/password_resets_controller.rb
|
17
|
+
|
18
|
+
```ここに言語を入力
|
19
|
+
class PasswordResetsController < ApplicationController
|
20
|
+
before_action :get_user, only: [:edit, :update]
|
21
|
+
before_action :valid_user, only: [:edit, :update]
|
22
|
+
before_action :check_expiration, only: [:edit, :update] # (1) への対応
|
23
|
+
|
24
|
+
def new
|
25
|
+
end
|
26
|
+
|
27
|
+
def create
|
28
|
+
@user = User.find_by(email: params[:password_reset][:email].downcase)
|
29
|
+
if @user
|
30
|
+
@user.create_reset_digest
|
31
|
+
@user.send_password_reset_email
|
32
|
+
flash[:info] = "Email sent with password reset instructions"
|
33
|
+
redirect_to root_url
|
34
|
+
else
|
35
|
+
flash.now[:danger] = "Email address not found"
|
36
|
+
render 'new'
|
37
|
+
end
|
38
|
+
end
|
39
|
+
|
40
|
+
def edit
|
41
|
+
end
|
42
|
+
|
43
|
+
def update
|
44
|
+
if params[:user][:password].empty? # (3) への対応
|
45
|
+
@user.errors.add(:password, :blank)
|
46
|
+
render 'edit'
|
47
|
+
elsif @user.update_attributes(user_params) # (4) への対応
|
48
|
+
log_in @user
|
49
|
+
flash[:success] = "Password has been reset."
|
50
|
+
redirect_to @user
|
51
|
+
else
|
52
|
+
render 'edit' # (2) への対応
|
53
|
+
end
|
54
|
+
end
|
55
|
+
|
56
|
+
private
|
57
|
+
|
58
|
+
def user_params
|
59
|
+
params.require(:user).permit(:password, :password_confirmation)
|
60
|
+
end
|
61
|
+
|
62
|
+
# beforeフィルタ
|
63
|
+
|
64
|
+
def get_user
|
65
|
+
@user = User.find_by(email: params[:email])
|
66
|
+
end
|
67
|
+
|
68
|
+
# 正しいユーザーかどうか確認する
|
69
|
+
def valid_user
|
70
|
+
unless (@user && @user.activated? &&
|
71
|
+
@user.authenticated?(:reset, params[:id]))
|
72
|
+
redirect_to root_url
|
73
|
+
end
|
74
|
+
end
|
75
|
+
|
76
|
+
# トークンが期限切れかどうか確認する
|
77
|
+
def check_expiration
|
78
|
+
if @user.password_reset_expired?
|
79
|
+
flash[:danger] = "Password reset has expired."
|
80
|
+
redirect_to new_password_reset_url
|
81
|
+
end
|
82
|
+
end
|
83
|
+
end
|
84
|
+
|
85
|
+
```
|