π§ dbackend (THE Backend)
dbackend is a modular backend toolkit that simplifies various backend functionalities, such as authentication, email handling, middleware management, and MongoDB integration. This guide provides detailed documentation for every service and how to use it.
π Installation
npm install dbackend
π¦ Features
- β MongoDB connection with Mongoose
- β Authentication system (Signup, Signin, JWT, Email Verification, Password Reset)
- β Built-in JWT middleware
- β Mailer integration using Nodemailer
- β Plug-and-play Express middlewares (CORS, JSON, URL Encoded)
π Auth API
The Auth API handles user authentication, including signup, signin, email verification, password reset, and middleware for protecting routes.
Setup Authentication
To set up the authentication service, you need to configure it with a JWT secret, the user model, and optionally a mailer for email verification and password reset functionalities. Here's how you do it:
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
This function registers a new user. It takes the following parameters:
- email: The user's email (String)
- password: The user's password (String)
- name: The user's name (String)
await auth.signup({ email: "user@example.com", password: "yourpassword", name: "Your Name", });
Signin
This function logs in an existing user. It requires the following parameters:
- email: The user's email (String)
- password: The user's password (String)
const result = await auth.signin({ email: "user@example.com", password: "yourpassword", });
Verify Email
This function verifies a user's email by the token sent during signup.
- token: The email verification token (String)
await auth.verifyEmail(verificationToken);
Forgot Password
This function sends a password reset link to the user's email.
- email: The user's email (String)
await auth.forgotPassword("user@example.com");
Reset Password
This function resets the user's password using the token from the password reset email.
- token: The reset password token (String)
- newPassword: The new password (String)
await auth.resetPassword(resetToken, "newPassword123");
π§ Mailer API
The Mailer API allows you to send emails for user verification, password reset, and other notifications.
sendMail
This function sends an email. It requires the following parameters:
- to: The recipient's email address (String)
- subject: The email subject (String)
- text: The plain text version of the email (String)
- html: The HTML version of the email (String)
await mailer.sendMail({ to: "user@example.com", subject: "Welcome!", 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.
middlewares
This function applies middleware to your Express app. It accepts an array of middleware names or custom middleware functions.
backend.middlewares(app, ["json", "urlencoded", "cors"]);
π MongoDB API
The MongoDB API connects your app to a MongoDB database. It accepts a configuration object with the connection URL.
mongodb
This function connects to MongoDB using Mongoose.
backend.mongodb({ url: "mongodb://localhost:27017/mydb" });
π Code Examples & References
For detailed code examples and additional references, check out the Reference Page.
π Credits
Special thanks to the contributors of dbackend and the open-source community!