0
0
NestJSframework~10 mins

Session-based authentication in NestJS - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Session-based authentication
User sends login request
Server verifies credentials
Yes
Server creates session
Session ID stored in cookie
User sends requests with cookie
Server checks session ID
Server grants access
End
No
Server denies access
End
User logs in, server checks credentials, creates a session, stores session ID in cookie, then checks session on each request to allow or deny access.
Execution Sample
NestJS
import { Controller, Post, Req, Res } from '@nestjs/common';
import * as session from 'express-session';

@Controller('auth')
export class AuthController {
  @Post('login')
  login(@Req() req, @Res() res) {
    if (req.body.user === 'admin' && req.body.pass === '123') {
      req.session.user = 'admin';
      res.send('Logged in');
    } else {
      res.status(401).send('Unauthorized');
    }
  }
}
This code checks user credentials, creates a session if valid, and sends a response.
Execution Table
StepActionInputSession StateResponse
1User sends login request{user:'admin', pass:'123'}{}Pending
2Server checks credentialsuser='admin', pass='123'{}Pending
3Credentials valid?Yes{}Pending
4Create sessionSet req.session.user='admin'{user:'admin'}Pending
5Send responseSession created{user:'admin'}'Logged in' with Set-Cookie
6User sends request with cookieCookie with session ID{user:'admin'}Pending
7Server checks session IDSession ID valid{user:'admin'}Pending
8Grant accessSession valid{user:'admin'}Access granted
9User sends request without valid sessionNo or invalid cookie{}Pending
10Deny accessSession invalid{}401 Unauthorized
💡 Execution stops when server grants or denies access based on session validity.
Variable Tracker
VariableStartAfter Step 4After Step 6Final
req.session{}{user:'admin'}{user:'admin'}{} or {user:'admin'} depending on request
ResponseNonePendingPending'Logged in' or '401 Unauthorized'
Key Moments - 3 Insights
Why does the server store user info in req.session instead of sending it back directly?
Storing user info in req.session keeps the user logged in across requests without resending credentials. See execution_table step 4 where session is created and step 6 where session is checked.
What happens if the user sends a request without the session cookie?
The server cannot find a valid session and denies access, as shown in execution_table steps 9 and 10.
How does the server know which session belongs to which user?
The server uses the session ID stored in the cookie sent by the user. This ID links to the session data on the server, as shown in execution_table step 7.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution table, what is the session state after step 4?
A{user:'admin'}
Bnull
C{}
D{user:'guest'}
💡 Hint
Check the 'Session State' column at step 4 in the execution_table.
At which step does the server deny access due to invalid session?
AStep 6
BStep 10
CStep 7
DStep 8
💡 Hint
Look for '401 Unauthorized' in the 'Response' column in execution_table.
If the user sends a request without valid session, which step shows the server response?
AStep 5
BStep 3
CStep 10
DStep 2
💡 Hint
Check where the server sends '401 Unauthorized' response in execution_table.
Concept Snapshot
Session-based authentication in NestJS:
- User sends login with credentials
- Server verifies credentials
- If valid, server creates session and stores user info
- Session ID sent in cookie to client
- Client sends cookie with each request
- Server checks session ID to allow or deny access
- Sessions keep user logged in without resending password
Full Transcript
Session-based authentication in NestJS works by the user sending a login request with credentials. The server checks these credentials. If they are correct, the server creates a session and stores user information in it. The server sends a session ID back to the user in a cookie. For every following request, the user sends this cookie. The server uses the session ID from the cookie to find the session data and verify the user is logged in. If the session is valid, the server grants access. If not, it denies access. This way, the user stays logged in without sending the password every time.