Exercise 09 worksheet: Does logging out really end the session?
Work only on the supplied local app with fictional accounts. Read safety. You need Node.js 22+, a desktop browser, and a text editor. No packages or previous lesson are required. Allow 75–90 minutes after setup. Record your work in the submission template.
1. Prepare your workspace (10 minutes)
Extract the repository or student kit. In your editor open exercises/09-authentication-failures/student, or app inside a student kit. Open a terminal in that folder (VS Code: Terminal → New Terminal).
node --version
node server.mjs
The version must be 22 or newer. Leave the terminal running and open http://127.0.0.1:3000. Stop any other exercise server first. Do not open index.html directly: the server makes the authentication decisions.
Use only the supplied fictional accounts: alice@campus.example and bob@campus.example, both with password Campus-demo-42!. These intentionally published teaching credentials are not for real services. All sessions disappear when you stop/restart the server. Refreshing a page clears its in-memory token but does not revoke a server session.
2. Observe access after logout (10 minutes)
Start a fresh server if you have experimented already. Use the sign-in form with Alice's fictional email and password.
- Sign in, then request the protected profile. Record the successful response and whose profile it contains.
- Use the capture control to retain a copy of the current token in the replay panel. This is a deliberate local demonstration, not token theft from another system.
- Log out. Observe that the normal browser session is now signed out.
- Request the protected profile normally: access should be rejected without a current token.
- Use the replay control to send the retained token. Do not refresh, restart, or sign in between logout and replay. In the starter, this request still returns the protected profile.
- Record the difference between the normal request and the retained-token replay, including HTTP results. Do not include token values in your submission.
The successful replay proves that the server still trusts the old token. If your replay is already rejected, check that you captured a token, have not restarted, and have not waited for it to expire. Repeat with a fresh login and capture before drawing a conclusion.
3. Understand the session boundary (10 minutes)
| Term | Meaning in this app |
|---|---|
| Login | Checks the fictional email and password before issuing a token. |
| Session token | A random credential representing one signed-in session. Whoever presents it may use that session while it remains valid. |
| Client | The browser, which holds the current token in memory. |
| Session store | The server's in-memory record of valid sessions and their expiry times. |
| Revocation | Removing the current session from that store so its token no longer works. |
| Replay | Sending a retained copy of a token again, through the same API. |
The browser sends Authorization: Bearer <token> with a protected request. The server checks its own store; it does not trust the screen's signed-in label. A second browser tab can hold a separate token for the same account.
Before editing, write your prediction: After logout, the server must reject that session's token even if someone kept a copy. Clearing the browser's token is useful, but does not remove a copy elsewhere. Expiry eventually ends sessions; explicit logout must end the current session immediately.
4. Repair the logout policy (15 minutes)
Save a before-copy of session-policy.mjs outside the app folder. Open the original in your editor. Keep the function name and arguments. The supplied revokeSession helper removes only the session identified by token and safely handles a missing or already-revoked token.
| JavaScript | Meaning |
|---|---|
import { revokeSession } from './session-helpers.mjs'; | Makes the supplied helper available in this file. |
state | The server's current accounts/session state. |
token | The credential presented to logout. |
| A function call | Runs a named operation with values in parentheses. |
return | Ends this function and passes its result back to the caller. |
Use this complete scaffold and fill its single blank with the helper named above:
import { revokeSession } from './session-helpers.mjs';
export function endSession(state, token) {
return __________(state, token);
}
Replace only session-policy.mjs, then save. Do not change the helper, server, UI, or checks to make the demonstration pass. Explain why deleting all sessions or always denying profile access would not be a correct repair.
5. Restart and prove revocation (10 minutes)
Save the policy, stop the server with Ctrl+C, run node server.mjs again, and refresh the page. Restarting alone is not proof of the repair: it clears all sessions even in the starter.
Create your repair evidence in this order, within one uninterrupted server run:
- Log in again with Alice and verify normal profile access succeeds.
- Capture this new token.
- Log out.
- Replay that retained token immediately, without another restart. The repaired server must reject access with HTTP 401.
- Log in again. A fresh session must still read Alice's profile successfully.
Record both the denied replay and the successful new login. A signed-out label, a failed login, or a token captured before the restart does not prove revocation.
6. Verify independent sessions and run checks (10–15 minutes)
Open the app in two browser tabs. Log in as Alice in both; each login creates a different session. In the first tab capture its token, log out and verify its replay is rejected. In the second tab request the protected profile: that independently signed-in session must still work. Repeat with Bob if you want to check a second account. This app defines logout as this session, not “log out everywhere.”
Stop the server with Ctrl+C or open a second terminal in the same app folder. Run:
node checks.mjs
The checks create their own isolated state. They do not inspect or log out your open browser sessions. In the unchanged starter, expect 8 of 10 checks to pass, ending with 8 passed; 2 failed; after repair, expect all 10 checks to pass, ending with 10 passed; 0 failed and exit code 0. The checks also cover expiry without a real-time wait. Record the actual output, not only the expected count.
If you stopped the server, run node server.mjs again before using the browser. A fresh login is then required.
7. Explain and submit (10 minutes)
Complete the submission template. Include the changed policy, the original and repaired replay evidence, a successful fresh login, independent-session evidence, and your actual check output. Explain which server-side state changed and why a copied token is now rejected. Do not submit token strings or real credentials.
Troubleshooting
- Address already in use: stop the other lesson server with Ctrl+C; only one can use port 3000.
- Page cannot connect: keep
node server.mjsrunning and use the exact loopback URL. - Change seems ineffective: save the correct
session-policy.mjs, restart that app's server, refresh, and repeat the full fresh-token sequence. - Replay is denied before repair: capture before logout and replay promptly; restart and expiry also invalidate tokens.
- Syntax error: compare the scaffold's braces, quotes, import path, and helper spelling; do not disable checks.
- Both tabs lose access: ensure you did not restart or clear all server sessions. Logout must target only its own token.
Optional discussion
How would “log out all devices” change the policy? Why might an application revoke sessions after a password reset? How would a production framework manage secure cookies, TLS, expiration, and reauthentication? These are discussion extensions, not required changes or graded features here.