Skip to content

Commit

Permalink
fix: reduce cookie size and remove middleware
Browse files Browse the repository at this point in the history
  • Loading branch information
joelhooks committed Dec 18, 2024
1 parent 424079c commit 3b5a97a
Show file tree
Hide file tree
Showing 6 changed files with 84 additions and 20 deletions.
19 changes: 0 additions & 19 deletions apps/epic-web/src/middleware.ts

This file was deleted.

23 changes: 23 additions & 0 deletions apps/epic-web/src/pages/admin/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -19,14 +19,37 @@ import {
Tooltip,
Legend,
} from 'chart.js'
import {getToken} from 'next-auth/jwt'
import {getCurrentAbility, UserSchema} from '@skillrecordings/skill-lesson'

type ProgressData = Awaited<
ReturnType<ReturnType<typeof getSdk>['getLessonProgressCountsByDate']>
>

export const getServerSideProps: GetServerSideProps = async (context) => {
const token = await getToken({req: context.req})
const {getLessonProgressCountsByDate} = getSdk()

try {
const user = UserSchema.parse(token)
const ability = getCurrentAbility({user})
if (!ability.can('create', 'Content')) {
return {
redirect: {
destination: '/login',
permanent: false,
},
}
}
} catch (error) {
return {
redirect: {
destination: '/login',
permanent: false,
},
}
}

const progressData: any[] = await getLessonProgressCountsByDate()

return {
Expand Down
22 changes: 22 additions & 0 deletions apps/epic-web/src/pages/creator/tips/[slug]/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,28 @@ import Layout from 'components/app/layout'
import cn from 'classnames'
import {type Tip} from 'lib/tips'
import {RxPlus, RxPencil1, RxEyeOpen} from 'react-icons/rx'
import {getCurrentAbility} from '@skillrecordings/skill-lesson'
import {UserSchema} from '@skillrecordings/skill-lesson'
import {getToken} from 'next-auth/jwt'
import {GetServerSideProps} from 'next'

export const getServerSideProps: GetServerSideProps = async (context) => {
const token = await getToken({req: context.req})
const user = UserSchema.parse(token)
const ability = getCurrentAbility({user})
if (!ability.can('create', 'Content')) {
return {
redirect: {
destination: '/login',
permanent: false,
},
}
}

return {
props: {},
}
}

const EditTip: React.FC<{slug?: string}> = ({slug}) => {
const router = useRouter()
Expand Down
15 changes: 15 additions & 0 deletions apps/epic-web/src/pages/creator/tips/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,26 @@ import EditTip from 'pages/creator/tips/[slug]/index'
import {getAllTips, TipSchema} from 'lib/tips'
import {first, groupBy} from 'lodash'
import {GetServerSideProps} from 'next'
import {UserSchema} from '@skillrecordings/skill-lesson'
import {getCurrentAbility} from '@skillrecordings/skill-lesson'
import {getToken} from 'next-auth/jwt'

export const getServerSideProps: GetServerSideProps = async ({req, params}) => {
const tips = await getAllTips(false)
const mostRecentTipSlug = tips && first(tips)?.slug

const token = await getToken({req})
const user = UserSchema.parse(token)
const ability = getCurrentAbility({user})
if (!ability.can('create', 'Content')) {
return {
redirect: {
destination: '/login',
permanent: false,
},
}
}

return {
redirect: {
destination: `/creator/tips/${mostRecentTipSlug}`,
Expand Down
22 changes: 22 additions & 0 deletions apps/epic-web/src/pages/creator/tips/new.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,28 @@ import CreateTipForm from 'module-builder/create-tip-form'
import Layout from 'components/app/layout'
import {Alert, AlertDescription, AlertTitle} from '@skillrecordings/ui'
import {GrInfo} from 'react-icons/gr'
import {getCurrentAbility} from '@skillrecordings/skill-lesson'
import {UserSchema} from '@skillrecordings/skill-lesson'
import {getToken} from 'next-auth/jwt'
import {GetServerSideProps} from 'next'

export const getServerSideProps: GetServerSideProps = async (context) => {
const token = await getToken({req: context.req})
const user = UserSchema.parse(token)
const ability = getCurrentAbility({user})
if (!ability.can('create', 'Content')) {
return {
redirect: {
destination: '/login',
permanent: false,
},
}
}

return {
props: {},
}
}

const NewTip = () => {
return (
Expand Down
3 changes: 2 additions & 1 deletion packages/convertkit-sdk/src/convertkit.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import fetch from 'node-fetch'
import {format} from 'date-fns'
import first from 'lodash/first'
import {z} from 'zod'
import {isNull, omitBy} from 'lodash'

const convertkitBaseUrl =
process.env.CONVERTKIT_BASE_URL || 'https://api.convertkit.com/v3/'
Expand Down Expand Up @@ -52,7 +53,7 @@ export function getConvertkitSubscriberCookie(subscriber: any): Cookie[] {
return [
{
name: 'ck_subscriber',
value: JSON.stringify(subscriber),
value: JSON.stringify(omitBy(subscriber, isNull)),
options: {
secure: process.env.NODE_ENV === 'production',
httpOnly: true,
Expand Down

0 comments on commit 3b5a97a

Please sign in to comment.