Skip to content

Commit

Permalink
fix: cookies epic react
Browse files Browse the repository at this point in the history
  • Loading branch information
joelhooks committed Dec 18, 2024
1 parent ff04c2b commit da3ae8d
Showing 1 changed file with 19 additions and 1 deletion.
20 changes: 19 additions & 1 deletion apps/epic-react/src/lib/users.ts
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ export async function getUserAndSubscriber({
user = user ? user : await getUserByEmail(subscriber.email_address)
res.setHeader(
'Set-Cookie',
serialize('ck_subscriber', JSON.stringify(subscriber), {
serialize('ck_subscriber', JSON.stringify(deepOmitNull(subscriber)), {
httpOnly: true,
secure: process.env.NODE_ENV === 'production',
path: '/',
Expand All @@ -112,3 +112,21 @@ export async function getUserAndSubscriber({
subscriber: convertToSerializeForNextResponse(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 da3ae8d

Please sign in to comment.