I will post the process of advancing the rails tutorial on my own.
It touches on words that I didn't understand in the process, jammed errors, and so on.
Please point out any mistakes as it is an output of personal learning.
Since this is my first post, I think there are many places that are difficult to read, but please forgive me.
Review of attr_accessor
reference https://qiita.com/Hassan/items/0e034a1d42b2335936e6 .remember_token can be used (remember_token attribute can be used)
def remember
self.remember_token = User.new_token
update_attribute(:remember_digest, User.digest(remember_token))
end
The last (remember_token) omits self.
BCrypt::Password.new(remember_digest) == remember_token
If you take a closer look at this code, it looks really strange. The bcrypt-encrypted password is compared directly to the token. (rails tutorial Quoted from Chapter 9)
Hmmm, surely ...
Is the digest decrypted when comparing with ==? However, the hash of bcrypt should not be decrypted, so it should not be decrypted. (rails tutorial Quoted from Chapter 9)
Well, what's going on?
BCrypt::Password.new(remember_digest).is_password?(remember_token)
Apparently the two codes above have the same meaning. Also, it seems that the is_password? method validates the object and the arguments passed to it and returns true if they match.
Also, the log_in_as helper method defined in Listing 9.24 defines it as session [: user_id]. At this rate, it is very difficult to check the complicated branching process of the current_user method with an integration test. (rails tutorial Quoted from Chapter 9)
Hmm? Why? A. Because the session method cannot be handled in the integration test
test/test_helper.rb
def log_in_as(user)
session[:user_id] = user.id #Not available
end
test/helpers/sessions_helper_test.rb
test "current_user returns right user when session is nil" do
assert_equal @user, current_user
assert is_logged_in?
end
What is this current_user? It seems that it becomes a model object after the current_user method is executed. Perhaps,,,
In other words log_in user And so on, and I think the session method is also being executed ...
app/helpers/sessions_helper.rb
def current_user
if user_id = session[:user_id] #false
@current_user ||= User.find_by(id: user_id)
elsif user_id = cookies.signed[:user_id] #true
user = User.find_by(id: user_id)
if user && user.authenticated?(cookies[:remember_token])
log_in user #Log in
@current_user = user
end
end
end
The current_user method is a model object that returns an instance of the model as a return value.
test/helpers/sessions_helper_test.rb
test "current_user returns nil when remember digest is wrong" do
@user.update_attribute(:remember_digest,User.digest(User.new_token))
assert_nil current_user
end
@user.update_attribute(:remember_digest, User.digest(User.new_token))
Updated remember_digest to new one.
assert_nil current_user
Is
app/helpers/sessions_helper.rb
def current_user
if user_id = session[:user_id]
@current_user ||= User.find_by(id: user_id)
elsif user_id = cookies.signed[:user_id]
user = User.find_by(id: user_id)
if user && user.authenticated?(cookies[:remember_token]) #false and current_user becomes nil
log_in user
@current_user = user
end
end
end
if user && user.authenticated?(cookies[:remember_token])
I am checking if current_user is nil in this verification.
In this chapter, there were few errors and I was able to understand the contents well.
Recommended Posts