-
Notifications
You must be signed in to change notification settings - Fork 4
chore: add telegram handle callback route #424
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,13 +1,13 @@ | ||
| import httpStatus from 'http-status'; | ||
| import { Request, Response } from 'express'; | ||
| import config from '../config'; | ||
| import { discord } from '../config/oAtuh2'; | ||
| import { userService, authService, tokenService, discordServices } from '../services'; | ||
| import { catchAsync } from '../utils'; | ||
| import httpStatus from 'http-status'; | ||
| import querystring from 'querystring'; | ||
| import { generateState } from '../config/oAtuh2'; | ||
| import { ISessionRequest } from '../interfaces'; | ||
|
|
||
| import config from '../config'; | ||
| import logger from '../config/logger'; | ||
| import { discord, generateState } from '../config/oAtuh2'; | ||
| import { ISessionRequest } from '../interfaces'; | ||
| import { authService, discordServices, tokenService, userService } from '../services'; | ||
| import { catchAsync } from '../utils'; | ||
|
|
||
| const discordAuthorize = catchAsync(async function (req: ISessionRequest, res: Response) { | ||
| const state = generateState(); | ||
|
|
@@ -78,9 +78,14 @@ const generateToken = catchAsync(async function (req: Request, res: Response) { | |
| res.send(token); | ||
| }); | ||
|
|
||
| const telegramAuthorizeCallback = catchAsync(async function (req: Request, res: Response) { | ||
| console.log(req.body, req.query, req.params); | ||
| res.send('Hi'); | ||
| }); | ||
|
Comment on lines
+81
to
+84
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 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:
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}`);
}
}); |
||
| export default { | ||
| discordAuthorize, | ||
| discordAuthorizeCallback, | ||
| telegramAuthorizeCallback, | ||
| refreshTokens, | ||
| logout, | ||
| generateToken, | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,12 +1,16 @@ | ||
| import express from 'express'; | ||
|
|
||
| import { authController } from '../../controllers'; | ||
| import { auth, validate } from '../../middlewares'; | ||
| import { authValidation } from '../../validations'; | ||
| import { validate, auth } from '../../middlewares'; | ||
|
|
||
| const router = express.Router(); | ||
|
|
||
| // 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. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Critical: Incorrect callback handler used for Telegram route The Telegram callback route is incorrectly using Discord's callback handler ( Apply this fix: -router.get('/telegram/authorize/callback', authController.discordAuthorizeCallback);
+router.get('/telegram/authorize/callback', authController.telegramAuthorizeCallback);
|
||
|
|
||
| router.post('/generate-token', auth(), validate(authValidation.generateToken), authController.generateToken); | ||
| router.post('/logout', validate(authValidation.logout), authController.logout); | ||
| router.post('/refresh-tokens', validate(authValidation.refreshTokens), authController.refreshTokens); | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🛠️ Refactor suggestion
Remove console.log statement
Using console.log is not recommended in production code. Use the logger instead if debugging is needed.