0
0
Ruby on Railsframework~10 mins

Remember me functionality in Ruby on Rails - Interactive Code Practice

Choose your learning style9 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to set a permanent signed cookie for remembering the user.

Ruby on Rails
cookies.permanent.signed[:user_id] = [1]
Drag options to blanks, or click blank then click option'
Auser.id
Bsession[:user_id]
Crequest.remote_ip
Dparams[:user_id]
Attempts:
3 left
💡 Hint
Common Mistakes
Using session or params instead of the user ID.
Storing IP address instead of user ID.
2fill in blank
medium

Complete the code to find the user by the ID stored in the signed cookie.

Ruby on Rails
User.find_by(id: cookies.signed[:[1]])
Drag options to blanks, or click blank then click option'
Asession_user_id
Bcurrent_user
Cremember_token
Duser_id
Attempts:
3 left
💡 Hint
Common Mistakes
Using incorrect cookie keys like remember_token or current_user.
Confusing session keys with cookie keys.
3fill in blank
hard

Fix the error in the method that sets the remember token cookie.

Ruby on Rails
cookies.[1][:remember_token] = user.remember_token
Drag options to blanks, or click blank then click option'
Aencrypted
Bsigned
Cpermanent
Dtemporary
Attempts:
3 left
💡 Hint
Common Mistakes
Using signed without permanent causes the cookie to expire quickly.
Using temporary which expires when the browser closes.
4fill in blank
hard

Fill both blanks to create a method that forgets the user by deleting cookies.

Ruby on Rails
cookies.delete(:[1])
cookies.delete(:[2])
Drag options to blanks, or click blank then click option'
Auser_id
Bremember_token
Csession_id
Dauth_token
Attempts:
3 left
💡 Hint
Common Mistakes
Deleting unrelated cookies like session_id or auth_token.
Deleting only one of the required cookies.
5fill in blank
hard

Fill all three blanks to complete the user authentication method using remember me cookies.

Ruby on Rails
if (user_id = cookies.signed[:[1]]) && (user = User.find_by(id: user_id)) && user.authenticated?(:[2], cookies[:[3]])
  log_in user
end
Drag options to blanks, or click blank then click option'
Auser_id
Bremember
Cremember_token
Dsession_token
Attempts:
3 left
💡 Hint
Common Mistakes
Mixing up cookie keys and token types.
Using session keys instead of cookie keys.