tazakazahstan/src/auth/routes.ts
2026-11-14 15:37:13 +05:00

23 lines
1.2 KiB
TypeScript
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

import type { Db } from "../db/database.ts";
import { badRequest, forbidden, unauthorized } from "../errors.ts";
import type { Router } from "../http/router.ts";
import { authenticate, createSession, findUserByLogin, publicUser } from "./users.ts";
export function registerAuthRoutes(router: Router, db: Db, opts: { devLogin: boolean }): void {
// Тестовый вход: только development/test. В остальных средах маршрута нет — 404.
if (opts.devLogin) {
router.post("/api/auth/dev-login", (ctx) => {
const login = (ctx.body as { login?: unknown } | undefined)?.login;
if (typeof login !== "string" || login.length === 0) {
throw badRequest("validation_error", "Поле login обязательно");
}
const user = findUserByLogin(db, login);
if (!user) throw unauthorized("invalid_login", "Неизвестный тестовый пользователь");
if (user.status === "blocked") throw forbidden("user_blocked", "Пользователь заблокирован");
return { body: { token: createSession(db, user.id), user: publicUser(user) } };
});
}
router.get("/api/me", (ctx) => ({ body: publicUser(authenticate(db, ctx)) }));
}