feat: add cookie-based auth and user management APIs#1
Open
sankara-sabapathy wants to merge 1 commit into
Open
feat: add cookie-based auth and user management APIs#1sankara-sabapathy wants to merge 1 commit into
sankara-sabapathy wants to merge 1 commit into
Conversation
Introduce JWT login/refresh flows, role-based authorization, and CRUD endpoints for users so the backend can manage authentication without relying on the template's placeholder user schema. Co-authored-by: Cursor <cursoragent@cursor.com>
hadron43
self-requested a review
July 27, 2026 05:44
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
This PR implements a token server and user management layer on top of the existing Express + Mongoose template. It replaces the placeholder user schema with a production-oriented Users/Roles design and adds cookie-based JWT authentication with role-based authorization.
Authentication (
/v1/login,/v1/refresh,/v1/me)/v1/login— validates email/password with bcrypt, blocks inactive/suspended accounts, updateslastLoginAt, and sets httpOnlyaccess_token+refresh_tokencookies/v1/refresh— verifies the refresh cookie and re-issues both tokens/v1/me— returns the authenticated user's profile with populated role (requires access cookie)Token strategy: cookie-only (no Bearer header). Access tokens use
accessJwtSecret; refresh tokens userefreshJwtSecret. Both carry{ userId }in the JWT payload.User management (
/v1/users)/v1/users— paginated user list with optionalstatusfilter (requiresusers.readpermission)/v1/users— create user with hashed password (requiresusers.writepermission)/v1/users/:id— partial update for name, email, password, role, status (requiresusers.writepermission)Data model
Users (
src/models/users.js):firstName,lastName,fullName(auto-computed),email(unique),passwordHash,roleId,status(active/inactive/suspended),lastLoginAt,createdBy,updatedBy, soft-delete viaisDeletedRoles (
src/models/roles.js):name,code(unique),description,permissions[],statusAuthorization changes
authenticateByCookienow verifies the access cookie (access_token) instead of the refresh cookieauthorizeInternalAccessloads permissions from the user's role in the database; users withadminrole bypass permission checksTooling & config
bcryptjsscripts/seedRoles.jsadminanduserroles + bootstrap admin usernpm run seednpm run dev:localnpm run test:local.env.exampleREFRESH_TOKEN_VALIDITY_IN_SECONDS,SEED_ADMIN_EMAIL,SEED_ADMIN_PASSWORD.gitignore.env.localBreaking changes
usersschema (name,username,admin,permissions[]) is replaced with the new design. Existing user documents will not be compatible.authorizeInternalAccessnow require role-based permissions from therolescollection instead of the inlineadminflag.Test plan
npm run test:local— all 6 endpoints pass (login, me, refresh, list users, create user, patch user)npm run dev:localconfirmed login and/meresponsesnpm run seed)/v1/userswithoutusers.read/users.writepermissionsLocal setup
Default seed credentials:
admin@example.com/ value ofSEED_ADMIN_PASSWORD