Api: rotacja refresh tokenu + limit na /auth/refresh, dokumentacja i localhost CORS tylko poza produkcja
This commit is contained in:
@@ -22,10 +22,7 @@ export interface AuthResult {
|
||||
refreshToken: string;
|
||||
}
|
||||
|
||||
export async function register(
|
||||
prisma: PrismaClient,
|
||||
input: RegisterInput,
|
||||
): Promise<AuthResult> {
|
||||
export async function register(prisma: PrismaClient, input: RegisterInput): Promise<AuthResult> {
|
||||
const email = input.email.toLowerCase();
|
||||
const existing = await prisma.user.findUnique({ where: { email } });
|
||||
if (existing) throw conflict('Email already registered');
|
||||
@@ -52,12 +49,20 @@ export async function login(prisma: PrismaClient, input: LoginInput): Promise<Au
|
||||
};
|
||||
}
|
||||
|
||||
/**
|
||||
* Odświeżenie access tokenu z rotacją refresh tokenu — każde wywołanie
|
||||
* wystawia nowy refresh token (stary pozostaje ważny do wygaśnięcia, ale
|
||||
* klient i tak dostaje wyłącznie nowy w cookie httpOnly).
|
||||
*/
|
||||
export async function refresh(
|
||||
prisma: PrismaClient,
|
||||
token: string,
|
||||
): Promise<{ accessToken: string }> {
|
||||
): Promise<{ accessToken: string; refreshToken: string }> {
|
||||
const payload = verifyRefreshToken(token);
|
||||
const user = await prisma.user.findUnique({ where: { id: payload.sub } });
|
||||
if (!user || user.tokenVersion !== payload.tv) throw unauthorized('Token revoked');
|
||||
return { accessToken: signAccessToken(user.id, user.tokenVersion) };
|
||||
return {
|
||||
accessToken: signAccessToken(user.id, user.tokenVersion),
|
||||
refreshToken: signRefreshToken(user.id, user.tokenVersion),
|
||||
};
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user