Auth with Password-less
npm install @jehankandy/auth-core-dbbash
npm install @jehankandy/auth-core-db
`
- No additional OTP or email core packages are required.
š§ Environment Variables
- Create or update a .env file in your root backend folder:
`env
PROJECT_NAME=MyProject
JWT_SECRET=your_jwt_secret
MONGO_URI=mongodb://localhost:27017/yourdb
EMAIL_USER=your-email@gmail.com
EMAIL_PASSWORD=your-app-password
PROJECT_NAME="your-project-name"
`
šļø MongoDB (Required)
- MongoDB must be connected before using this package
- The package does not create models automatically
š Required Project Structure
- Your backend must follow this structure:
`pgsql
root-backend/
āāā models/
ā āāā role.model.js
ā āāā user.model.js
ā āāā userlog.model.js
ā āāā userotp.model.js
ā
āāā routes/
ā āāā auth.route.js
ā
āāā .env
āāā app.js / server.js
`
š§© Required Mongoose Models (MANDATORY)
- These models are NOT included in the npm package.
- They must exist in your backend under models/.
$3
`js
const mongoose = require("mongoose");
const roleSchema = new mongoose.Schema({
name: { type: String, required: true, unique: true },
permissions: [{ type: String }],
}, { timestamps: true });
module.exports = mongoose.model("Role", roleSchema);
`
$3
- Your database must contain a role record with:
`json
{
"name": "user"
}
`
Example valid roles:
- admin
- developer
- user ā
(required)
$3
`js
const mongoose = require('mongoose');
const UserSchema = new mongoose.Schema({
fullName: { type: String, trim: true },
username: { type: String, unique: true, lowercase: true },
email: { type: String, required: true, unique: true, lowercase: true },
role: { type: mongoose.Schema.Types.ObjectId, ref: "Role", required: true },
isActive: { type: Boolean, default: true },
login_attempt: { type: Number, default: 0 },
lastLoginAttemptAt: { type: Date },
lastLogin: Date,
});
module.exports = mongoose.model('User', UserSchema);
`
$3
`js
const mongoose = require('mongoose');
const UserlogsSchema = new mongoose.Schema({
user: {
type: mongoose.Schema.Types.ObjectId,
ref: 'User',
required: true
},
action: {
type: String,
required: true,
default: 'other'
},
description: {
type: String,
trim: true
},
ipAddress: String,
userAgent: String,
metadata: {
type: Object,
default: {}
}
}, { timestamps: true });
module.exports = mongoose.model('Userlogs', UserlogsSchema);
`
$3
`js
const mongoose = require('mongoose');
const UserOTPSchema = new mongoose.Schema({
email: { type: String, required: true },
otp: { type: String, required: true },
createdAt: {
type: Date,
default: Date.now,
expires: 900
}
}, { timestamps: true });
module.exports = mongoose.model('UserOTP', UserOTPSchema);
`
- ā±ļø OTP records auto-expire after 15 minutes (900 seconds).
š Express Route Usage
- Create or update routes/auth.route.js in your backend:
`js
const express = require("express");
const router = express.Router();
const { AuthController } = require("@jehankandy/auth-core-db");
router.post("/create-auth", AuthController.createAuth);
router.post("/verify-otp", AuthController.verifyOTP);
module.exports = router;
`
- Mount the route in your main app:
`js
app.use("/auth", require("./routes/auth.route"));
``