Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 12 additions & 7 deletions src/controllers/auth.controller.ts
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();
Expand Down Expand Up @@ -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);
Copy link

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.

-  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.

res.send('Hi');
});
Comment on lines +81 to +84
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ Potential issue

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}`);
  }
});

export default {
discordAuthorize,
discordAuthorizeCallback,
telegramAuthorizeCallback,
refreshTokens,
logout,
generateToken,
Expand Down
6 changes: 5 additions & 1 deletion src/routes/v1/auth.route.ts
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);
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ Potential issue

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.


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);
Expand Down
Loading