A simple, extensible authentication SDK for Node.js and Express applications.
Supports manual login, JWT authentication, refresh tokens, and easy integration with Mongoose models.
- Manual authentication (username/email + password)
- JWT-based authentication middleware
- Refresh token support
- Easy integration with Express and Mongoose
- Error handling utilities
npm install @dmxdev/auth-sdkimport {
AuthSDK,
AuthSDKError,
AuthTypesEnum,
passport,
} from "@dmxdev/auth-sdk";
import mongoose from "mongoose";
import express from "express";
const app = express();
app.use(express.json());
// Connect to MongoDB
mongoose.connect("mongodb://<your-mongo-uri>");
// Define your User model
const User = mongoose.model(
"User",
new mongoose.Schema({
username: String,
password: String,
})
);
// Configure AuthSDK
AuthSDK.configure(
app,
{
authType: AuthTypesEnum.MANUAL,
jwtSecret: "your-secret",
requireRefreshToken: true,
tokenExpiry: "15m",
refreshTokenExpiry: "7d",
},
{
UserModel: User,
}
);app.post("/register", async (req, res) => {
const { username, password, email } = req.body;
try {
const data = await AuthSDK.register({
username,
email,
password,
});
res.json(data);
} catch (error) {
if (error instanceof AuthSDKError) {
res.status(400).json({ error: error.message });
}
}
});app.post("/login", async (req, res) => {
const { username, password } = req.body;
try {
const data = await AuthSDK.login({
username,
password,
});
res.json(data);
} catch (error) {
if (error instanceof AuthSDKError) {
res.status(400).json({ error: error.message });
}
}
});app.get("/me", AuthSDK.authenticate, (req, res) => {
const user = AuthSDK.getLoggedInUser(req);
res.json(user);
});app.listen(3000, () => {
console.log("Server running on port 3000");
});Initializes the SDK with your Express app, configuration, and Mongoose models.
Registers a new user.
Authenticates a user and returns JWT (and refresh token if enabled).
Express middleware to protect routes using JWT.
Returns the authenticated user from the request.
Base error class for SDK errors.
Auth-SDK/
├── src/
│ ├── config.ts
│ ├── errors.ts
│ ├── index.ts
│ ├── types.ts
│ ├── strategies/
│ │ └── manual.login.ts
│ └── utils/
│ └── jwt.ts
├── dist/
├── Demo/
│ └── index.js
├── package.json
└── tsconfig.json