Skip to content

Commit

Permalink
all the cookies ffs
Browse files Browse the repository at this point in the history
  • Loading branch information
joelhooks committed Dec 18, 2024
1 parent 6a64598 commit ff04c2b
Show file tree
Hide file tree
Showing 4 changed files with 79 additions and 7 deletions.
20 changes: 19 additions & 1 deletion apps/epic-web/src/lib/users.ts
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,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 @@ -113,3 +113,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
}
26 changes: 22 additions & 4 deletions packages/skill-lesson/trpc/routers/convertkit.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ export const convertkitRouter = router({

const convertkitCookie = serialize(
`ck_subscriber`,
JSON.stringify(updatedSubscriber.subscriber),
JSON.stringify(deepOmitNull(updatedSubscriber.subscriber)),
{
secure: process.env.NODE_ENV === 'production',
path: '/',
Expand Down Expand Up @@ -86,7 +86,7 @@ export const convertkitRouter = router({

const convertkitCookie = serialize(
`ck_subscriber`,
JSON.stringify(updatedSubscriber.subscriber),
JSON.stringify(deepOmitNull(updatedSubscriber.subscriber)),
{
secure: process.env.NODE_ENV === 'production',
path: '/',
Expand Down Expand Up @@ -167,7 +167,7 @@ export const convertkitRouter = router({

const convertkitCookie = serialize(
`ck_subscriber`,
JSON.stringify(updatedSubscriber.subscriber),
JSON.stringify(deepOmitNull(updatedSubscriber.subscriber)),
{
secure: process.env.NODE_ENV === 'production',
path: '/',
Expand Down Expand Up @@ -226,7 +226,7 @@ export const convertkitRouter = router({

const convertkitCookie = serialize(
`ck_subscriber`,
JSON.stringify(updatedSubscriber.subscriber),
JSON.stringify(deepOmitNull(updatedSubscriber.subscriber)),
{
secure: process.env.NODE_ENV === 'production',
path: '/',
Expand Down Expand Up @@ -321,3 +321,21 @@ export const convertkitRouter = router({
}
}),
})

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 packages/skill-lesson/trpc/routers/pricing.ts
Original file line number Diff line number Diff line change
Expand Up @@ -211,7 +211,7 @@ export const pricing = router({
user = user ? user : await getUserByEmail(subscriber.email_address)
ctx.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 Down Expand Up @@ -305,3 +305,21 @@ export const pricing = router({
return null
}),
})

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 packages/skill-lesson/utils/ck-set-subscriber-cookie.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ export function convertkitSetSubscriberCookie({
if (subscriber) {
const convertkitCookie = serialize(
`ck_subscriber`,
JSON.stringify(subscriber),
JSON.stringify(deepOmitNull(subscriber)),
{
secure: process.env.NODE_ENV === 'production',
path: '/',
Expand All @@ -25,3 +25,21 @@ export function convertkitSetSubscriberCookie({
res.setHeader('Set-Cookie', convertkitCookie)
}
}

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 ff04c2b

Please sign in to comment.