Complete the code to generate a secure token for user authentication.
token = SecureRandom.[1](24)
The SecureRandom.hex(24) method generates a random hexadecimal string of length 48 characters, suitable for tokens.
Complete the code to add secure password authentication to the User model.
has_secure_[1]The has_secure_password method adds password hashing and authentication features to the User model.
Fix the error in the controller method that finds a user by token.
user = User.find_by([1]: params[:token])The User model typically stores the token in the auth_token attribute, so the query should use that key.
Fill both blanks to generate and save a new token for the user.
user.[1] = SecureRandom.[2](20) user.save!
The auth_token attribute stores the token, and SecureRandom.hex(20) generates a secure hex string token.
Fill all three blanks to create a before_action that authenticates user by token.
before_action :[1], only: [:show, :update] def [2] @user = User.find_by([3]: request.headers['Authorization']) head :unauthorized unless @user end
The method authenticate_user is used as a before_action. It finds the user by auth_token from the Authorization header and denies access if not found.