Skip to content

Commit

Permalink
Merge pull request #13 from Electrium-Mobility/next-js-update
Browse files Browse the repository at this point in the history
updated breaking changes for next-js 15
  • Loading branch information
Jerryzjx authored Nov 4, 2024
2 parents 1e641b0 + c0ebdbd commit ae5e3df
Show file tree
Hide file tree
Showing 10 changed files with 46 additions and 182 deletions.
7 changes: 6 additions & 1 deletion app/auth/callback/route.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,17 @@ export async function GET(request: Request) {
const requestUrl = new URL(request.url);
const code = requestUrl.searchParams.get("code");
const origin = requestUrl.origin;
const redirectTo = requestUrl.searchParams.get("redirect_to")?.toString();

if (code) {
const supabase = createClient();
const supabase = await createClient();
await supabase.auth.exchangeCodeForSession(code);
}

if (redirectTo) {
return NextResponse.redirect(`${origin}${redirectTo}`);
}

// URL to redirect to after sign up process completes
return NextResponse.redirect(`${origin}/protected`);
}
2 changes: 1 addition & 1 deletion app/items/page.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { createClient } from '@/utils/supabase/server';

export default async function Notes() {
const supabase = createClient();
const supabase = await createClient();
const { data: notes } = await supabase.from("bikes").select();

return <pre>{JSON.stringify(notes, null, 2)}</pre>
Expand Down
119 changes: 0 additions & 119 deletions app/login/page.tsx

This file was deleted.

20 changes: 0 additions & 20 deletions app/login/submit-button.tsx

This file was deleted.

4 changes: 2 additions & 2 deletions app/page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -89,8 +89,8 @@ function ProductDisplay({ id, image, name, price, isRental }: {
}

export default async function Home() {
const supabase = createClient();
const { data: bikes, error } = await supabase.from('bikes').select('*');
const supabase = await createClient();
const { data: bikes, error } = await supabase.from("bikes").select();

if (error) {
console.error('Error fetching bikes:', error);
Expand Down
17 changes: 11 additions & 6 deletions app/products/[productId]/page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import Navbar from '@/components/shop/Navbar';
import Footer from '@/components/shop/Footer';
import {Bike, getOneBike} from "@/utils/getBike";
import {notFound} from "next/navigation";

import { GetServerSideProps } from 'next';

function CartNotification({bike, subtotal, quantity, numItems}: {
bike: Bike,
Expand Down Expand Up @@ -36,9 +36,13 @@ function CartNotification({bike, subtotal, quantity, numItems}: {
);
}

interface ProductPageProps {
params: Promise<{ productId: string }>;
}

export default async function ProductPage({params}: { params: { productId: string } }) {
const bike = await getOneBike(params.productId);
export default async function ProductPage({ params }: ProductPageProps) {
const { productId } = await params;
const bike = await getOneBike(productId);
if (!bike) {
notFound();
}
Expand All @@ -62,14 +66,15 @@ export default async function ProductPage({params}: { params: { productId: strin
unoptimized
width={500}
height={500}
style={{objectFit: "contain"}}
className="rounded-lg"/>
style={{ objectFit: "contain" }}
className="rounded-lg"
/>
</div>
<div className="md:w-1/2">
<p className="text-gray-700 mb-4">{bike.description}</p>
<div className="flex items-center gap-4 mb-6">
<input type="number" defaultValue={1} min={1} max={bike.amount_stocked}
className="border p-2 w-16 text-center text-gray-800"/>
className="border p-2 w-16 text-center text-gray-800" />
<button className="bg-green-600 text-white px-6 py-2 rounded hover:bg-green-500">
{bike.for_rent ? 'Rent Now' : 'Add to Cart'}
</button>
Expand Down
2 changes: 1 addition & 1 deletion app/protected/page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import Header from "@/components/Header";
import { redirect } from "next/navigation";

export default async function ProtectedPage() {
const supabase = createClient();
const supabase = await createClient();

const {
data: { user },
Expand Down
4 changes: 2 additions & 2 deletions components/AuthButton.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import Link from "next/link";
import { redirect } from "next/navigation";

export default async function AuthButton() {
const supabase = createClient();
const supabase = await createClient();

const {
data: { user },
Expand All @@ -12,7 +12,7 @@ export default async function AuthButton() {
const signOut = async () => {
"use server";

const supabase = createClient();
const supabase = await createClient();
await supabase.auth.signOut();
return redirect("/login");
};
Expand Down
4 changes: 2 additions & 2 deletions utils/getBike.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ export interface Bike {

export async function getOneBike(productId: string) {
const cookieStore = cookies();
const supabase = createClient();
const supabase = await createClient();
const {data: bike, error} = await supabase
.from('bikes')
.select('*')
Expand All @@ -30,7 +30,7 @@ export async function getOneBike(productId: string) {

export async function getManyBikes(productIds: string[]) {
const cookieStore = cookies();
const supabase = createClient();
const supabase = await createClient();
const {data: bikes, error} = await supabase
.from('bikes')
.select('*')
Expand Down
49 changes: 21 additions & 28 deletions utils/supabase/server.ts
Original file line number Diff line number Diff line change
@@ -1,36 +1,29 @@
import { createServerClient, type CookieOptions } from "@supabase/ssr";
import { createServerClient } from "@supabase/ssr";
import { cookies } from "next/headers";

export const createClient = () => {
const cookieStore = cookies();
export const createClient = async () => {
const cookieStore = await cookies();

return createServerClient(
process.env.NEXT_PUBLIC_SUPABASE_URL!,
process.env.NEXT_PUBLIC_SUPABASE_ANON_KEY!,
{
cookies: {
get(name: string) {
return cookieStore.get(name)?.value;
},
set(name: string, value: string, options: CookieOptions) {
try {
cookieStore.set({ name, value, ...options });
} catch (error) {
// The `set` method was called from a Server Component.
// This can be ignored if you have middleware refreshing
// user sessions.
}
},
remove(name: string, options: CookieOptions) {
try {
cookieStore.set({ name, value: "", ...options });
} catch (error) {
// The `delete` method was called from a Server Component.
// This can be ignored if you have middleware refreshing
// user sessions.
}
process.env.NEXT_PUBLIC_SUPABASE_URL!,
process.env.NEXT_PUBLIC_SUPABASE_ANON_KEY!,
{
cookies: {
getAll() {
return cookieStore.getAll();
},
setAll(cookiesToSet) {
try {
cookiesToSet.forEach(({ name, value, options }) => {
cookieStore.set(name, value, options);
});
} catch (error) {
// The `set` method was called from a Server Component.
// This can be ignored if you have middleware refreshing
// user sessions.
}
},
},
},
},
);
};

0 comments on commit ae5e3df

Please sign in to comment.