rails tutorial Chapter 9

Introduction

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.

Chapter 9 Evolving Login Mechanism

9.1.1 Memory token and encryption

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.

9.1.2 Keeping logged in

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.

9.3.2 Test [Remember me]

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.

At the end

In this chapter, there were few errors and I was able to understand the contents well.

Recommended Posts

rails tutorial Chapter 7
rails tutorial Chapter 5
rails tutorial Chapter 10
rails tutorial Chapter 9
rails tutorial Chapter 8
Rails Tutorial Chapter 5 Notes
Rails Tutorial Chapter 10 Notes
Rails Tutorial Chapter 3 Notes
Rails Tutorial Chapter 3 Learning
Rails Tutorial Memorandum (Chapter 3, 3.1)
Rails Tutorial Chapter 4 Notes
Rails Tutorial Chapter 4 Learning
Rails Tutorial Chapter 1 Learning
Rails Tutorial Chapter 2 Learning
Rails Tutorial Chapter 8 Notes
Rails Tutorial Memorandum (Chapter 3, 3.3.2)
rails tutorial
rails tutorial
rails tutorial
rails tutorial
rails tutorial
rails tutorial
[Rails Tutorial Chapter 4] Rails-flavored Ruby
[Rails Struggle/Rails Tutorial] Summary of Rails Tutorial Chapter 2
Rails tutorial memorandum 1
Rails tutorial memorandum 2
Chewing Rails Tutorial [Chapter 2 Toy Application]
Start Rails Tutorial
[Beginner] Rails Tutorial
Rails Tutorial (4th Edition) Memo Chapter 6
Rails Tutorial 6th Edition Learning Summary Chapter 10
Rails Tutorial 6th Edition Learning Summary Chapter 7
Rails Tutorial 6th Edition Learning Summary Chapter 4
Rails Tutorial 6th Edition Learning Summary Chapter 9
Rails Tutorial 6th Edition Learning Summary Chapter 6
Rails Tutorial 6th Edition Learning Summary Chapter 5
Rails Tutorial 6th Edition Learning Summary Chapter 2
Rails Tutorial Chapter 0: Preliminary Basic Knowledge Learning 5
Rails Tutorial 6th Edition Learning Summary Chapter 3
Rails Tutorial 6th Edition Learning Summary Chapter 8
Chapter 4 Rails Flavored Ruby
Rails Tutorial cheat sheet
[Rails] Learning with Rails tutorial
rails tutorial fighting notes Ⅲ
[Rails Struggle/Rails Tutorial] What you learned in Rails Tutorial Chapter 6
Rails Tutorial Chapter 1 From Zero to Deployment [Try]
Chewing Rails Tutorial [Chapter 3 Creating Almost Static Pages]
[Rails tutorial] A memorandum of "Chapter 11 Account Activation"
Resolve Gem :: FilePermissionError when running gem install rails (Rails Tutorial Chapter 1)
[Ruby on Rails Tutorial] Error in the test in Chapter 3
11.1 AccountActivations Resources: Rails Tutorial Notes-Chapter 11
Rails Tutorial Records and Memorandum # 0
I tried Rails beginner [Chapter 1]
Rails Tutorial (4th Edition) Summary
Rails Tutorial 4th Edition: Chapter 1 From Zero to Deployment
Resolve ActiveRecord :: NoDatabaseError when doing rails test (Rails tutorial Chapter 3)
I tried Rails beginner [Chapter 2]
[Rails] Implementation of tutorial function
Chewing Rails Tutorial [Chapter 1 From Zero to Deployment] Second Half
Chewing Rails Tutorial [Chapter 1 From Zero to Deployment] First Half
Rails Tutorial Chapter 14 Creating Relationship Test Data with Factory Bot