0
0
Ruby on Railsframework~10 mins

Token-based authentication 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 generate a secure token for user authentication.

Ruby on Rails
token = SecureRandom.[1](24)
Drag options to blanks, or click blank then click option'
Ahex
Bbase64
Cuuid
Drandom
Attempts:
3 left
💡 Hint
Common Mistakes
Using SecureRandom.random which does not exist.
Using SecureRandom.uuid which returns a UUID, not a hex string.
2fill in blank
medium

Complete the code to add secure password authentication to the User model.

Ruby on Rails
has_secure_[1]
Drag options to blanks, or click blank then click option'
Asession
Bpassword
Ctoken
Dauthentication
Attempts:
3 left
💡 Hint
Common Mistakes
Trying to use has_secure_token which is a different method.
Using has_secure_authentication which does not exist.
3fill in blank
hard

Fix the error in the controller method that finds a user by token.

Ruby on Rails
user = User.find_by([1]: params[:token])
Drag options to blanks, or click blank then click option'
Aauth_token
Btoken
Cauthentication_token
Daccess_token
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'token' which may not be the attribute name.
Using 'access_token' which is not standard in this context.
4fill in blank
hard

Fill both blanks to generate and save a new token for the user.

Ruby on Rails
user.[1] = SecureRandom.[2](20)
user.save!
Drag options to blanks, or click blank then click option'
Aauth_token
Bhex
Crandom
Dtoken
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'token' instead of 'auth_token' as attribute.
Using SecureRandom.random which does not exist.
5fill in blank
hard

Fill all three blanks to create a before_action that authenticates user by token.

Ruby on Rails
before_action :[1], only: [:show, :update]

def [2]
  @user = User.find_by([3]: request.headers['Authorization'])
  head :unauthorized unless @user
end
Drag options to blanks, or click blank then click option'
Aauthenticate_user
Bauth_token
Ctoken
Dauthorize_user
Attempts:
3 left
💡 Hint
Common Mistakes
Using different names for before_action and method.
Using 'token' instead of 'auth_token' as attribute.