chore: add telegram handle callback route#424
Conversation
WalkthroughThe pull request introduces modifications to the authentication controller and routes, focusing on adding Telegram authorization support. A new function Changes
Poem
✨ Finishing Touches
Thank you for using CodeRabbit. We offer it for free to the OSS community and would appreciate your support in helping us grow. If you find it useful, would you consider giving us a shout-out on your favorite social media? 🪧 TipsChatThere are 3 ways to chat with CodeRabbit:
Note: Be mindful of the bot's finite context window. It's strongly recommended to break down tasks such as reading entire modules into smaller chunks. For a focused discussion, use review comments to chat about specific files and their changes, instead of using the PR comments. CodeRabbit Commands (Invoked using PR comments)
Other keywords and placeholders
CodeRabbit Configuration File (
|
There was a problem hiding this comment.
Actionable comments posted: 3
📜 Review details
Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro
📒 Files selected for processing (2)
src/controllers/auth.controller.ts(2 hunks)src/routes/v1/auth.route.ts(1 hunks)
⏰ Context from checks skipped due to timeout of 90000ms (1)
- GitHub Check: ci / lint / Lint
🔇 Additional comments (2)
src/controllers/auth.controller.ts (2)
81-84: Add input validation for Telegram callback dataThe callback handler should validate the incoming Telegram authentication data before processing it.
Consider adding:
- Validation middleware using your existing validate middleware
- Type definitions for Telegram auth data
- Hash verification of Telegram data
Would you like me to provide an example implementation of the validation logic?
5-10: Verify required imports for Telegram authenticationThe current imports might not include all necessary modules for Telegram authentication. Consider adding:
- Telegram auth configuration
- Telegram service (if separate from auth service)
- Hash verification utilities for Telegram
Run this script to check for existing Telegram-related modules:
✅ Verification successful
Current imports are sufficient for Telegram authentication
The telegramAuthorizeCallback implementation is a simple placeholder that only logs request data. The current imports provide all necessary dependencies, and the verification token is already accessible through the general config import. No additional Telegram-specific imports are required at this stage.
🏁 Scripts executed
The following scripts were executed for the analysis:
Script:
#!/bin/bash # Search for existing Telegram-related files and imports echo "Searching for Telegram-related files..." fd -e ts -e js telegram echo "Searching for Telegram-related imports..." rg -i "import.*telegram" -g "*.ts"Length of output: 1256
Script:
#!/bin/bash # Check telegram config exports and auth controller usage echo "Checking telegram config exports..." cat src/config/telegram.ts echo -e "\nChecking auth controller for Telegram patterns..." rg -A 5 "telegram" src/controllers/auth.controller.tsLength of output: 684
| // Routes | ||
| router.get('/discord/authorize', authController.discordAuthorize); | ||
| router.get('/discord/authorize/callback', authController.discordAuthorizeCallback); | ||
| router.get('/telegram/authorize/callback', authController.discordAuthorizeCallback); |
There was a problem hiding this comment.
Critical: Incorrect callback handler used for Telegram route
The Telegram callback route is incorrectly using Discord's callback handler (discordAuthorizeCallback). This will not work as Telegram and Discord have different OAuth flows and data structures. Use the newly added telegramAuthorizeCallback instead.
Apply this fix:
-router.get('/telegram/authorize/callback', authController.discordAuthorizeCallback);
+router.get('/telegram/authorize/callback', authController.telegramAuthorizeCallback);Committable suggestion skipped: line range outside the PR's diff.
| const telegramAuthorizeCallback = catchAsync(async function (req: Request, res: Response) { | ||
| console.log(req.body, req.query, req.params); | ||
| res.send('Hi'); | ||
| }); |
There was a problem hiding this comment.
Implementation incomplete: Add proper Telegram authorization logic
The current implementation is a placeholder that only logs data and returns "Hi". This needs to be replaced with proper Telegram authorization logic including:
- Validation of Telegram authentication data
- User creation/lookup logic
- Token generation
- Error handling similar to discordAuthorizeCallback
Here's a suggested structure:
const telegramAuthorizeCallback = catchAsync(async function (req: Request, res: Response) {
const STATUS_CODE_SIGNIN = 1001;
const STATUS_CODE_LOGIN = 1002;
const STATUS_CODE_ERROR = 1003;
try {
// Validate Telegram auth data
const telegramData = validateTelegramAuth(req.query);
// Find or create user
let user = await userService.getUserByFilter({ telegramId: telegramData.id });
let statusCode = STATUS_CODE_LOGIN;
if (!user) {
user = await userService.createUser({ telegramId: telegramData.id });
statusCode = STATUS_CODE_SIGNIN;
}
// Generate tokens
const tokens = await tokenService.generateAuthTokens(user);
// Return response
const params = {
statusCode,
accessToken: tokens.access.token,
accessExp: tokens.access.expires.toString(),
refreshToken: tokens.refresh.token,
refreshExp: tokens.refresh.expires.toString(),
};
const query = querystring.stringify(params);
res.redirect(`${config.frontend.url}/callback?${query}`);
} catch (err) {
logger.error({ err }, 'Failed to authorize telegram account');
const params = {
statusCode: STATUS_CODE_ERROR,
};
const query = querystring.stringify(params);
res.redirect(`${config.frontend.url}/callback?${query}`);
}
});|
|
||
| const telegramAuthorizeCallback = catchAsync(async function (req: Request, res: Response) { | ||
| console.log(req.body, req.query, req.params); | ||
| res.send('Hi'); |
There was a problem hiding this comment.
🛠️ Refactor suggestion
Remove console.log statement
Using console.log is not recommended in production code. Use the logger instead if debugging is needed.
- console.log(req.body, req.query, req.params);
+ logger.debug({ body: req.body, query: req.query, params: req.params }, 'Telegram callback received');Committable suggestion skipped: line range outside the PR's diff.
Summary by CodeRabbit
New Features
Chores