What if your users never had to log in twice, and you didn't have to write tricky cookie code?
Why Remember me functionality in Ruby on Rails? - Purpose & Use Cases
Imagine you have to ask users to log in every single time they visit your website, even if they just closed the tab a minute ago.
They have to type their username and password repeatedly, which feels annoying and slow.
Manually handling login persistence means writing complex code to store and check cookies securely.
This is easy to get wrong, leading to security risks or users getting logged out unexpectedly.
Remember me functionality in Rails automatically manages secure cookies to keep users logged in across visits.
This saves you from writing error-prone code and improves user experience smoothly.
if cookies.signed[:user_id]
@current_user = User.find(cookies.signed[:user_id])
endif user.remembered?
log_in user
endIt enables users to stay logged in safely without typing credentials every time, making your app feel friendly and reliable.
Think about how social media sites keep you logged in for days, so you can instantly see updates without logging in again.
Manual login persistence is tricky and risky.
Rails provides built-in support to handle 'remember me' securely.
This improves user experience by keeping users logged in smoothly.
Practice
remember me functionality in a Rails application?Solution
Step 1: Understand the purpose of remember me
The remember me feature is designed to keep users logged in even after they close their browser by saving a cookie.Step 2: Identify the correct description
Options B, C, and D describe unrelated features like password reset, forced logout, or data encryption, which are not the purpose of remember me.Final Answer:
To keep users logged in by saving a cookie on their browser -> Option BQuick Check:
Remember me = save cookie to stay logged in [OK]
- Confusing remember me with password reset
- Thinking it logs out users quickly
- Assuming it encrypts database data
Solution
Step 1: Understand permanent signed cookies
Permanent signed cookies in Rails are set withcookies.signedand an expiration far in the future, like 20 years.Step 2: Analyze options
cookies[:user_id] = { value: user.id, expires: 1.hour.from_now } sets a cookie but expires in 1 hour, not permanent. session[:user_id] = user.id uses session, not cookie. cookies.permanent[:user_id] = user.id sets a permanent cookie but not signed, which is less secure.Final Answer:
cookies.signed[:user_id] = { value: user.id, expires: 20.years.from_now } -> Option AQuick Check:
Permanent signed cookie = cookies.signed with long expiry [OK]
- Using session instead of cookies
- Setting short expiry for permanent cookie
- Not signing the cookie for security
def create
user = User.find_by(email: params[:email])
if user&.authenticate(params[:password])
if params[:remember_me] == '1'
cookies.permanent.signed[:user_id] = user.id
else
session[:user_id] = user.id
end
redirect_to root_path
else
render :new
end
endWhat happens if the user checks the 'remember me' box?
Solution
Step 1: Check the remember_me condition
Ifparams[:remember_me] == '1', the code setscookies.permanent.signed[:user_id] = user.id, which saves a permanent signed cookie.Step 2: Understand cookie effect
This cookie keeps the user logged in even after closing the browser, unlike session which clears on close.Final Answer:
User ID is saved in a permanent signed cookie to keep logged in -> Option DQuick Check:
Remember me checked = permanent signed cookie set [OK]
- Thinking session is used when remember me is checked
- Assuming password is saved in cookie
- Believing user logs out immediately
def destroy session.delete(:user_id) cookies.delete(:user_id) redirect_to login_path end
Solution
Step 1: Check what is deleted
The method deletessession[:user_id]andcookies[:user_id], but the remember me cookie is usually a signed cookie.Step 2: Understand cookie deletion
Deletingcookies[:user_id]does not removecookies.signed[:user_id]. The signed cookie remains, so user stays logged in.Final Answer:
It deletes the session but forgets to delete the signed cookie -> Option CQuick Check:
Logout must delete signed cookie too [OK]
- Deleting only session but not signed cookie
- Deleting wrong cookie key
- Not redirecting after logout
Solution
Step 1: Choose secure cookie with expiry
Usingcookies.signed[:user_id]with a 2 weeks expiry ensures the cookie is secure and expires reasonably.Step 2: Invalidate cookie on logout
Deleting this cookie on logout prevents unauthorized access after user logs out.Final Answer:
Set cookies.signed[:user_id] with 2 weeks expiry and delete it on logout -> Option AQuick Check:
Secure signed cookie + expiry + delete on logout = best practice [OK]
- Storing password in cookie (insecure)
- Using session only (no persistence after close)
- Using unsigned or permanent cookies without expiry
Ready to prove it?
Pass the test and this pattern moves unknown
