From ab0fa37ba7bf0cf0050202465091ef45a0bc83ca Mon Sep 17 00:00:00 2001 From: Fachri Hawari Date: Wed, 2 Oct 2024 10:02:23 +0700 Subject: [PATCH] fix unauthenticated user to add to cart --- src/app/(main)/cart/page.tsx | 4 +- src/app/(main)/orders/page.tsx | 4 +- src/app/(main)/payment/[token]/page.tsx | 2 +- src/components/AddToCartButton.tsx | 12 +++++- src/components/Navbar.tsx | 4 +- src/components/SweetAlert.tsx | 52 +++---------------------- src/lib/actions/cart.ts | 10 ++++- src/lib/actions/orders.ts | 8 ++++ src/lib/actions/users.ts | 9 ++++- src/lib/utils/auth.ts | 5 --- src/lib/utils/swal.ts | 49 +++++++++++++++++++++++ 11 files changed, 97 insertions(+), 62 deletions(-) delete mode 100644 src/lib/utils/auth.ts create mode 100644 src/lib/utils/swal.ts diff --git a/src/app/(main)/cart/page.tsx b/src/app/(main)/cart/page.tsx index 06f1f1f..c4ce7a3 100644 --- a/src/app/(main)/cart/page.tsx +++ b/src/app/(main)/cart/page.tsx @@ -2,7 +2,7 @@ import CartNotLoggedIn from './components/CartNotLoggedIn'; import CartEmpty from './components/CartEmpty'; import CartItems from './components/CartItems'; import { getCart } from '@/lib/actions/cart'; -import { isLoggedIn } from '@/lib/utils/auth'; +import { isLoggedIn } from '@/lib/actions/users'; import CartSummary from './components/CartSummary'; export default async function CartPage() { @@ -10,7 +10,7 @@ export default async function CartPage() { let content; - if (!isLoggedIn()) { + if (!await isLoggedIn()) { content = } else if (cart.items.length === 0) { content = diff --git a/src/app/(main)/orders/page.tsx b/src/app/(main)/orders/page.tsx index 95f6fa3..247f0ca 100644 --- a/src/app/(main)/orders/page.tsx +++ b/src/app/(main)/orders/page.tsx @@ -1,7 +1,7 @@ import { getOrders } from "@/lib/actions/orders"; import OrdersEmpty from './components/OrdersEmpty'; import OrdersList from './components/OrdersList'; -import { isLoggedIn } from '@/lib/utils/auth'; +import { isLoggedIn } from '@/lib/actions/users'; import OrdersNotLoggedIn from './components/OrdersNotLoggedIn'; export default async function OrdersPage() { @@ -9,7 +9,7 @@ export default async function OrdersPage() { let content; - if (!isLoggedIn()) { + if (!await isLoggedIn()) { content = } else if (orders.length === 0) { content = diff --git a/src/app/(main)/payment/[token]/page.tsx b/src/app/(main)/payment/[token]/page.tsx index fedd4d7..7f130ba 100644 --- a/src/app/(main)/payment/[token]/page.tsx +++ b/src/app/(main)/payment/[token]/page.tsx @@ -4,12 +4,12 @@ import { useMidtrans } from "@/lib/hooks/useMidtrans" import { setQueryParams } from "@/lib/utils/url" import { useEffect } from "react" import { useRouter } from "next/navigation" + type PaymentPageProps = { params: { token: string } } - export default function PaymentPage({ params }: PaymentPageProps) { const { token } = params const router = useRouter() diff --git a/src/components/AddToCartButton.tsx b/src/components/AddToCartButton.tsx index ae862f9..bade99f 100644 --- a/src/components/AddToCartButton.tsx +++ b/src/components/AddToCartButton.tsx @@ -4,6 +4,8 @@ import { useEffect, useState } from 'react'; import { FiShoppingCart, FiPlus, FiMinus } from "react-icons/fi"; import { addToCart, getCart, removeFromCart, updateCartItemQuantity } from "@/lib/actions/cart"; import type { Product } from "@/lib/db/product_collection"; +import { isLoggedIn } from '@/lib/actions/users'; +import { swal } from '@/lib/utils/swal'; interface AddToCartButtonProps { product: Product; @@ -32,6 +34,14 @@ export default function AddToCartButton({ product, size = 'small' }: AddToCartBu }, [product._id]); const handleAddToCart = async () => { + if (!await isLoggedIn()) { + return swal({ + title: 'Unauthenticated', + type: 'error', + message: 'You must be login to add product to the cart' + }) + } + setIsLoading(true); if (isInCart) { await updateCartItemQuantity(product._id.toString(), quantity + 1); @@ -119,4 +129,4 @@ export default function AddToCartButton({ product, size = 'small' }: AddToCartBu {isLoading ? 'Adding...' : 'Add to Cart'} ); -} \ No newline at end of file +} diff --git a/src/components/Navbar.tsx b/src/components/Navbar.tsx index 6ace270..0556961 100644 --- a/src/components/Navbar.tsx +++ b/src/components/Navbar.tsx @@ -7,7 +7,7 @@ import { buildSearchParams, setQueryParams } from '@/lib/utils/url'; import TokoPakEdiLogo from './TokoPakEdiLogo'; import { logout } from '@/lib/actions/users'; import { getCart } from '@/lib/actions/cart'; -import { isLoggedIn } from '@/lib/utils/auth'; +import { isLoggedIn } from '@/lib/actions/users'; async function Navbar() { const searchParams = buildSearchParams(headers().get('x-current-url')) // HACK: get current url from headers @@ -71,7 +71,7 @@ async function Navbar() {
{ - isLoggedIn() ? ( + await isLoggedIn() ? (