flex-auth-core
Version:
Flexible Node.js authentication module supporting multiple credential strategies.
191 lines (129 loc) โข 4.59 kB
Markdown
# flex-auth-core
A flexible, plug-and-play authentication middleware for Node.js & Express. Supports multiple credential strategies like:
- Email + Password
- Email + Username + Password
- Mobile + Username + Password
- Google OAuth
- GitHub OAuth
Designed for developers who want to drop in secure, customizable authentication logic into any Node.js backend.
## ๐ Table of Contents
- [๐ฆ Installation](#-installation)
- [โจ Features](#-features)
- [๐ Docs & Community](#-docs--community)
- [โก Quick Start](#-quick-start)
- [๐ Authentication Methods](#-authentication-methods)
- [๐งช Running Tests](#-running-tests)
- [๐ก Philosophy](#-philosophy)
- [๐งฉ Examples](#-examples)
- [๐ Contributing to flex-auth-core](#-contributing-to-flex-auth-core)
- [๐จโ๐ฌ TC (Technical Committee)](#-tc-technical-committee)
- [๐ License](#-license)
## ๐ฆ Installation
```bash
npm install flex-auth-core
```
Or from GitHub:
```bash
npm install git+https://github.com/AdarshYadav04/flex-auth.git
```
## โจ Features
- ๐ Multiple auth strategies: email, username, mobile
- ๐ Built-in OAuth login via Google and GitHub
- ๐ง Configurable with JWT and MongoDB
- ๐ Pluggable design for adding OTP in future versions
- ๐ฆ Easy Express integration
- ๐งฐ Lightweight, fast, and dependency-friendly
## ๐ Docs & Community
- Raise issues or ideas via [GitHub Issues](https://github.com/AdarshYadav04/flex-auth/issues)
## โก Quick Start
```js
const express = require('express');
const flexAuth = require('flex-auth-core');
require('dotenv').config();
const app = express();
app.use(express.json());
const auth = flexAuth({
jwtSecret: process.env.JWT_SECRET,
mongoUri: process.env.MONGO_URI,
enableOAuth: true
});
app.post('/register', (req, res) => auth.register({ ...req, method: req.body.method }, res));
app.post('/login', (req, res) => auth.login({ ...req, method: req.body.method }, res));
// Enable OAuth routes
auth.useOAuth(app);
app.listen(3000, () => console.log('Auth running at http://localhost:3000'));
```
## ๐ Authentication Methods
### ๐ Credential-Based
- **email-password**
- **email-username-password**
- **mobile-username-password**
Each request to `/register` or `/login` must include a `method` field and the relevant credentials in the request body.
### ๐ OAuth-Based
#### โ
Google Login
- Visit: `GET /auth/google`
- Callback: `GET /auth/callback/google`
#### โ
GitHub Login
- Visit: `GET /auth/github`
- Callback: `GET /auth/callback/github`
๐ฆ On success, youโll receive a JWT token:
```json
{
"message": "OAuth login success",
"token": "your.jwt.token"
}
```
Add the following to your `.env`:
```env
//Required
JWT_SECRET=your_jwt_secret_here
MONGO_URI=mongodb+srv://your_user:your_pass@cluster.mongodb.net/your_db
// Only if using OAuth
GOOGLE_CLIENT_ID=your_google_client_id
GOOGLE_CLIENT_SECRET=your_google_client_secret
GITHUB_CLIENT_ID=your_github_client_id
GITHUB_CLIENT_SECRET=your_github_client_secret
OAUTH_CALLBACK=http://localhost:3000/auth/callback
CLIENT_BASE_URL=http://localhost:5173
```
Ensure the callback URLs are registered in Google/GitHub developer settings:
- Google: `http://localhost:3000/auth/callback/google`
- GitHub: `http://localhost:3000/auth/callback/github`
## ๐งช Running Tests
```bash
npm test
```
*Tests are currently basic and will be extended in future versions with Jest/Mocha.*
## ๐ก Philosophy
- Minimal configuration, maximum flexibility
- Secure by design using bcrypt and JWT
- Easy to extend with additional auth methods (e.g. Google, GitHub, OTP)
- Zero vendor lock-in
## ๐งฉ Examples
Example usage in a standalone Express app:
- See `example-app.js` in the repo
## ๐ Contributing to flex-auth-core
We welcome contributions! To get started:
1. Fork the repo
2. Create a new branch: `git checkout -b feature/your-feature-name`
3. Make changes and commit: `git commit -m 'Add XYZ feature'`
4. Push to your fork: `git push origin feature/your-feature-name`
5. Create a Pull Request
## ๐จโ๐ฌ TC (Technical Committee)
The initial maintainer and current decision-maker:
- **Adarsh Yadav** โ [@AdarshYadav04](https://github.com/AdarshYadav04)
Future contributors and collaborators welcome.
## ๐ License
MIT ยฉ [Adarsh Yadav](https://github.com/AdarshYadav04)