Api: rotacja refresh tokenu + limit na /auth/refresh, dokumentacja i localhost CORS tylko poza produkcja
This commit is contained in:
@@ -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');
|
||||
|
||||
Reference in New Issue
Block a user