onAuth middleware, allowing you to validate credentials and control access to your SMTP server.
Basic Authentication
Use theonAuth middleware to handle authentication requests. You must call either ctx.accept() or ctx.reject() to complete the authentication flow.
Credentials Object
Thectx.credentials object provides authentication information:
Using validatePassword
ThevalidatePassword helper method simplifies password validation:
Accept and Reject Patterns
Call
ctx.accept(user) to grant access. The user object is stored in ctx.session.user for subsequent middleware:app.onAuth(async (ctx, next) => {
const user = await validateCredentials(ctx.credentials);
if (user) {
ctx.accept({ id: user.id, email: user.email });
}
await next();
});
app.onMailFrom(async (ctx, next) => {
// Access authenticated user
console.log("User:", ctx.session.user);
await next();
});
app.onAuth(async (ctx) => {
// Default: "Rejected", code 535
ctx.reject();
// Custom message
ctx.reject("Invalid credentials");
// Custom message and code
ctx.reject("Account locked", 535);
});
If you don’t call
ctx.accept() in any middleware, authentication automatically fails with code 535:Authentication Options
Configure authentication behavior inFumiOptions:
Optional Authentication
Allow both authenticated and anonymous connections:Supported Auth Methods
Specify which authentication mechanisms to advertise:Secure Authentication
By default, authentication over unencrypted connections is blocked. Enable it explicitly:Real-World Examples
Database Authentication
Rate-Limited Authentication
Role-Based Access
Session Context
The authenticated user is available insession.user for all subsequent middleware: