/
fegerV
/
Stogram
Обзор
Документация
Войти
/
fegerV
/
Stogram
Код
Запросы
0
Задачи
Вики
Пакеты
0
Релизы
0
Аналитика
Безопасность
main
server/src/middleware/auth.ts
52 строки
1 KB
engine-labs-app[bot]
feat(telegram): full Telegram integration with bot, login, bridges, and mini app
01 ноя 2025, 18:44
01 ноя 2025, 18:44
9732650
Код
Авторство
О чём код?
import { Request, Response, NextFunction } from 'express'; import jwt from 'jsonwebtoken'; import { prisma } from '../index'; export interface AuthRequest extends Request { userId?: string; user?: any; } export const authenticate = async ( req: AuthRequest, res: Response, next: NextFunction ) => { try { const token = req.headers.authorization?.split(' ')[1]; if (!token) { return res.status(401).json({ error: 'Authentication required' }); } const decoded = jwt.verify(token, process.env.JWT_SECRET || 'secret') as { userId: string; }; const user = await prisma.user.findUnique({ where: { id: decoded.userId }, select: { id: true, email: true, username: true, displayName: true, avatar: true, bio: true, status: true, }, }); if (!user) { return res.status(401).json({ error: 'User not found' }); } req.userId = user.id; req.user = user; next(); } catch (error) { return res.status(401).json({ error: 'Invalid token' }); } }; export const auth = authenticate; export const authenticateToken = authenticate;