A secure Authentication and User Management backend built with Node.js, Express, MongoDB, JWT, and bcrypt.
Includes role-based access control (user/admin), password reset via token, and email notifications using Resend.
- User Registration (with hashed passwords)
- User Login (JWT authentication via cookies)
- User Logout (clears cookie)
- Password Reset (forgot/reset flow via email token)
- Role-based Access (user/admin)
- User Management
- Self profile management (view, update, delete account)
- Admin operations (list all users, update role, delete any user)
- Email Notifications
- Welcome email on registration
- Login alert
- Password reset email
- Account deletion email
- API Documentation with Swagger (
/api-docs)
-
Clone the repository:
git clone https://github.com/iamdanwi/auth-user-management-api.git cd auth-user-management-api -
Install dependencies:
npm install
-
Create a
.envfile:PORT=3000 MONGO_URI=mongodb://127.0.0.1:27017/authdb JWT_SECRET=your_jwt_secret JWT_EXPIRES_IN=7d CLIENT_URL=http://localhost:3000 RESEND_API_KEY=your_resend_api_key NODE_ENV=development
-
Start the server:
npm run dev
or
node server.js
-
Visit API Docs:
http://localhost:3000/api-docs
POST /auth/register
curl -X POST http://localhost:3000/auth/register \
-H "Content-Type: application/json" \
-d '{"name":"John Doe","email":"john@example.com","password":"secret123"}'POST /auth/login
curl -X POST http://localhost:3000/auth/login \
-H "Content-Type: application/json" \
-d '{"email":"john@example.com","password":"secret123"}' \
-c cookies.txtPOST /auth/logout
curl -X POST http://localhost:3000/auth/logout -b cookies.txtPOST /auth/forgot-password
curl -X POST http://localhost:3000/auth/forgot-password \
-H "Content-Type: application/json" \
-d '{"email":"john@example.com"}'POST /auth/reset-password/:token
curl -X POST http://localhost:3000/auth/reset-password/<token> \
-H "Content-Type: application/json" \
-d '{"password":"newpassword123"}'curl -X GET http://localhost:3000/users/me -b cookies.txtcurl -X PUT http://localhost:3000/users/me \
-H "Content-Type: application/json" \
-d '{"name":"Johnny Doe","password":"newSecret123"}' \
-b cookies.txtcurl -X DELETE http://localhost:3000/users/me -b cookies.txtcurl -X GET http://localhost:3000/users -b cookies.txtcurl -X GET http://localhost:3000/users/64f1234567890 -b cookies.txtcurl -X PUT http://localhost:3000/users/64f1234567890/role \
-H "Content-Type: application/json" \
-d '{"role":"admin"}' \
-b cookies.txtcurl -X DELETE http://localhost:3000/users/64f1234567890 -b cookies.txt- Welcome Mail β Sent on registration
- Login Alert β Sent on login
- Password Reset β Sent when user requests password reset
- Account Deletion β Sent when account is deleted
Swagger API docs available at:
http://localhost:3000/api-docs
Built by iamdanwi as a beginner-friendly Authentication & User Management API project.
This project is licensed under the MIT License. See the LICENSE file for details.