A full-stack authentication system built with Next.js 14, TypeScript, and MongoDB. This application provides secure user registration, login, and password recovery functionality with email-based OTP verification.
- User Registration: Secure account creation with form validation
- User Login: JWT-based authentication with persistent sessions
- Password Recovery: Email-based OTP system for password reset
- Protected Routes: Middleware-based route protection
- Responsive Design: Mobile-first UI built with Tailwind CSS
- Type Safety: Full TypeScript implementation
- Real-time Notifications: Toast notifications for user feedback
- Loading States: Smooth loading indicators with React Spinners
- Next.js 14 - React framework with App Router
- TypeScript - Type-safe JavaScript
- Tailwind CSS - Utility-first CSS framework
- React Context - Global state management
- React Toastify - Toast notifications
- FontAwesome - Icon library
- Lora Font - Google Fonts integration
- Next.js API Routes - Serverless API endpoints
- MongoDB - NoSQL database with Mongoose ODM
- JWT - JSON Web Tokens for authentication
- bcryptjs - Password hashing
- Nodemailer - Email service for OTP delivery
- Joi - Data validation
src/
├── app/ # Next.js App Router
│ ├── api/ # API routes
│ │ ├── login/ # Login endpoint
│ │ ├── register/ # Registration endpoint
│ │ └── forgot-password/
│ │ ├── sendotp/ # OTP generation
│ │ └── setpassword/ # Password reset
│ ├── login/ # Login page
│ ├── register/ # Registration page
│ └── forgot-password/ # Password recovery page
├── components/ # Reusable UI components
│ ├── formElements/ # Form input components
│ ├── loader/ # Loading components
│ ├── Navbar.tsx # Navigation component
│ └── Notification.tsx # Toast notification wrapper
├── context/ # React Context providers
├── database/ # MongoDB connection
├── middleware/ # Authentication middleware
├── models/ # Mongoose schemas
│ ├── user.model.tsx # User schema
│ └── otp.model.tsx # OTP schema
├── service/ # API service functions
├── types/ # TypeScript type definitions
└── utils/ # Utility functions
- Node.js 18+
- MongoDB database
- SMTP email service (Gmail, SendGrid, etc.)
- Clone the repository:
git clone <repository-url>
cd authentication- Install dependencies:
npm install- Set up environment variables:
cp .env.sample .env.local- Configure your
.env.localfile:
MONGODB_URI=mongodb://localhost:27017/authentication
# or MongoDB Atlas connection string
SMTP_HOST=smtp.gmail.com
SMTP_PORT=587
SMTP_MAIL=your-email@gmail.com
SMTP_PASSWORD=your-app-passwordRun the development server:
npm run devOpen http://localhost:3000 to view the application.
npm run build
npm startPOST /api/register- User registrationPOST /api/login- User login
POST /api/forgot-password/sendotp- Send OTP to emailPOST /api/forgot-password/setpassword- Reset password with OTP
| Variable | Description | Required |
|---|---|---|
MONGODB_URI |
MongoDB connection string | Yes |
SMTP_HOST |
SMTP server hostname | Yes |
SMTP_PORT |
SMTP server port | Yes |
SMTP_MAIL |
Email address for sending OTPs | Yes |
SMTP_PASSWORD |
Email password or app password | Yes |
{
name: String,
userName: String,
password: String, // bcrypt hashed
role: String,
timestamps: true
}{
// OTP verification schema
timestamps: true
}- Password hashing with bcryptjs
- JWT token-based authentication
- Input validation with Joi
- Protected API routes
- CORS configuration
- Environment variable protection
We welcome contributions to improve this authentication system! Here's how you can help:
- Fork the repository and clone it locally
- Create a feature branch from
main:git checkout -b feature/your-feature-name
- Make your changes and test thoroughly
- Commit your changes with clear, descriptive messages:
git commit -m "feat: add email verification for registration" - Push to your fork and submit a Pull Request
- Follow TypeScript best practices
- Maintain consistent code formatting
- Add appropriate error handling
- Update documentation for new features
- Test your changes locally before submitting
Found a bug or have a feature request? Please open an issue with:
- Bug Reports: Include steps to reproduce, expected vs actual behavior, and environment details
- Feature Requests: Describe the feature, use case, and potential implementation approach
- Security Issues: Please report security vulnerabilities privately
- 🔐 Enhanced security features (2FA, rate limiting)
- 📧 Email template improvements
- 🎨 UI/UX enhancements
- 📱 Mobile responsiveness improvements
- 🧪 Test coverage expansion
- 📚 Documentation improvements
- 🌐 Internationalization support
Please be respectful and constructive in all interactions. We're here to build something great together!
- 📖 Check the documentation above
- 🐛 Open an issue for bugs
- 💡 Request features with enhancement label
- 💬 Join discussions in the Issues section
This project is private and not licensed for public use.