Skip to content

Commit b34d4aa

Browse files
committed
User.authenticate クラスメソッドを実装
user.salt と password 引数から hashed_password を再生成して user.hashed_password と比較する
1 parent adc6303 commit b34d4aa

File tree

1 file changed

+12
-0
lines changed

1 file changed

+12
-0
lines changed

depot/app/models/user.rb

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,18 @@ def password= (pwd)
2323
#@password, self.salt でも良いはず...?
2424
end
2525

26+
def self.authenticate (name, password)
27+
user = self.find_by_name(name)
28+
if user
29+
encrypted_password = encrypted_password(password, user.salt)
30+
#self.encrypted_password() と呼ぶほうがいいんじゃないかなぁ...
31+
if user.hashed_password != encrypted_password
32+
user = nil
33+
end
34+
end
35+
user
36+
end
37+
2638
private
2739

2840
def password_non_blank

0 commit comments

Comments
 (0)