Api: rotacja refresh tokenu + limit na /auth/refresh, dokumentacja i localhost CORS tylko poza produkcja

This commit is contained in:
tisachi
2026-10-11 15:53:01 +02:00
parent 2fb288e029
commit c5c6583a2d
5 changed files with 28 additions and 12 deletions

View File

@@ -103,20 +103,22 @@ export function createAuthRoutes({ prisma }: AppDeps) {
* @openapi
* /auth/refresh:
* post:
* summary: Refresh access token (refresh token from httpOnly cookie)
* summary: Refresh access token (refresh token from httpOnly cookie, rotated on success)
* tags: [Auth]
* responses:
* 200: { description: OK, returns new access token }
* 200: { description: OK, returns new access token (new refresh token in cookie) }
* 401: { description: Missing/invalid/revoked refresh token }
*/
router.post(
'/refresh',
authLimiter,
asyncHandler(async (req, res) => {
const token = (req.cookies as Record<string, string | undefined>)[REFRESH_COOKIE];
try {
if (!token) throw unauthorized('Missing refresh token');
const result = await refresh(prisma, token);
res.json(result);
setRefreshCookie(res, result.refreshToken);
res.json({ accessToken: result.accessToken });
} catch (e) {
res.clearCookie(REFRESH_COOKIE, { path: '/' });
throw e instanceof AppError ? e : unauthorized('Invalid refresh token');