Challenge - 5 Problems
Authentication Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ component_behavior
intermediate2:00remaining
What happens when a user tries to access a protected page without logging in?
Consider a Rails app with a before_action that requires authentication for certain pages. What will the user see if they try to visit a protected page without being logged in?
Ruby on Rails
class DashboardController < ApplicationController before_action :authenticate_user! def index render plain: "Welcome to your dashboard" end end
Attempts:
2 left
💡 Hint
Think about what authenticate_user! does before the action runs.
✗ Incorrect
The authenticate_user! method checks if the user is logged in. If not, it redirects them to the login page. This prevents unauthorized access to protected pages.
❓ state_output
intermediate2:00remaining
What is the value of current_user after a successful login?
In a Rails app using Devise, after a user logs in successfully, what does the helper method current_user return?
Ruby on Rails
user = User.create(email: "test@example.com", password: "password") # After logging in with these credentials current_user
Attempts:
2 left
💡 Hint
current_user is a helper method provided by Devise.
✗ Incorrect
After login, current_user returns the User object for the logged-in user. This lets the app know who is using it.
📝 Syntax
advanced2:30remaining
Which code snippet correctly restricts access to admin users only?
You want to allow only admin users to access the admin dashboard. Which before_action code correctly enforces this?
Attempts:
2 left
💡 Hint
Think about where to put the admin check and how to use before_action properly.
✗ Incorrect
Option C uses a separate method check_admin called by before_action. It redirects non-admin users safely before the action runs.
🔧 Debug
advanced2:30remaining
Why does this authentication code raise an error?
This code is meant to authenticate users but raises an error. What is the cause?
Ruby on Rails
class ApplicationController < ActionController::Base before_action :authenticate_user def authenticate_user unless current_user redirect_to login_path return end end end
Attempts:
2 left
💡 Hint
Think about what happens after redirect_to is called.
✗ Incorrect
Without return, the action continues running after redirect_to, which can cause errors or unexpected behavior.
🧠 Conceptual
expert2:00remaining
Why is authentication critical for application security?
Which statement best explains why authentication is essential to secure a web application?
Attempts:
2 left
💡 Hint
Think about what authentication does before allowing access.
✗ Incorrect
Authentication confirms who the user is. This stops strangers from accessing private parts of the app, protecting data and functionality.