Skip to content

Commit

Permalink
sigh
Browse files Browse the repository at this point in the history
  • Loading branch information
joelhooks committed Dec 18, 2024
1 parent d368956 commit 6a64598
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 5 deletions.
3 changes: 0 additions & 3 deletions apps/epic-web/src/server/get-middleware-response.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,5 @@ export async function getMiddlewareResponse(req: NextRequest) {
}
}

// Delete ck_subscriber cookie if it exists
response.cookies.delete('ck_subscriber')

return response
}
20 changes: 19 additions & 1 deletion apps/epic-web/src/server/user-cookies.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import {Subscriber} from 'schemas/subscriber'

export function setUserCookie(res: NextResponse, subscriber: Subscriber) {
if (subscriber) {
res.cookies.set('ck_subscriber', JSON.stringify(subscriber), {
res.cookies.set('ck_subscriber', JSON.stringify(deepOmitNull(subscriber)), {
secure: process.env.NODE_ENV === 'production',
path: '/',
httpOnly: true,
Expand All @@ -18,3 +18,21 @@ export function setUserCookie(res: NextResponse, subscriber: Subscriber) {
export function clearUserCookie(res: NextResponse) {
res.cookies.delete('ck_subscriber')
}

function deepOmitNull(obj: any): any {
if (Array.isArray(obj)) {
return obj.map(deepOmitNull).filter((x) => x !== null)
}

if (obj && typeof obj === 'object') {
return Object.entries(obj).reduce((acc, [key, value]) => {
const cleaned = deepOmitNull(value)
if (cleaned !== null) {
acc[key] = cleaned
}
return acc
}, {} as Record<string, any>)
}

return obj === null ? undefined : obj
}
20 changes: 19 additions & 1 deletion apps/total-typescript/src/server/user-cookies.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import {Subscriber} from '@skillrecordings/skill-lesson/schemas/subscriber'

export function setUserCookie(res: NextResponse, subscriber: Subscriber) {
if (subscriber) {
res.cookies.set('ck_subscriber', JSON.stringify(subscriber), {
res.cookies.set('ck_subscriber', JSON.stringify(deepOmitNull(subscriber)), {
secure: process.env.NODE_ENV === 'production',
path: '/',
httpOnly: true,
Expand All @@ -18,3 +18,21 @@ export function setUserCookie(res: NextResponse, subscriber: Subscriber) {
export function clearUserCookie(res: NextResponse) {
res.cookies.delete('ck_subscriber')
}

function deepOmitNull(obj: any): any {
if (Array.isArray(obj)) {
return obj.map(deepOmitNull).filter((x) => x !== null)
}

if (obj && typeof obj === 'object') {
return Object.entries(obj).reduce((acc, [key, value]) => {
const cleaned = deepOmitNull(value)
if (cleaned !== null) {
acc[key] = cleaned
}
return acc
}, {} as Record<string, any>)
}

return obj === null ? undefined : obj
}

0 comments on commit 6a64598

Please sign in to comment.