Bird
Raised Fist0
Ruby on Railsframework~20 mins

Session-based authentication in Ruby on Rails - Practice Problems & Coding Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
Session Auth Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ component_behavior
intermediate
2:00remaining
What happens after a successful login in a Rails session-based authentication?
In a typical Rails app using session-based authentication, what is stored in the session after a user logs in successfully?
ANo data is stored; the session remains empty after login.
BThe user's password is stored in the session for quick verification.
CThe entire user object is serialized and stored in the session.
DThe user's ID is stored in the session hash to identify the logged-in user.
Attempts:
2 left
💡 Hint
Think about what minimal information is needed to recognize the user on future requests.
❓ lifecycle
intermediate
2:00remaining
What happens when a user logs out in a Rails session-based authentication?
Which action correctly clears the session to log out a user in Rails?
Asession[:user_id] = nil but session data remains intact.
Bsession.clear to remove all session data.
Csession[:user_id] = false to mark user as logged out.
Dsession.delete(:user_id) to remove the user ID from the session.
Attempts:
2 left
💡 Hint
Think about removing all session data to ensure complete logout.
📝 Syntax
advanced
2:00remaining
Identify the correct way to set a session variable in Rails controller
Which of the following code snippets correctly sets the current user's ID in the session?
Asession.user_id = current_user.id
Bsession.set(:user_id, current_user.id)
Csession[:user_id] = current_user.id
Dsession['user_id'] == current_user.id
Attempts:
2 left
💡 Hint
Remember how to access and assign values in a Ruby hash.
🔧 Debug
advanced
2:00remaining
Why does the session not persist after redirect in Rails?
Consider this code snippet in a Rails controller: ```ruby session[:user_id] = @user.id redirect_to dashboard_path ``` But after redirect, session[:user_id] is nil. What is the most likely cause?
Ruby on Rails
session[:user_id] = @user.id
redirect_to dashboard_path
AThe session store is not configured properly, so session data is not saved.
BThe redirect_to method clears the session automatically.
CThe session hash is read-only and cannot be assigned.
D@user.id is nil, so nothing is stored in the session.
Attempts:
2 left
💡 Hint
Check if the app has a session store configured in config/initializers or environment files.
🧠 Conceptual
expert
3:00remaining
Why is storing sensitive data in Rails session discouraged?
Which is the best explanation for why sensitive data like passwords should not be stored in Rails sessions?
ASessions are stored client-side as cookies, which can be read or tampered with by users.
BRails sessions expire immediately, so sensitive data is lost too soon.
CSessions automatically encrypt all data, so storing sensitive data is safe.
DStoring sensitive data in sessions causes the server to crash.
Attempts:
2 left
💡 Hint
Think about where session data is stored and who can access it.

Practice

(1/5)
1. What does session[:user_id] typically store in a Rails application using session-based authentication?
easy
A. The user's password
B. The user's email address
C. The ID of the currently logged-in user
D. The entire user object

Solution

  1. Step 1: Understand session usage in Rails

    Rails uses the session hash to store small pieces of data between requests, commonly the user ID for authentication.
  2. Step 2: Identify what session[:user_id] holds

    This key stores the unique identifier of the logged-in user to recognize them on subsequent requests.
  3. Final Answer:

    The ID of the currently logged-in user -> Option C
  4. Quick Check:

    session[:user_id] = user ID [OK]
Hint: Remember session stores user ID, not full user data [OK]
Common Mistakes:
  • Thinking session stores full user object
  • Confusing user ID with password or email
  • Assuming session is client-side storage
2. Which of the following is the correct way to log out a user by clearing the session in a Rails controller?
easy
A. session.clear
B. All of the above
C. session[:user_id] = nil
D. session.delete(:user_id)

Solution

  1. Step 1: Review methods to clear session data

    In Rails, you can remove session data by clearing all keys, deleting a specific key, or setting a key to nil.
  2. Step 2: Confirm all options effectively log out user

    All three methods remove the user ID from session, effectively logging out the user.
  3. Final Answer:

    All of the above -> Option B
  4. Quick Check:

    Any method removing user_id logs out user [OK]
Hint: Any method removing session[:user_id] logs out user [OK]
Common Mistakes:
  • Only using session[:user_id] = nil and ignoring others
  • Trying to clear session without removing user_id
  • Confusing session.clear with resetting entire app
3. Given this Rails controller code snippet:
def create
  user = User.find_by(email: params[:email])
  if user&.authenticate(params[:password])
    session[:user_id] = user.id
    redirect_to dashboard_path
  else
    render :new
  end
end

What happens when the user enters correct email and password?
medium
A. User ID is saved in session and redirected to dashboard
B. User is redirected to login page again
C. Session is cleared and user stays on login page
D. An error is raised because session[:user_id] is not set

Solution

  1. Step 1: Analyze authentication condition

    The code checks if a user with the given email exists and if the password matches using authenticate.
  2. Step 2: Understand session assignment and redirect

    If authentication passes, it stores the user's ID in session and redirects to the dashboard.
  3. Final Answer:

    User ID is saved in session and redirected to dashboard -> Option A
  4. Quick Check:

    Correct login sets session[:user_id] and redirects [OK]
Hint: Correct login sets session[:user_id] and redirects [OK]
Common Mistakes:
  • Assuming user stays on login page after success
  • Thinking session[:user_id] is not set on success
  • Confusing render with redirect behavior
4. Identify the error in this logout action:
def logout
  session[:user] = nil
  redirect_to root_path
end
medium
A. No error, this code works fine
B. redirect_to should be redirect_back
C. session[:user] = nil does not clear session
D. Session key should be :user_id, not :user

Solution

  1. Step 1: Check session key used for authentication

    Rails session-based authentication typically uses session[:user_id] to track logged-in user.
  2. Step 2: Identify mismatch in session key

    The code sets session[:user] to nil, which does not clear the actual user ID stored in session[:user_id].
  3. Final Answer:

    Session key should be :user_id, not :user -> Option D
  4. Quick Check:

    Correct session key is :user_id [OK]
Hint: Use session[:user_id] to clear login, not session[:user] [OK]
Common Mistakes:
  • Using wrong session key name
  • Assuming setting any session key to nil logs out user
  • Confusing redirect_to with redirect_back
5. You want to restrict access to a controller action so only logged-in users can see it. Which code snippet correctly checks session-based authentication before allowing access?
hard
A. before_action :require_login def require_login redirect_to login_path unless session[:user_id] end
B. before_action :require_login def require_login redirect_to login_path if session[:user_id] end
C. before_action :require_login def require_login redirect_to root_path unless session[:user_id] end
D. before_action :require_login def require_login redirect_to login_path unless session[:user] end

Solution

  1. Step 1: Understand access control with before_action

    Use a method to check if session[:user_id] exists before allowing access.
  2. Step 2: Identify correct redirect condition

    If session[:user_id] is missing, redirect to login page to block access.
  3. Final Answer:

    Redirect to login unless session[:user_id] exists -> Option A
  4. Quick Check:

    Access allowed only if session[:user_id] present [OK]
Hint: Redirect unless session[:user_id] exists to protect pages [OK]
Common Mistakes:
  • Redirecting if session[:user_id] exists (wrong logic)
  • Using wrong session key like :user
  • Redirecting to wrong path on failure
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard