Menu

# Week 10 -- Task 2: JWT Authentication and Route Protection in NestJS
πŸ“°

# Week 10 -- Task 2: JWT Authentication and Route Protection in NestJS

DEV Community: nodeΒ·koushikmayaΒ·about 3 hours ago
#elVCaOOX
#devtonodejs#devto#dev#user#task
Reading 0:00
15s threshold

Introduction

In Week 10 Task 1, I connected a NestJS Tasks API to PostgreSQL using

TypeORM. The API could create users and tasks, but it did not know who

was making a request. Anyone who could reach the task endpoints could

access the available task data.

Task 2 builds on the same idea and adds authentication and user-level

authorization .

I created a separate runnable project so the first task remains

unchanged:

week-10/Task-02/nest-jwt-auth-api/

The goal is simple: users should register, log in, receive a JWT access

token, and manage only their own tasks.

What this API does

Register a new account.

Hash passwords before saving them.

Log in using email and password.

Issue a signed JWT access token.

Verify bearer tokens using Passport and a JWT strategy.

Protect all task routes with a NestJS guard.

Scope every task operation to the authenticated user's ID.

Validate request bodies using DTOs and a global ValidationPipe .

The project intentionally does not include refresh tokens,…

Continue reading β€” create a free account

Join HashtagPLUS to read full articles, follow hashtags, vote, and join the conversation.