Skip to content
This repository was archived by the owner on Jan 23, 2025. It is now read-only.

Commit 4b64f0b

Browse files
Remove deprecated supabase auth helpers and update
workflows for creating supabase client
1 parent c7867b2 commit 4b64f0b

File tree

18 files changed

+1095
-1371
lines changed

18 files changed

+1095
-1371
lines changed

app/account/page.tsx

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,9 @@ import {
33
getSession,
44
getUserDetails,
55
getSubscription
6-
} from '@/app/supabase-server';
6+
} from '@/app/supabase-server-calls';
77
import Button from '@/components/ui/Button';
8-
import { Database } from '@/types_db';
9-
import { createServerActionClient } from '@supabase/auth-helpers-nextjs';
8+
import { createClient } from '@/utils/supabase/server';
109
import { revalidatePath } from 'next/cache';
1110
import { cookies } from 'next/headers';
1211
import Link from 'next/link';
@@ -38,13 +37,15 @@ export default async function Account() {
3837
'use server';
3938

4039
const newName = formData.get('name') as string;
41-
const supabase = createServerActionClient<Database>({ cookies });
40+
const cookieStore = cookies();
41+
const supabase = createClient(cookieStore);
4242
const session = await getSession();
43-
const user = session?.user;
43+
if (!session) return redirect('/signin');
44+
const user = session.user;
4445
const { error } = await supabase
4546
.from('users')
4647
.update({ full_name: newName })
47-
.eq('id', user?.id);
48+
.eq('id', user.id);
4849
if (error) {
4950
console.log(error);
5051
}
@@ -55,7 +56,8 @@ export default async function Account() {
5556
'use server';
5657

5758
const newEmail = formData.get('email') as string;
58-
const supabase = createServerActionClient<Database>({ cookies });
59+
const cookieStore = cookies();
60+
const supabase = createClient(cookieStore);
5961
const { error } = await supabase.auth.updateUser({ email: newEmail });
6062
if (error) {
6163
console.log(error);

app/api/create-checkout-session/route.ts

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,8 @@
1-
import { cookies, headers } from 'next/headers';
2-
import { createRouteHandlerClient } from '@supabase/auth-helpers-nextjs';
1+
import { cookies } from 'next/headers';
2+
import { createClient } from '@/utils/supabase/server';
33
import { stripe } from '@/utils/stripe';
4-
import { createOrRetrieveCustomer } from '@/utils/supabase-admin';
4+
import { createOrRetrieveCustomer } from '@/utils/supabase/admin';
55
import { getURL } from '@/utils/helpers';
6-
import { Database } from '@/types_db';
76

87
export async function POST(req: Request) {
98
if (req.method === 'POST') {
@@ -12,7 +11,8 @@ export async function POST(req: Request) {
1211

1312
try {
1413
// 2. Get the user from Supabase auth
15-
const supabase = createRouteHandlerClient<Database>({cookies});
14+
const cookieStore = cookies();
15+
const supabase = createClient(cookieStore);
1616
const {
1717
data: { user }
1818
} = await supabase.auth.getUser();

app/api/create-portal-link/route.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,14 @@
11
import { cookies } from 'next/headers';
2-
import { createRouteHandlerClient } from '@supabase/auth-helpers-nextjs';
2+
import { createClient } from '@/utils/supabase/server';
33
import { stripe } from '@/utils/stripe';
4-
import { createOrRetrieveCustomer } from '@/utils/supabase-admin';
4+
import { createOrRetrieveCustomer } from '@/utils/supabase/admin';
55
import { getURL } from '@/utils/helpers';
6-
import { Database } from '@/types_db';
76

87
export async function POST(req: Request) {
98
if (req.method === 'POST') {
109
try {
11-
const supabase = createRouteHandlerClient<Database>({cookies});
10+
const cookieStore = cookies();
11+
const supabase = createClient(cookieStore);
1212
const {
1313
data: { user }
1414
} = await supabase.auth.getUser();

app/api/webhooks/route.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import {
44
upsertProductRecord,
55
upsertPriceRecord,
66
manageSubscriptionStatusChange
7-
} from '@/utils/supabase-admin';
7+
} from '@/utils/supabase/admin';
88

99
const relevantEvents = new Set([
1010
'product.created',

app/auth/callback/route.ts

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,18 @@
1-
import { createRouteHandlerClient } from '@supabase/auth-helpers-nextjs'
1+
import { createClient } from '@/utils/supabase/server'
22
import { cookies } from 'next/headers'
33
import { NextResponse } from 'next/server'
4-
5-
import type { NextRequest } from 'next/server'
6-
import type { Database } from '@/types_db'
4+
import { NextRequest } from 'next/server'
75

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

1213
if (code) {
13-
const supabase = createRouteHandlerClient<Database>({ cookies })
14+
const cookieStore = cookies()
15+
const supabase = createClient(cookieStore)
1416
await supabase.auth.exchangeCodeForSession(code)
1517
}
1618

app/page.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import {
33
getSession,
44
getSubscription,
55
getActiveProductsWithPrices
6-
} from '@/app/supabase-server';
6+
} from '@/app/supabase-server-calls';
77

88
export default async function PricingPage() {
99
const [session, products, subscription] = await Promise.all([

app/signin/page.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { getSession } from '@/app/supabase-server';
1+
import { getSession } from '@/app/supabase-server-calls';
22
import AuthUI from './AuthUI';
33

44
import { redirect } from 'next/navigation';

app/supabase-provider.tsx

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
'use client';
22

33
import type { Database } from '@/types_db';
4-
import { createPagesBrowserClient } from '@supabase/auth-helpers-nextjs';
5-
import type { SupabaseClient } from '@supabase/auth-helpers-nextjs';
4+
import { createClient } from '@/utils/supabase/client';
5+
import type { SupabaseClient } from '@supabase/supabase-js';
66
import { useRouter } from 'next/navigation';
77
import { createContext, useContext, useEffect, useState } from 'react';
88

@@ -17,7 +17,7 @@ export default function SupabaseProvider({
1717
}: {
1818
children: React.ReactNode;
1919
}) {
20-
const [supabase] = useState(() => createPagesBrowserClient());
20+
const [supabase] = useState(() => createClient());
2121
const router = useRouter();
2222

2323
useEffect(() => {

app/supabase-server.ts renamed to app/supabase-server-calls.ts

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
1-
import { Database } from '@/types_db';
2-
import { createServerComponentClient } from '@supabase/auth-helpers-nextjs';
1+
import { createClient } from '@/utils/supabase/server';
32
import { cookies } from 'next/headers';
43
import { cache } from 'react';
54

6-
export const createServerSupabaseClient = cache(() =>
7-
createServerComponentClient<Database>({ cookies })
8-
);
5+
export const createServerSupabaseClient = cache(() => {
6+
const cookieStore = cookies();
7+
return createClient(cookieStore);
8+
});
99

1010
export async function getSession() {
1111
const supabase = createServerSupabaseClient();

components/ui/Navbar/Navbar.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import Link from 'next/link';
2-
import { createServerSupabaseClient } from '@/app/supabase-server';
2+
import { createServerSupabaseClient } from '@/app/supabase-server-calls';
33

44
import Logo from '@/components/icons/Logo';
55
import SignOutButton from './SignOutButton';

0 commit comments

Comments
 (0)