This project is a robust, full-stack authentication system developed using Node.js, Express, MongoDB, and JSON Web Tokens (JWT). It features user registration, secure authentication, and role-based access control. The system is designed with security best practices in mind, ensuring user data protection and secure authentication flows.
https://registration-form-17qr.onrender.com/
- Secure User Registration
- JWT Authentication
- Protected Routes
- Role-Based Access Control (RBAC)
- Error Handling
- Environment Configuration
-
Navigate to http://localhost:3000 to access the registration form.
-
Register a new user and log in using the credentials.
-
Access protected routes to test authentication and authorization.
POST /api/register
{
"firstName": "John",
"lastName": "Doe",
"gender": "male",
"email": "john.doe@example.com",
"password": "password123",
}
{
"success": true,
"message": "Successfully registered",
"newUser": {
"firstName": "John",
"lastName": "Doe",
"gender": "male",
"email": "john.doe@example.com",
"password": "$2b$10$...",
"createdAt": "2023-12-12T00:00:00.000Z",
"updatedAt": "2023-12-12T00:00:00.000Z",
"_id": "60c72b2f9b1d4c3a6d8d4f5b"
}
}
GET /users
{
"Authorization": "Bearer your_jwt_token"
}
{
"message": "You have accessed a protected route"
}
Set up environment variables:
Create a .env file in the root directory.
Add the following variables:
MONGO_URI
: your_mongodb_uri
JWT_SECRET
: your_jwt_secret
Clone the project
git clone https://github.com/sauravkamble5/registration-form.git
Go to the project directory
cd registration-form
Install dependencies
npm install
Start the server
npm run dev
registration-form/
├── config/
│ └── db.js # Database connection setup
├── models/
│ └── User.js # User model schema
├── routes/
│ ├── authRoutes.js # Authentication routes
│ └── protectedRoutes.js # Routes requiring authentication
├── middleware/
│ └── authMiddleware.js # Authentication and authorization middleware
├── pages/
│ ├── register.html # Registration form page
│ └── errorPage.html # Error page
├── .env # Environment variables
├── .gitignore # Ignored files
├── index.js # Main server file
├── package.json # Project metadata and dependencies
└── README.md # Project documentation
Contributions are always welcome!
1. Fork the repository.
2. Create a new branch (git checkout -b feature-branch).
3. Make your changes.
4.Commit your changes (git commit -am 'Add new feature').
5.Push to the branch (git push origin feature-branch).
6.Create a new Pull Request.