0
0
Expressframework~10 mins

Session-based auth with express-session - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Session-based auth with express-session
Client sends login request
Server checks credentials
Create session and store user info
Send session cookie to client
Client sends requests with cookie
Server reads session from cookie
Allow access
This flow shows how a client logs in, the server creates a session, sends a cookie, and then uses that cookie to authenticate future requests.
Execution Sample
Express
const session = require('express-session');
app.use(session({ secret: 'key', resave: false, saveUninitialized: false }));

app.post('/login', (req, res) => {
  if (req.body.user === 'admin' && req.body.pass === '123') {
    req.session.user = 'admin'; res.send('Logged in');
  } else res.status(401).send('Fail');
});
This code sets up session middleware and a login route that saves user info in the session on success.
Execution Table
StepActionInputSession StateResponse to Client
1Client sends POST /login with user=admin, pass=123{user:'admin', pass:'123'}{}Waiting
2Server checks credentialsuser=admin, pass=123{}Waiting
3Credentials valid, create sessionN/A{user:'admin'}Set-Cookie header sent
4Send response 'Logged in'N/A{user:'admin'}'Logged in' text
5Client sends GET /dashboard with cookieCookie with session ID{user:'admin'}Waiting
6Server reads session from cookieSession ID{user:'admin'}Waiting
7Session valid, allow accessN/A{user:'admin'}Dashboard content
8Client sends GET /dashboard without cookieNo cookie{}Waiting
9Server finds no sessionN/A{}Redirect to /login
10Execution endsN/AN/ANo session, access denied
💡 Execution stops when client has no valid session cookie or after response sent.
Variable Tracker
VariableStartAfter Step 3After Step 6Final
req.sessionundefined{user:'admin'}{user:'admin'}Depends on request
res.headers['Set-Cookie']undefinedSession cookie setSession cookie sentSession cookie present or absent
client.cookieundefinedCookie with session IDCookie with session IDCookie present or absent
Key Moments - 3 Insights
Why does the server send a cookie after login?
The cookie holds the session ID so the server can recognize the client on future requests, as shown in step 3 and 4 of the execution_table.
What happens if the client sends a request without the session cookie?
The server cannot find the session and denies access, redirecting to login as shown in steps 8 and 9.
Is the user info stored on the client or server?
User info is stored on the server inside the session object, while the client only holds a session ID cookie (step 3 and 6).
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, what is the session state after step 3?
Aundefined
B{}
C{user:'admin'}
D{user:'guest'}
💡 Hint
Check the 'Session State' column at step 3 in the execution_table.
At which step does the server send the session cookie to the client?
AStep 2
BStep 3
CStep 5
DStep 7
💡 Hint
Look for 'Set-Cookie header sent' in the 'Response to Client' column.
If the client sends a request without a cookie, what response does the server give?
ARedirect to /login
BDashboard content
CLogged in text
DFail status 401
💡 Hint
See steps 8 and 9 in the execution_table for requests without cookie.
Concept Snapshot
Use express-session middleware to create sessions.
On login, save user info in req.session.
Server sends a cookie with session ID to client.
Client sends cookie on future requests.
Server reads session from cookie to authenticate.
No cookie means no session, access denied.
Full Transcript
This visual execution shows how session-based authentication works with express-session. When a client logs in with correct credentials, the server creates a session object storing user info and sends a cookie with a session ID back to the client. The client includes this cookie in future requests. The server reads the cookie, finds the session, and allows access. If the client sends no cookie or an invalid one, the server denies access and redirects to login. Variables like req.session and response headers change during these steps, showing how state is maintained securely on the server while the client holds only a session ID cookie.