0
0
Ruby on Railsframework~10 mins

Remember me functionality in Ruby on Rails - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Remember me functionality
User logs in
Check 'Remember me' box?
NoCreate session only
Yes
Generate remember token
Store token digest in DB
Store token in cookie
User closes browser
User returns
Check cookie token
Match token with DB digest?
NoAsk to login
Yes
Log user in automatically
This flow shows how the app remembers a user by storing a token in a cookie and matching it with the database to keep the user logged in.
Execution Sample
Ruby on Rails
def remember(user)
  user.remember
  cookies.permanent.signed[:user_id] = user.id
  cookies.permanent[:remember_token] = user.remember_token
end
This code sets a permanent signed cookie with the user ID and a remember token cookie to keep the user logged in.
Execution Table
StepActionToken GeneratedCookie SetDB UpdatedResult
1User logs in and checks 'Remember me'No token yetNo cookiesNo DB changeWaiting for token generation
2Generate remember tokenToken123abcNo cookies yetNo DB changeToken ready
3Store token digest in DBToken123abcNo cookies yetDigest storedDB updated with token digest
4Set cookies with user_id and tokenToken123abcCookies set with user_id and tokenDigest storedCookies saved on browser
5User closes browserToken123abcCookies persist (permanent)Digest storedUser session saved
6User returns, app reads cookiesToken123abcCookies readDigest storedToken retrieved from cookie
7Compare cookie token with DB digestToken123abcCookies readDigest storedTokens match
8Log user in automaticallyToken123abcCookies readDigest storedUser logged in without password
9If tokens did not matchToken123abcCookies readDigest storedUser asked to login manually
💡 Execution stops after user is logged in automatically or asked to login if tokens don't match.
Variable Tracker
VariableStartAfter Step 2After Step 3After Step 4After Step 6Final
remember_tokennilToken123abcToken123abcToken123abcToken123abcToken123abc
cookies[:user_id]nilnilniluser.iduser.iduser.id
cookies[:remember_token]nilnilnilToken123abcToken123abcToken123abc
DB remember_digestnilnilDigest(Token123abc)Digest(Token123abc)Digest(Token123abc)Digest(Token123abc)
Key Moments - 3 Insights
Why do we store a digest of the token in the database instead of the token itself?
Storing the digest protects user security because the actual token is only in the cookie. If the database is leaked, attackers can't use the digest to log in. See step 3 in execution_table where digest is stored, not the token.
What happens if the user clears cookies but the database still has the token digest?
The user will have no token cookie to match, so the app will ask them to log in again. This is shown in step 9 where tokens don't match or cookie is missing.
Why do we use permanent cookies for 'Remember me'?
Permanent cookies stay after the browser closes, so the user stays logged in next time. This is shown in step 5 where cookies persist after closing the browser.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table at step 4. What cookies are set in the user's browser?
AOnly remember_token cookie
BBoth user_id and remember_token cookies
COnly user_id cookie
DNo cookies are set yet
💡 Hint
Check the 'Cookie Set' column at step 4 in execution_table.
At which step does the app compare the token from the cookie with the database digest?
AStep 7
BStep 5
CStep 2
DStep 9
💡 Hint
Look for the step mentioning 'Compare cookie token with DB digest' in execution_table.
If the user does NOT check 'Remember me' at login, what changes in the flow?
AToken is generated but not stored
BToken is stored but cookies are session-only
CNo token is generated and no cookies are set permanently
DUser is logged out immediately
💡 Hint
Refer to the decision after 'Check Remember me box?' in concept_flow.
Concept Snapshot
Remember me functionality in Rails:
- Generate a secure token when user opts in
- Store token digest in DB, token in permanent cookie
- On return, compare cookie token with DB digest
- If match, log user in automatically
- Protects user convenience and security
Full Transcript
This visual execution trace shows how the 'Remember me' feature works in Rails. When a user logs in and checks 'Remember me', the app generates a secure token and stores its digest in the database. It sets permanent cookies with the user ID and token. When the user returns, the app reads the cookies and compares the token with the digest in the database. If they match, the user is logged in automatically without entering credentials again. If not, the user must log in manually. This process balances user convenience with security by storing only the token digest in the database and the actual token in the user's browser cookie.