nextjs-auth-kit v1.2.1
Next.js Auth Setup
A comprehensive authentication setup package for Next.js applications that provides complete authentication functionality, including Google OAuth, email verification, and password management.
Note: Use TypeScript and src folder while creating a new next application
Features
- 🔐 Complete authentication system
- 🌐 Google OAuth integration
- ✉️ Email verification with OTP
- 🔑 Password management with validation
- 📱 Responsive UI components
- 🎯 TypeScript support
- 🗄️ MongoDB integration
- 🛡️ Protected routes with middleware
Installation
First install a nextjs project
npx create-next-app@latest
Note: Use TypeScript and src directory while creating a new next application
Then install the package
npm i nextjs-auth-kit
or
npm i nextjs-auth-kit --legacy-peer-deps
Prerequisites
Ensure you have the following before getting started:
- Node.js and npm installed
- MongoDB database (local or cloud instance)
- Google OAuth credentials (Client ID and Secret)
- SMTP email service credentials (e.g., Gmail, SendGrid, etc.)
Setup Steps
1. Install the Package
Run the following command in your Next.js project:
npm i nextjs-auth-kit
or
npm i nextjs-auth-kit --legacy-peer-deps
2. Configure Environment Variables
Edit the .env
file in your project root and add the following:
MONGODB_URI=your_mongoDB_URI
JWT_SECRET=your_jwt_secret
GOOGLE_CLIENT_ID=your_google_client_id
GOOGLE_CLIENT_SECRET=your_google_client_secret
NEXTAUTH_URL=http://localhost:3000
NEXTAUTH_SECRET=your_nextauth_secret
EMAIL_USER=your_email_user
EMAIL_PASSWORD=your_email_password
NEXT_PUBLIC_APP_URL=http://localhost:3000
Refer this link for Google client and secret id
https://www.balbooa.com/help/gridbox-documentation/integrations/other/google-client-id
Refer this link for Gmail App Password
https://www.getmailbird.com/gmail-app-password/
3. Start Your Development Server
Run the development server with:
npm run dev
Authentication Flow
Email/Password Signup
- User enters email and password.
- Password validation checks:
- Minimum 8 characters
- At least one uppercase letter
- At least one lowercase letter
- At least one number
- At least one special character
- OTP is sent to the provided email for verification.
- Upon successful verification, the user is redirected to the dashboard.
Google OAuth
- User clicks "Continue with Google."
- Google login dialog appears for authorization.
- Upon success:
- New users are automatically registered.
- Existing users are linked to their Google account.
- The user is redirected to the dashboard.
Security Features
- Password Hashing: Secure passwords using bcrypt.
- OTP Expiration: Automatically invalidates OTP after a set time.
- Protected API Routes: Ensures restricted access via middleware.
- Session Management: Secure sessions with NextAuth.js.
- CSRF Protection: Prevents cross-site request forgery attacks.
Contributing
To contribute to the project:
- Fork the repository.
- Create a feature branch:
git checkout -b feature-name
- Commit your changes:
git commit -m "Add feature description"
- Push to the branch:
git push origin feature-name
- Create a Pull Request.
License
This project is licensed under the MIT License.
Authors
- Sany
- Kaviyarasan
Support
For any questions or support, you can:
- Open an issue on GitHub.
- Connect on LinkedIn:
Acknowledgments
Special thanks to the following technologies:
- Next.js
- NextAuth.js
- MongoDB
- Tailwind CSS