All 42 projects

42 Project brief

ft_transcendence

An online Pong platform built by a team of five at 1337. I owned the backend for authentication, real-time chat and notifications.

My role
Backend: auth, chat, real-time
Team
5 developers
Timeline
Aug 2023 — Jan 2024
Workload
245 h

The project

ft_transcendence is the final project of the 42 common core: a full-stack web application built as a team, with a lot of freedom in the design as long as the subject’s rules are met.

We built an online Pong platform with live matches and game invites, public, private and password-protected chat channels with admins, bans and mutes, direct messages, friends, profiles with match history and a ranking, login with 42 and two-factor authentication.

The team’s stack

  • Next.js
  • React
  • Tailwind CSS
  • NestJS
  • TypeScript
  • Socket.io
  • PostgreSQL
  • TypeORM
  • Docker

My part

I was the main contributor, with more than 500 commits, and wrote the auth, users, friends and chat modules of the NestJS backend.

01

Login with 42

The OAuth 2.0 authorization-code flow against the 42 API, written by hand.

  • The code is exchanged for an access token on the server, then the 42 profile is fetched and the account created on first login
  • The session is a JWT in an httpOnly cookie, out of reach of page scripts
  • Logout clears the cookie, sets the user offline and drops their tracked sockets
02

Two-factor authentication

Time-based one-time codes that work with any authenticator app.

  • Secrets generated and codes verified with otplib
  • Secrets encrypted at rest with Cryptr (AES-256-GCM), never stored in plain text
  • Turning 2FA on or off needs a valid code, and until the second step passes, every route and socket refuses the session
03

One guard for HTTP and WebSockets

A single NestJS guard protects REST endpoints and Socket.io events with the same rules.

  • Reads the JWT from the cookie on API requests and on the socket handshake
  • Loads the user, enforces the 2FA step and attaches the user to the request
  • Socket errors go through a dedicated exception filter instead of breaking the connection
04

Real-time direct messages

A Socket.io gateway in NestJS, with every message stored in PostgreSQL.

  • Each user joins a personal room, so a message reaches every tab they have open
  • Messages are validated (1 to 500 characters), checked against blocks, saved with TypeORM, then pushed to both sides
  • REST endpoints for the conversation list and for each conversation’s history, in order
05

Presence & notifications

Live online status, and events pushed to the right user.

  • Status tracked per socket: online while at least one tab is connected, offline when the last one closes
  • A shared emitter gateway any module can use to notify a user; the channel features reuse it
  • Friend requests notified in real time when they are sent, accepted or cancelled
06

Accounts, profiles & friends

Registration and everything around the user.

  • Account created from the 42 profile, then a setup step for nickname, display name and avatar
  • Server-side validation with class-validator DTOs; avatars limited to JPEG or PNG under a size cap, stored on the ImageKit CDN
  • Friends (send, accept, reject, cancel, unfriend) and blocking, which also hides direct messages both ways

Login flow

  1. 1

    Sign in with 42

    The backend redirects to the 42 intranet, where the user approves access.

  2. 2

    Callback

    The authorization code is exchanged for an access token and the 42 profile is fetched.

  3. 3

    Account

    New users are created and sent to the setup page; returning users are loaded.

  4. 4

    Session

    A signed JWT is set as an httpOnly cookie, valid for 10 hours.

  5. 5

    Second factor

    If 2FA is on, the session stays locked until a valid code is entered.

  6. 6

    Signed in

    From then on, the same guard checks every API call and socket connection.

What I used

  • NestJS
  • TypeScript
  • Socket.io
  • PostgreSQL
  • TypeORM
  • JWT
  • OAuth 2.0
  • otplib (TOTP)
  • Cryptr
  • class-validator
  • ImageKit

The code lives in a private team repository. I’m happy to walk through it in an interview.