π Auth API
The Auth API handles user authentication, including signup, signin, email verification, password reset, and middleware for protecting routes.
Example Usage
const backend = require("dbackend"); // THE Backend
const auth = backend.auth({
jwtSecret: "your_jwt_secret", // Your JWT secret key
UserModel: User, // Your user model for authentication
mailer: mailer, // Optional: Mailer for email verification and password reset
});
// Signup Example
await auth.signup({
email: "user@example.com",
password: "yourpassword",
name: "Your Name",
});
// Signin Example
const result = await auth.signin({
email: "user@example.com",
password: "yourpassword",
});
// Verify Email Example
await auth.verifyEmail(verificationToken);
// Forgot Password Example
await auth.forgotPassword("user@example.com");
// Reset Password Example
await auth.resetPassword(resetToken, "newPassword123");π§ Mailer API
The Mailer API allows you to send emails for user verification, password reset, and other notifications.
Example Usage
const backend = require("dbackend"); // THE Backend
const mailer = backend.mailer({
service: "gmail", // Your email service (e.g., "gmail")
auth: {
user: "your-email@gmail.com",
pass: "your-email-password",
},
});
// Send Email Example
await mailer.sendMail({
to: "user@example.com",
subject: "Welcome to dbackend!",
html: "<h1>Welcome to dbackend!</h1>",
});βοΈ Middleware API
The Middleware API allows you to use built-in middleware for tasks like JSON parsing, URL encoding, and enabling CORS.
Example Usage
const backend = require("dbackend"); // THE Backend
const app = require("express")();
// Middleware Example
backend.middlewares(app, ["json", "urlencoded", "cors"]);
// Custom Middleware Example
backend.middlewares(app, [
(req, res, next) => {
console.log("Custom Middleware Triggered");
next();
},
]);π MongoDB API
The MongoDB API connects your app to a MongoDB database using Mongoose.
Example Usage
const backend = require("dbackend"); // THE Backend
// MongoDB Connection Example
backend.mongodb({
url: "mongodb://localhost:27017/mydb",
});π Full Example
Here's a complete example that demonstrates how to use all the services in a simple app.
const backend = require("dbackend"); // THE Backend
const express = require("express");
const app = express();
// Setup Authentication
const auth = backend.auth({
jwtSecret: "your_jwt_secret",
UserModel: User,
mailer: mailer,
});
// Setup Mailer
const mailer = backend.mailer({
service: "gmail",
auth: {
user: "your-email@gmail.com",
pass: "your-email-password",
},
});
// Setup Middleware
backend.middlewares(app, ["json", "urlencoded", "cors"]);
// Setup MongoDB
backend.mongodb({ url: "mongodb://localhost:27017/mydb" });
// Sample Route: Signup
app.post("/signup", async (req, res) => {
const { email, password, name } = req.body;
try {
await auth.signup({ email, password, name });
res.status(200).send("Signup successful");
} catch (error) {
res.status(500).send("Error: " + error.message);
}
});
// Sample Route: Send Email
app.post("/send-email", async (req, res) => {
const { to, subject, html } = req.body;
try {
await mailer.sendMail({ to, subject, html });
res.status(200).send("Email sent successfully");
} catch (error) {
res.status(500).send("Error: " + error.message);
}
});
// Start the server
app.listen(3000, () => {
console.log("Server running on port 3000");
});