feat: creates backend for the project

This commit is contained in:
vakabunga
2026-03-02 00:32:37 +03:00
parent 9d12702688
commit 4d67636633
24 changed files with 1735 additions and 0 deletions

View File

@@ -0,0 +1,51 @@
import { Router } from 'express';
import { asyncHandler } from '../utils';
import { requireAuth } from '../middleware/auth';
import * as authService from '../services/auth';
const router = Router();
router.post(
'/login',
asyncHandler(async (req, res) => {
const { login, password } = req.body;
if (!login || !password) {
res.status(400).json({ error: 'BAD_REQUEST', message: 'login and password are required' });
return;
}
const result = await authService.login({ login, password });
if (!result) {
res.status(401).json({ error: 'UNAUTHORIZED', message: 'Invalid credentials' });
return;
}
res.cookie('sid', result.sessionId, {
httpOnly: true,
sameSite: 'lax',
path: '/',
});
res.json({ ok: true });
}),
);
router.post(
'/logout',
requireAuth,
asyncHandler(async (req, res) => {
await authService.logout(req.sessionId!);
res.clearCookie('sid');
res.json({ ok: true });
}),
);
router.get(
'/me',
requireAuth,
asyncHandler(async (req, res) => {
const result = await authService.me(req.sessionId!);
res.json(result);
}),
);
export default router;