0
0
Ruby on Railsframework~10 mins

Why authentication secures applications in Ruby on Rails - Test Your Understanding

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

Complete the code to add user authentication in a Rails controller.

Ruby on Rails
before_action :[1], only: [:edit, :update, :destroy]
Drag options to blanks, or click blank then click option'
Aauthenticate_user!
Bverify_token
Ccheck_admin
Dauthorize_user
Attempts:
3 left
💡 Hint
Common Mistakes
Using authorization methods instead of authentication
Forgetting the exclamation mark in method name
2fill in blank
medium

Complete the code to create a secure login form in Rails.

Ruby on Rails
<%= form_with url: session_path, method: :post do |[1]| %>
Drag options to blanks, or click blank then click option'
Af
Bbuilder
Cform
Dsession
Attempts:
3 left
💡 Hint
Common Mistakes
Using variable names that are not standard form builders
Using plural or unrelated variable names
3fill in blank
hard

Fix the error in the authentication check method.

Ruby on Rails
def current_user
  @current_user ||= User.find_by(id: session[:[1]])
end
Drag options to blanks, or click blank then click option'
Aid
Bcurrent_user_id
Cuser_id
Dsession_id
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'id' instead of 'user_id'
Using 'session_id' which is unrelated
4fill in blank
hard

Fill both blanks to securely log out a user in a Rails controller.

Ruby on Rails
def destroy
  session[:[1]] = [2]
  redirect_to root_path
end
Drag options to blanks, or click blank then click option'
Auser_id
Bnil
Cfalse
Dcurrent_user
Attempts:
3 left
💡 Hint
Common Mistakes
Setting session key to false instead of nil
Using wrong session key name
5fill in blank
hard

Fill all three blanks to create a secure user authentication helper method.

Ruby on Rails
def logged_in?
  !!session[:[1]] && User.exists?(id: session[:[2]]) && @current_user = User.find_by(id: session[:[3]])
end
Drag options to blanks, or click blank then click option'
Auser_id
Dcurrent_user_id
Attempts:
3 left
💡 Hint
Common Mistakes
Using different keys for session lookup
Using 'current_user_id' which is not standard