@wasserstoff/mangi-tg-bot
Version:
A powerful Telegram Bot SDK with built-in authentication, session management, and database integration
116 lines • 4.92 kB
JavaScript
;
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.createAuthMiddleware = void 0;
const jsonwebtoken_1 = __importDefault(require("jsonwebtoken"));
const createAuthMiddleware = (jwtSecret) => {
return async (ctx, next) => {
if (!ctx.chat || ctx.chat.type !== "private") {
return next();
}
// Ensure session is properly extended with default properties
if (!ctx.session) {
ctx.session = { jwtToken: undefined };
}
// Set default properties on the session
ctx.session.custom = ctx.session.custom || {};
// Unconditionally override the save method with our auto-save implementation
ctx.session.save = function (callback) {
const storage = ctx.__storageAdapter;
const sessionKey = ctx.__sessionKey;
if (storage && typeof storage.write === 'function' && sessionKey) {
storage.write(sessionKey, ctx.session)
.then(() => {
if (ctx.config?.isDev) {
console.log('Session successfully saved to Redis via adapter');
}
callback();
})
.catch((err) => {
if (ctx.config?.isDev) {
console.error('Error saving session via adapter:', err);
}
callback(err);
});
}
else {
const redis = ctx.redis;
const key = sessionKey || `session:${ctx.chat?.id}`;
if (redis && typeof redis.set === 'function' && key) {
redis.set(key, JSON.stringify(ctx.session), (err) => {
if (err && ctx.config?.isDev) {
console.error('Error saving session to Redis:', err);
}
else if (ctx.config?.isDev) {
console.log('Session successfully saved to Redis directly');
}
callback(err);
});
}
else {
if (ctx.config?.isDev) {
console.log('Session auto-save triggered (no Redis available):', ctx.session);
}
callback();
}
}
};
const chatId = ctx.chat.id;
const userId = ctx.from?.id;
if (ctx.config?.isDev) {
ctx.logger.debug(`Processing auth middleware for user ${userId} in chat ${chatId}. Session:`, ctx.session);
}
if (!ctx.session.jwtToken) {
if (ctx.config?.isDev) {
ctx.logger.info(`No token found. Creating new JWT token for user ${userId} in chat ${chatId}`);
}
const payload = {
chatId: chatId,
userId: userId,
createdAt: new Date().toISOString(),
};
const token = jsonwebtoken_1.default.sign(payload, jwtSecret);
ctx.session.jwtToken = token;
if (ctx.config?.isDev) {
ctx.logger.debug(`JWT token created: ${token.substring(0, 20)}...`);
ctx.logger.info(`JWT token stored for user ${userId}`);
}
ctx.session.save((err) => {
if (err && ctx.config?.isDev) {
ctx.logger.error('Error saving session:', err);
}
else if (ctx.config?.isDev) {
ctx.logger.info('JWT token saved in session.');
}
});
}
else {
try {
if (ctx.config?.isDev) {
ctx.logger.debug(`Verifying existing token for user ${userId}`);
}
const decoded = jsonwebtoken_1.default.verify(ctx.session.jwtToken, jwtSecret);
if (decoded.chatId !== chatId) {
if (ctx.config?.isDev) {
ctx.logger.warn(`JWT token chatId mismatch: ${decoded.chatId} vs ${chatId}`);
}
ctx.session.jwtToken = undefined;
}
else if (ctx.config?.isDev) {
ctx.logger.debug(`JWT token verified for user ${userId} in chat ${chatId}`);
}
}
catch (error) {
if (ctx.config?.isDev) {
ctx.logger.error('Error verifying JWT token:', error);
}
ctx.session.jwtToken = undefined;
}
}
return next();
};
};
exports.createAuthMiddleware = createAuthMiddleware;
//# sourceMappingURL=auth.js.map