init
This commit is contained in:
@@ -0,0 +1,66 @@
|
||||
import { Elysia, t } from "elysia";
|
||||
import { authMiddleware } from "../middlewares/auth";
|
||||
import {
|
||||
loginService,
|
||||
registerService,
|
||||
sessionService,
|
||||
} from "../services/auth";
|
||||
import type { LoginData, RegisterData } from "../services/auth/types";
|
||||
|
||||
export const authController = new Elysia({ prefix: "/auth" })
|
||||
// POST /login
|
||||
.post("/login", async ({ body, status, request }) => {
|
||||
const { email, password } = body as LoginData;
|
||||
|
||||
// Получить метаданные запроса
|
||||
const metadata = {
|
||||
userAgent: request.headers.get("User-Agent") || null,
|
||||
ipAddress:
|
||||
request.headers.get("X-Forwarded-For") ||
|
||||
request.headers.get("X-Real-IP") ||
|
||||
null,
|
||||
};
|
||||
|
||||
const result = await loginService.login(email, password, metadata);
|
||||
|
||||
if (!result) {
|
||||
return status(401, "Invalid email or password");
|
||||
}
|
||||
|
||||
return result;
|
||||
})
|
||||
// POST /register (публичная регистрация)
|
||||
.post("/register", async ({ body, status }) => {
|
||||
const result = await registerService.register(body as RegisterData);
|
||||
|
||||
if (!result) {
|
||||
return status(409, "User with this email already exists");
|
||||
}
|
||||
|
||||
return { user: result };
|
||||
})
|
||||
// Защищенные роуты (требуют authMiddleware + проверка ролей через БД)
|
||||
.use(authMiddleware)
|
||||
// GET /me
|
||||
.get("/me", async ({ currentUser }) => {
|
||||
return { user: currentUser };
|
||||
})
|
||||
// POST /logout
|
||||
.post("/logout", async ({ authSession }) => {
|
||||
await sessionService.revoke(authSession.id);
|
||||
return { message: "Logged out successfully" };
|
||||
})
|
||||
// POST /register-user (регистрация администратором)
|
||||
// Доступ проверяется через БД (таблица protected_routes)
|
||||
.post("/register-user", async ({ body, status, currentUser }) => {
|
||||
const result = await registerService.register(
|
||||
body as RegisterData,
|
||||
currentUser.role
|
||||
);
|
||||
|
||||
if (!result) {
|
||||
return status(409, "User with this email already exists");
|
||||
}
|
||||
|
||||
return { user: result };
|
||||
});
|
||||
Reference in New Issue
Block a user