fix and update

This commit is contained in:
lendry
2026-07-01 14:03:57 +03:00
parent 6929fb41fc
commit 55deb5c152
2 changed files with 16 additions and 6 deletions

View File

@@ -1,6 +1,6 @@
import { CallHandler, ExecutionContext, Injectable, NestInterceptor } from '@nestjs/common'; import { CallHandler, ExecutionContext, Injectable, NestInterceptor } from '@nestjs/common';
import { JwtService } from '@nestjs/jwt'; import { JwtService } from '@nestjs/jwt';
import { Observable, tap } from 'rxjs'; import { Observable, mergeMap } from 'rxjs';
import type { Response } from 'express'; import type { Response } from 'express';
import { attachFedcmCookieFromAuthResult } from '../lib/fedcm-cookie'; import { attachFedcmCookieFromAuthResult } from '../lib/fedcm-cookie';
@@ -13,8 +13,9 @@ export class FedcmCookieInterceptor implements NestInterceptor {
const response = http.getResponse<Response>(); const response = http.getResponse<Response>();
return next.handle().pipe( return next.handle().pipe(
tap((data) => { mergeMap(async (data) => {
attachFedcmCookieFromAuthResult(response, this.jwt, data); await attachFedcmCookieFromAuthResult(response, this.jwt, data);
return data;
}) })
); );
} }

View File

@@ -53,9 +53,15 @@ export async function setFedcmSessionCookie(res: Response, jwt: JwtService, payl
if (!payload.sub || !payload.sessionId || payload.pinVerified === false) { if (!payload.sub || !payload.sessionId || payload.pinVerified === false) {
return; return;
} }
if (res.headersSent) {
return;
}
const secure = cookieSecureEnabled(); const secure = cookieSecureEnabled();
const value = await signFedcmSessionPayload(jwt, payload); const value = await signFedcmSessionPayload(jwt, payload);
if (res.headersSent) {
return;
}
res.cookie(FEDCM_SESSION_COOKIE, value, { res.cookie(FEDCM_SESSION_COOKIE, value, {
httpOnly: true, httpOnly: true,
@@ -67,6 +73,9 @@ export async function setFedcmSessionCookie(res: Response, jwt: JwtService, payl
} }
export function clearFedcmSessionCookie(res: Response) { export function clearFedcmSessionCookie(res: Response) {
if (res.headersSent) {
return;
}
const secure = cookieSecureEnabled(); const secure = cookieSecureEnabled();
res.clearCookie(FEDCM_SESSION_COOKIE, { res.clearCookie(FEDCM_SESSION_COOKIE, {
httpOnly: true, httpOnly: true,
@@ -98,11 +107,11 @@ export async function resolveFedcmSessionFromRequest(
return verifyFedcmSessionToken(jwt, token); return verifyFedcmSessionToken(jwt, token);
} }
export function attachFedcmCookieFromAuthResult( export async function attachFedcmCookieFromAuthResult(
res: Response, res: Response,
jwt: JwtService, jwt: JwtService,
result: unknown result: unknown
): void { ): Promise<void> {
if (!result || typeof result !== 'object') return; if (!result || typeof result !== 'object') return;
const data = result as { const data = result as {
accessToken?: string; accessToken?: string;
@@ -120,7 +129,7 @@ export function attachFedcmCookieFromAuthResult(
const userId = data.user?.id; const userId = data.user?.id;
if (!userId) return; if (!userId) return;
void setFedcmSessionCookie(res, jwt, { await setFedcmSessionCookie(res, jwt, {
sub: userId, sub: userId,
sessionId: data.sessionId, sessionId: data.sessionId,
pinVerified: true pinVerified: true