Skip to content

Commit dc4daea

Browse files
authored
[FEATURE] Migrate to supabase-ssr (#9)
- Migrate to @supabase/ssr - Update unstable_cache implementation - Update edge runtime for some pages
1 parent 190a41d commit dc4daea

33 files changed

+258
-143
lines changed

app/(auth)/signin/page.tsx

+3-4
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import { Metadata } from "next"
22
import { redirect } from "next/navigation"
33
import { UserAuthForm } from "@/components/modules/auth/UserAuthForm"
44
import { getCurrentSession } from "@/lib/session"
5-
import { createServerComponentClient } from "@supabase/auth-helpers-nextjs"
5+
import { createClient } from "@/lib/supabase/server"
66
import { cookies } from "next/headers"
77
import { Heading3 } from "@/components/ui/typography"
88
import { siteConfig } from "@/config/site"
@@ -12,13 +12,12 @@ export const metadata: Metadata = {
1212
description: "Sigin to your account",
1313
}
1414

15+
export const runtime = "edge"
1516
export const dynamic = 'force-dynamic'
1617

1718
export default async function LoginPage() {
1819
const cookieStore = cookies()
19-
const supabase = createServerComponentClient({
20-
cookies: () => cookieStore,
21-
})
20+
const supabase = createClient(cookieStore)
2221
const session = await getCurrentSession(supabase)
2322

2423
if (session) {

app/(auth)/signup/page.tsx

+3-4
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,13 @@
11
import { Metadata } from "next"
22
import { redirect } from "next/navigation"
33
import { UserSignupForm } from "@/components/modules/auth/UserSignupForm"
4-
import { createServerComponentClient } from "@supabase/auth-helpers-nextjs"
4+
import { createClient } from "@/lib/supabase/server"
55
import { getCurrentSession } from "@/lib/session"
66
import { cookies } from "next/headers"
77
import { Heading3 } from "@/components/ui/typography"
88
import { siteConfig } from "@/config/site"
99

10+
export const runtime = "edge"
1011
export const metadata: Metadata = {
1112
title: "Signup",
1213
description: "Signup a new account",
@@ -16,9 +17,7 @@ export const dynamic = 'force-dynamic'
1617

1718
export default async function LoginPage() {
1819
const cookieStore = cookies()
19-
const supabase = createServerComponentClient({
20-
cookies: () => cookieStore,
21-
})
20+
const supabase = createClient(cookieStore)
2221
const session = await getCurrentSession(supabase)
2322

2423
if (session) {

app/api/auth/callback/route.ts

+13-10
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,20 @@
1-
import { createRouteHandlerClient } from '@supabase/auth-helpers-nextjs'
1+
import { NextResponse } from 'next/server'
22
import { cookies } from 'next/headers'
3-
import { NextRequest, NextResponse } from 'next/server'
3+
import { createClient } from '@/lib/supabase/server'
44

5-
export const dynamic = 'force-dynamic'
6-
7-
export async function GET(req: NextRequest) {
8-
const supabase = createRouteHandlerClient({ cookies })
9-
const { searchParams } = new URL(req.url)
10-
const code = searchParams.get('code')
5+
export async function GET(request: Request) {
6+
// The `/auth/callback` route is required for the server-side auth flow implemented
7+
// by the Auth Helpers package. It exchanges an auth code for the user's session.
8+
// https://supabase.com/docs/guides/auth/auth-helpers/nextjs#managing-sign-in-with-code-exchange
9+
const requestUrl = new URL(request.url)
10+
const code = requestUrl.searchParams.get('code')
1111

1212
if (code) {
13+
const cookieStore = cookies()
14+
const supabase = createClient(cookieStore)
1315
await supabase.auth.exchangeCodeForSession(code)
1416
}
1517

16-
return NextResponse.redirect(new URL('/apps/chat', req.url))
17-
}
18+
// URL to redirect to after sign in process completes
19+
return NextResponse.redirect(requestUrl.origin)
20+
}

app/api/auth/logout/route.ts

+3-2
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,12 @@
1-
import { createRouteHandlerClient } from '@supabase/auth-helpers-nextjs'
1+
import { createClient } from '@/lib/supabase/server'
22
import { cookies } from 'next/headers'
33
import { type NextRequest, NextResponse } from 'next/server'
44

55
export const dynamic = 'force-dynamic'
66

77
export async function POST(req: NextRequest) {
8-
const supabase = createRouteHandlerClient({ cookies })
8+
const cookieStore = cookies()
9+
const supabase = createClient(cookieStore)
910

1011
// Check if we have a session
1112
const {

app/api/chat/route.ts

+4-4
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
1-
import { RequestCookies } from "@edge-runtime/cookies";
21
import OpenAI from 'openai'
32
import { OpenAIStream, StreamingTextResponse } from 'ai'
4-
import { createRouteHandlerClient } from '@supabase/auth-helpers-nextjs';
53
import { pick } from 'lodash';
64
import { AxiomRequest, withAxiom } from 'next-axiom';
75
import { getCurrentSession } from '@/lib/session';
86
import { createNewMessage, deleteMessagesFrom, getMessageById } from '@/lib/db/message';
97
import { createNewChat } from '@/lib/db/chats';
108
import { getAppBySlug } from '@/lib/db/apps';
9+
import { createClient } from "@/lib/supabase/server";
10+
import { cookies } from "next/headers";
1111
import {
1212
env
1313
} from '@/env.mjs';
@@ -25,8 +25,8 @@ export const POST = withAxiom(async (req: AxiomRequest) => {
2525
route: 'api/chat',
2626
});
2727

28-
const cookies = new RequestCookies(req.headers) as any;
29-
const supabase = createRouteHandlerClient({ cookies: () => cookies })
28+
const cookieStore = cookies()
29+
const supabase = createClient(cookieStore)
3030
const params = await req.json()
3131
const {
3232
messages,

app/apps/chat/[id]/page.tsx

+6-2
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,17 @@ import React from "react"
22
import { cookies } from "next/headers"
33
import { Metadata } from "next"
44
import { ChatPanel } from "@/components/modules/apps/chat/ChatPanel"
5-
import { createServerComponentClient } from "@supabase/auth-helpers-nextjs"
5+
import { createClient } from "@/lib/supabase/server"
66
import { getChatById, getChats } from "@/lib/db/chats"
77
import { getAppBySlug } from "@/lib/db/apps"
88
import { getCurrentSession } from "@/lib/session"
99
import { Message } from "ai"
1010
import { getMessages } from "@/lib/db/message"
1111
import { ChatParams } from "@/components/modules/apps/chat/types"
1212

13+
export const runtime = "edge"
14+
export const preferredRegion = "home"
15+
1316
export const metadata: Metadata = {
1417
title: "Chat",
1518
description: "Chat with your AI assistant to generate new ideas and get inspired.",
@@ -18,7 +21,8 @@ export const metadata: Metadata = {
1821
export default async function ChatPage({ params }: { params: { id: string } }) {
1922
const chatId = params.id
2023

21-
const supabase = createServerComponentClient({ cookies })
24+
const cookieStore = cookies()
25+
const supabase = createClient(cookieStore)
2226
const session = await getCurrentSession(supabase)
2327
const currentApp = await getAppBySlug(supabase, '/apps/chat')
2428

app/apps/chat/page.tsx

+2-4
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import React from "react"
22
import { Metadata } from "next"
33
import { ChatPanel } from "@/components/modules/apps/chat/ChatPanel"
4-
import { createServerComponentClient } from "@supabase/auth-helpers-nextjs"
4+
import { createClient } from "@/lib/supabase/server"
55
import { cookies } from "next/headers"
66
import { getAppBySlug } from "@/lib/db/apps"
77
import { getCurrentSession } from "@/lib/session"
@@ -16,9 +16,7 @@ export default async function NewChatPage() {
1616
const chatId = uuidv4()
1717

1818
const cookieStore = cookies()
19-
const supabase = createServerComponentClient({
20-
cookies: () => cookieStore,
21-
})
19+
const supabase = createClient(cookieStore)
2220
const session = await getCurrentSession(supabase)
2321
const currentApp = await getAppBySlug(supabase, '/apps/chat')
2422

app/apps/layout.tsx

+3-2
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import { ChatHistory } from "@/components/modules/apps/chat/ChatHistory"
22
import { getAppBySlug } from "@/lib/db/apps"
33
import { getChats } from "@/lib/db/chats"
44
import { getCurrentSession } from "@/lib/session"
5-
import { createServerComponentClient } from "@supabase/auth-helpers-nextjs"
5+
import { createClient } from "@/lib/supabase/server"
66
import { cookies } from "next/headers"
77
import { MainLayout } from '@/components/ui/common/MainLayout'
88

@@ -11,7 +11,8 @@ interface AppLayoutProps {
1111
}
1212

1313
export default async function AppLayout({ children }: AppLayoutProps) {
14-
const supabase = createServerComponentClient({ cookies })
14+
const cookieStore = cookies()
15+
const supabase = createClient(cookieStore)
1516
const session = await getCurrentSession(supabase)
1617
const currentApp = await getAppBySlug(supabase, '/apps/chat')
1718

app/apps/page.tsx

-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
import { Heading1 } from "@/components/ui/typography"
22

33
export const runtime = "edge"
4-
export const preferredRegion = "home"
54

65
export default function Apps() {
76
return (

app/page.tsx

+2
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@ export const metadata: Metadata = {
1414
description: siteConfig.description,
1515
}
1616

17+
export const runtime = "edge"
18+
1719
export default async function Home() {
1820
return (
1921
<MainLayout>

app/profile/page.tsx

+5-2
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,16 @@ import { ProfileForm } from "@/components/modules/profile/ProfileForm";
33
import { ProfileFormValues } from "@/components/modules/profile/type";
44
import { getCurrentProfile } from "@/lib/db/profile";
55
import { getCurrentSession } from "@/lib/session";
6-
import { createServerComponentClient } from "@supabase/auth-helpers-nextjs";
6+
import { createClient } from "@/lib/supabase/server";
77
import { cookies } from "next/headers";
88

99
export const dynamic = 'force-dynamic'
1010

11+
export const runtime = "edge"
12+
1113
export default async function Profile() {
12-
const supabase = createServerComponentClient({ cookies })
14+
const cookieStore = cookies()
15+
const supabase = createClient(cookieStore)
1316
const profile = await getCurrentProfile(supabase)
1417
const session = await getCurrentSession(supabase)
1518

components/modules/apps/app-side-bar/AppSideBar.tsx

+3-2
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,12 @@
11
import React from 'react'
22
import { cookies } from 'next/headers';
3-
import { createServerComponentClient } from '@supabase/auth-helpers-nextjs';
3+
import { createClient } from '@/lib/supabase/server';
44
import { getApps } from '@/lib/db/apps';
55
import { AppSideBarList } from './AppSideBarList';
66

77
export const AppSideBar = async () => {
8-
const supabase = await createServerComponentClient({ cookies })
8+
const cookieStore = cookies()
9+
const supabase = await createClient(cookieStore)
910
const apps = await getApps(supabase)
1011
return (
1112
<aside className="sticky top-16 flex w-0 flex-col overflow-x-hidden bg-muted transition-[width] lg:w-[73px] lg:border-r lg:hover:w-80" aria-label="Sidenav">

components/modules/apps/chat/action.ts

+8-5
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,17 @@
11
'use server'
22

3-
import { Database, Update } from "@/lib/db"
3+
import { Update } from "@/lib/db"
44
import { getAppBySlug } from "@/lib/db/apps"
55
import { createNewChat as createNewChatDb, deleteChat as deleteChatDb, updateChat as updateChatDb } from "@/lib/db/chats"
66
import { getCurrentSession } from "@/lib/session"
7-
import { createServerComponentClient } from "@supabase/auth-helpers-nextjs"
7+
import { createClient } from "@/lib/supabase/server"
88
import { revalidatePath } from "next/cache"
99
import { cookies } from "next/headers"
1010
import { redirect } from "next/navigation"
1111

1212
export const createNewChat = async () => {
13-
const supabase = createServerComponentClient<Database>({ cookies })
13+
const cookieStore = cookies()
14+
const supabase = createClient(cookieStore)
1415
const session = await getCurrentSession(supabase)
1516
const currentApp = await getAppBySlug(supabase, '/apps/chat')
1617

@@ -32,7 +33,8 @@ export const createNewChat = async () => {
3233
}
3334

3435
export const deleteChat = async (id: string) => {
35-
const supabase = createServerComponentClient<Database>({ cookies })
36+
const cookieStore = cookies()
37+
const supabase = createClient(cookieStore)
3638

3739
try {
3840
await deleteChatDb(supabase, id)
@@ -43,7 +45,8 @@ export const deleteChat = async (id: string) => {
4345
}
4446

4547
export const updateChat = async (params: Update<'chats'>) => {
46-
const supabase = createServerComponentClient<Database>({ cookies })
48+
const cookieStore = cookies()
49+
const supabase = createClient(cookieStore)
4750
const { id, ...rest } = params
4851
if (!id) {
4952
throw new Error('Missing ID')

components/modules/apps/chat/control-side-bar/action.ts

+4-3
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,15 @@
11
'use server'
22

3-
import { Chat, Database, Update } from "@/lib/db"
3+
import { Chat, Update } from "@/lib/db"
44
import { getAppBySlug } from "@/lib/db/apps"
55
import { updateChat } from "@/lib/db/chats"
66
import { getCurrentSession } from "@/lib/session"
7-
import { createServerComponentClient } from "@supabase/auth-helpers-nextjs"
7+
import { createClient } from "@/lib/supabase/server"
88
import { cookies } from "next/headers"
99

1010
export const updateChatSettings = async (id: Chat['id'], params: Update<'chats'>['settings']) => {
11-
const supabase = createServerComponentClient<Database>({ cookies })
11+
const cookieStore = cookies()
12+
const supabase = createClient(cookieStore)
1213
const session = await getCurrentSession(supabase)
1314
const currentApp = await getAppBySlug(supabase, '/apps/chat')
1415

components/modules/auth/SocialLoginButton.tsx

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import { Button, ButtonProps } from '@/components/ui/Button'
22
import { useToast } from '@/components/ui/use-toast'
33
import { getURL } from '@/config/site'
4-
import { createClientComponentClient } from '@supabase/auth-helpers-nextjs'
4+
import { createClient } from '@/lib/supabase/client'
55
import { Provider } from '@supabase/supabase-js'
66
import { Loader } from 'lucide-react'
77
import React from 'react'
@@ -11,7 +11,7 @@ type SocialLoginButtonProps = ButtonProps & {
1111
}
1212

1313
export const SocialLoginButton = ({ provider, children, ...rest }: SocialLoginButtonProps) => {
14-
const supabase = createClientComponentClient()
14+
const supabase = createClient()
1515
const [isLoading, setIsLoading] = React.useState<boolean>(false)
1616
const { toast } = useToast()
1717

components/modules/auth/UserAuthForm.tsx

+2-3
Original file line numberDiff line numberDiff line change
@@ -9,18 +9,17 @@ import { zodResolver } from "@hookform/resolvers/zod"
99
import { useToast } from "@/components/ui/use-toast"
1010
import { InputField } from "@/components/ui/form/form-fields"
1111
import { Loader } from "lucide-react"
12-
import { createClientComponentClient } from "@supabase/auth-helpers-nextjs"
13-
import { Database } from "@/lib/db"
1412
import Link from "next/link"
1513
import { credentialAuthSchema } from "./schema"
1614
import { SocialLoginOptions } from "./SocialLoginOptions"
15+
import { createClient } from "@/lib/supabase/client"
1716

1817
type UserAuthFormProps = React.HTMLAttributes<HTMLDivElement>
1918

2019
type FormData = z.infer<typeof credentialAuthSchema>
2120

2221
export function UserAuthForm({ className, ...props }: UserAuthFormProps) {
23-
const supabase = createClientComponentClient<Database>()
22+
const supabase = createClient()
2423
const {
2524
register,
2625
formState,

components/modules/auth/UserSignupForm.tsx

+2-3
Original file line numberDiff line numberDiff line change
@@ -10,17 +10,16 @@ import { zodResolver } from "@hookform/resolvers/zod"
1010
import { useToast } from "@/components/ui/use-toast"
1111
import { InputField } from "@/components/ui/form/form-fields"
1212
import { Loader } from "lucide-react"
13-
import { createClientComponentClient } from "@supabase/auth-helpers-nextjs"
14-
import { Database } from "@/lib/db"
1513
import { registerProfileSchema } from "./schema"
1614
import Link from "next/link"
15+
import { createClient } from "@/lib/supabase/client"
1716

1817
type UserSignupFormProps = React.HTMLAttributes<HTMLDivElement>
1918

2019
type FormData = z.infer<typeof registerProfileSchema>
2120

2221
export function UserSignupForm({ className, ...props }: UserSignupFormProps) {
23-
const supabase = createClientComponentClient<Database>()
22+
const supabase = createClient()
2423
const { replace } = useRouter()
2524
const {
2625
register,

components/modules/profile/AccountDropdownMenu.tsx

+2-2
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ import {
1111
DropdownMenuTrigger,
1212
} from '@/components/ui/DropdownMenu';
1313
import { getCurrentProfile } from '@/lib/db/profile';
14-
import { createClientComponentClient } from '@supabase/auth-helpers-nextjs';
14+
import { createClient } from '@/lib/supabase/client'
1515
import { Loader, User } from 'lucide-react';
1616
import Link from 'next/link';
1717
import LogoutButton from '../auth/LogoutButton';
@@ -24,7 +24,7 @@ type AccountDropdownMenuProps = {
2424
}
2525

2626
export const AccountDropdownMenu = ({ userEmail }: AccountDropdownMenuProps) => {
27-
const supabase = createClientComponentClient()
27+
const supabase = createClient()
2828
const [ isLoading, setIsLoading ] = React.useState<boolean>()
2929
const [isMounted, setIsMounted] = React.useState(false)
3030

0 commit comments

Comments
 (0)