secure-2fa
Version:
A secure, developer-friendly Node.js package for email-based OTP (2FA) with strong security controls
47 lines • 1.6 kB
JavaScript
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.SendGridAdapter = void 0;
const mail_1 = __importDefault(require("@sendgrid/mail"));
class SendGridAdapter {
constructor(config) {
mail_1.default.setApiKey(config.apiKey);
this.defaultFrom = config.from || 'noreply@yourdomain.com';
}
async sendEmail(params) {
const msg = {
to: params.to,
from: params.from || this.defaultFrom,
subject: params.subject,
html: params.html,
};
if (params.text) {
msg.text = params.text;
}
try {
await mail_1.default.send(msg);
}
catch (error) {
throw new Error(`Failed to send email: ${error instanceof Error ? error.message : 'Unknown error'}`);
}
}
async verifyConnection() {
try {
// SendGrid doesn't have a direct connection test, but we can validate the API key
// by making a minimal request
const response = await fetch('https://api.sendgrid.com/v3/user/profile', {
headers: {
'Authorization': `Bearer ${process.env['SENDGRID_API_KEY'] || ''}`,
},
});
return response.ok;
}
catch (error) {
return false;
}
}
}
exports.SendGridAdapter = SendGridAdapter;
//# sourceMappingURL=sendgrid-adapter.js.map
;