fix and update
This commit is contained in:
@@ -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;
|
||||||
})
|
})
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -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
|
||||||
|
|||||||
Reference in New Issue
Block a user