A flexible and plug-and-play authentication module for Node.js applications. Provides features such as OTP-based verification, JWT authentication, email verification, password reset, and user profile management.
β Successfully tested and used in production at: π https://pulsetalk-6lrk.onrender.com
npm i authbackendpackage// index.js or app.js
import express from "express";
import { createAuthModule } from "authbackendpackage";
import userModel from "./models/user.model.js";
import cloudinary from "./lib/cloudinary.js";
const app = express();
const auth = createAuthModule({
userModel,
cloudinaryInstance: cloudinary,
jwtSecret: process.env.JWT_SECRET,
BREVO_API_KEY=your_brevo_api_key_here
BREVO_SENDER_EMAIL=noreply@pulsetalk.com
BREVO_SENDER_NAME=PulseTalk
env: process.env.NODE_ENV,
});Create an account on Cloudinary, generate API credentials, and store them in your .env file.
Cloudinary Instance:
import { config } from "dotenv";
import { v2 as cloudinary } from "cloudinary";
config();
cloudinary.config({
cloud_name: process.env.CLOUDINARY_CLOUD_NAME,
api_key: process.env.CLOUDINARY_API_KEY,
api_secret: process.env.CLOUDINARY_API_SECRET,
});
export default cloudinary;Set a secure JWT_SECRET string in your .env file.
Generate an App Password from your Gmail settings and store it in .env.
π Follow this Gmail App Password Guide
import mongoose from 'mongoose';
const userSchema = new mongoose.Schema({
email: { type: String, required: true, unique: true },
name: { type: String, required: true },
password: { type: String, required: true },
profilePicture: { type: String, default: "" },
}, { timestamps: true });
const User = mongoose.model('User', userSchema);
export default User;app.post("/api/send-otp", auth.sendOtp);
app.post("/api/verify-otp", auth.verifyOTP);
app.post("/api/signup", auth.signup);
app.post("/api/login", auth.login);
app.post("/api/logout", auth.logout);
app.put("/api/update-profile", auth.updateProfile);
app.get("/api/check-auth", auth.checkAuth);
app.post("/api/forgot-password", auth.forgotPassword);import jwt from "jsonwebtoken";
import user from "../models/user.model.js";
import dotenv from "dotenv";
dotenv.config();
export const protectRoute = async (req, res, next) => {
try {
const token = req.cookies.jwt;
if (!token) {
return res.status(401).json({ message: "Not authorized - No token provided" });
}
const decoded = jwt.verify(token, process.env.JWT_SECRET || "shreyash5506");
const foundUser = await user.findById(decoded.userId).select("-password");
if (!foundUser) {
return res.status(401).json({ message: "Not authorized - User not found" });
}
req.user = foundUser;
next();
} catch (error) {
console.error("Auth middleware error:", error);
res.status(401).json({ message: "Not authorized - Invalid token" });
}
}- β OTP verification via email (SMTP)
- β Signup with verified OTP
- β Secure login with JWT
- β Profile update with image support (Cloudinary)
- β Forgot password with bcrypt
- β Cookie-based logout
- β Middleware-ready routes
BREVO_API_KEY=your_brevo_api_key_here
BREVO_SENDER_EMAIL=[email protected]
BREVO_SENDER_NAME=PulseTalk
JWT_SECRET=your-secret-key
NODE_ENV=development
CLOUDINARY_CLOUD_NAME=your-cloud-name
CLOUDINARY_API_KEY=your-api-key
CLOUDINARY_API_SECRET=your-api-secretPOST /api/send-otp
Content-Type: application/json
{
"email": "[email protected]"
}POST /api/verify-otp
Content-Type: application/json
{
"email": "[email protected]",
"otp": "123456"
}POST /api/signup
Content-Type: application/json
{
"email": "[email protected]",
"password": "your-password",
"name": "User Name"
}POST /api/login
Content-Type: application/json
{
"email": "[email protected]",
"password": "your-password"
}PUT /api/update-profile
Content-Type: application/json
{
"name": "New Name",
"profilePicture": "base64encodedImageOrUrl"
}POST /api/forgot-password
Content-Type: application/json
{
"email": "[email protected]",
"newPassword": "new-secure-password"
}Authentication is done using httpOnly cookies which automatically expire after 7 days for enhanced security.
β Successfully running on: π https://pulsetalk-6lrk.onrender.com
Licensed under Apache-2.0.
Built with β€οΈ by the Shreyash Team