/
IvanRyabchenko
/
random-coffee-backend
Обзор
Документация
Войти
/
IvanRyabchenko
/
random-coffee-backend
Код
Запросы
1
Задачи
Вики
Пакеты
0
Релизы
0
CI/CD
Аналитика
Безопасность
develop
src/controllers/auth.controller.ts
25 строк
846 B
Ivan Ryabchenko
feat(auth): implement POST /auth/login and authGuard middleware
14 май 2026, 20:56
14 май 2026, 20:56
e16359e
Код
Авторство
О чём код?
import type { Context } from 'hono' import { loginSchema } from '../dto/auth.dto' import { AuthError, login } from '../services/auth.service' export async function loginController(c: Context): Promise<Response> { const body = await c.req.json().catch(() => null) const parsed = loginSchema.safeParse(body) if (!parsed.success) { return c.json( { message: 'Validation failed', statusCode: 400, error: parsed.error.flatten() }, 400, ) } try { const token = await login(parsed.data.email, parsed.data.password) return c.json({ token }, 200) } catch (err) { if (err instanceof AuthError) { return c.json({ message: err.message, statusCode: 401, error: 'Unauthorized' }, 401) } return c.json({ message: 'Internal server error', statusCode: 500, error: 'Internal Server Error' }, 500) } }