Skip to content

feat: implement JWT authentication and authorization flow#164

Open
pranshugarg637 wants to merge 2 commits into
Dev-Card:mainfrom
pranshugarg637:feat/authentication-and-protected-routes
Open

feat: implement JWT authentication and authorization flow#164
pranshugarg637 wants to merge 2 commits into
Dev-Card:mainfrom
pranshugarg637:feat/authentication-and-protected-routes

Conversation

@pranshugarg637
Copy link
Copy Markdown

🚀 Overview

This PR implements a complete backend authentication and authorization flow for DevCard with secure authentication practices and protected route support.


✨ Features Added

👤 Authentication APIs

Implemented the following APIs:

  • POST /auth/register
  • POST /auth/login
  • Updated POST /auth/logout flow compatibility

🛡️ Security Enhancements

Added secure backend authentication practices including:

  • Password hashing using bcrypt
  • JWT-based authentication
  • Reusable secure auth cookie helper
  • Environment-based secure cookie configuration
  • Proper validation using Zod schemas
  • Structured validation and authentication error handling

🔑 Authorization & Protected Routes

Implemented middleware and typing support for:

  • Protected/private API routes
  • Authenticated user access
  • Unauthorized access handling
  • Shared app.authenticate route protection flow

🗄️ Database Changes

Prisma Schema

Added:

  • passwordHash field to User model

Migration

Created migration:

  • 20260517000000_add_password_auth

🧪 Test Coverage

Added authentication tests covering:

  • Successful registration
  • Duplicate email handling
  • Successful login
  • Invalid credentials
  • Unauthorized protected route access

All backend tests and builds are passing successfully.


📁 Files Added / Updated

Updated Files

  • apps/backend/package.json
  • apps/backend/prisma/schema.prisma
  • apps/backend/src/routes/auth.ts
  • apps/backend/src/routes/cards.ts
  • apps/backend/src/routes/connect.ts
  • apps/backend/src/routes/follow.ts
  • apps/backend/src/routes/public.ts
  • apps/backend/src/utils/validators.ts
  • apps/backend/src/types/fastify.d.ts
  • pnpm-lock.yaml
  • pnpm-workspace.yaml

Added Files

  • apps/backend/prisma/migrations/20260517000000_add_password_auth/migration.sql
  • apps/backend/src/__tests__/auth.test.ts

✅ Verification

  • Backend tests passing
  • Backend build passing
  • Prisma migration verified
  • JWT auth flow tested
  • Route protection verified

@Harxhit Harxhit added the gssoc:approved Required label for every approved PR. Gives the base +50 points and enables contribution tracking. label May 19, 2026
@Harxhit
Copy link
Copy Markdown
Collaborator

Harxhit commented May 19, 2026

@pranshugarg637 Please add the issue it closes.

@pranshugarg637
Copy link
Copy Markdown
Author

@Harxhit Pls help there is some issue

@pranshugarg637
Copy link
Copy Markdown
Author

@Harxhit After adding some of the features i tried to create a pull request but it is showing this
(refer image)
Screenshot 2026-05-19 111801

@Harxhit
Copy link
Copy Markdown
Collaborator

Harxhit commented May 19, 2026

@Harxhit After adding some of the features i tried to create a pull request but it is showing this (refer image) Screenshot 2026-05-19 111801

There is nothing wrong with it. Please wait for the review process to be completed.

@Harxhit Harxhit added the critical Includes schema, architecture, or other critical core functionality changes. label May 19, 2026
@ShantKhatri
Copy link
Copy Markdown
Contributor

Need some more time for this PR. Thanks!

Copy link
Copy Markdown
Collaborator

@Harxhit Harxhit May 20, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Revert this file please.

accentColor String @default("#6366f1") @map("accent_color")
provider String
providerId String @map("provider_id")
passwordHash String? @map("password_hash")
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why did you made it optional?

const PASSWORD_SALT_ROUNDS = 12;

export async function authRoutes(app: FastifyInstance) {
app.post('/register', async (request: FastifyRequest, reply: FastifyReply) => {
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Request schema needed

}

const { email, username, displayName, password } = parsed.data;
const existingUser = await app.prisma.user.findFirst({
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Error handling can be better.

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Any breaks the type safety

layer: 'api',
},
}).catch(err => app.log.error('Failed to log follow:', err));
}).catch((err: unknown) => app.log.error({ err }, 'Failed to log follow'));
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Use instance off more better approach.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

critical Includes schema, architecture, or other critical core functionality changes. gssoc:approved Required label for every approved PR. Gives the base +50 points and enables contribution tracking.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants