Skip to content

Commit

Permalink
Merge pull request #49 from New-Syatte/kakao-user-bugfix-#45
Browse files Browse the repository at this point in the history
fix : 카카오 유저 로그인 시 userEmail 누락으로 Orders 확인 불가 이슈
ruddnjs3769 authored Dec 9, 2024
2 parents e8465ed + 62ce019 commit ce086df
Showing 6 changed files with 35 additions and 45 deletions.
12 changes: 5 additions & 7 deletions src/app/(order)/order/history/OrderHistoryClient.tsx
Original file line number Diff line number Diff line change
@@ -12,16 +12,16 @@ import { TrackingResponseEvent } from "@/type/order";
import Loader from "@/components/loader/Loader";
import Heading from "@/components/heading/Heading";

interface OrderHistoryClientProps {
userEmail: string;
interface Props {
userId: string;
}

const OrderHistoryClient = ({ userEmail }: OrderHistoryClientProps) => {
export default function OrderHistoryClient({ userId }: Props) {
const {
data: orders,
error,
mutate,
} = useSWR(["orders", userEmail], () => getOrders(userEmail));
} = useSWR(["orders", userId], () => getOrders(userId));

const dispatch = useDispatch();
const reduxOrders = useSelector(selectOrders);
@@ -76,6 +76,4 @@ const OrderHistoryClient = ({ userEmail }: OrderHistoryClientProps) => {
</div>
</section>
);
};

export default OrderHistoryClient;
}
2 changes: 1 addition & 1 deletion src/app/(order)/order/history/page.tsx
Original file line number Diff line number Diff line change
@@ -12,5 +12,5 @@ export default async function OrderHistory() {
}
const user = session?.user as UserWithId;

return <OrderHistoryClient userEmail={user.email} />;
return <OrderHistoryClient userId={user.id} />;
}
13 changes: 8 additions & 5 deletions src/app/api/auth/[...nextauth]/route.ts
Original file line number Diff line number Diff line change
@@ -34,13 +34,16 @@ export const authOptions: AuthOptions = {
if (!id) {
return false;
}
await addUser({

const userData = {
id,
name: name || "",
email: email || "",
name: name || "Guest User",
email: email || `${id}@placeholder.com`,
image,
username: email?.split("@")[0] || "",
});
username: email ? email.split("@")[0] : id,
};

await addUser(userData);
return true;
},
async jwt({ token, user }) {
40 changes: 14 additions & 26 deletions src/services/sanity/orders.ts
Original file line number Diff line number Diff line change
@@ -1,31 +1,11 @@
import { client } from "@/services/sanity/sanity";
import { Order } from "@/type/order";

export async function getOrders(email: string) {
if (!email) {
throw new Error("userId is required");
}

try {
const orders = await client.fetch(
`*[ _type == "order" && userEmail == $email]{
username,
userEmail,
orderDate,
orderAmount,
orderStatus,
cartItems,
billingAddress,
shippingAddress,
createdAt,
shippingInfo,
_id
}`,
{ email },
);
return orders;
} catch (error: any) {
console.error(`주문 불러오기 실패: ${error.message}`);
}
export async function getOrders(userId: string) {
return client.fetch(
`*[_type == "order" && userId == $userId] | order(orderDate desc)`,
{ userId },
);
}

export async function getOrder(orderId: string) {
@@ -73,3 +53,11 @@ export async function updateOrderStatus(
console.error(`주문 상태 변경 실패: ${error.message}`);
}
}

export async function createOrder(orderData: Order) {
const order = {
_type: "order",
...orderData,
};
return client.create(order);
}
2 changes: 2 additions & 0 deletions src/type/order.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
import { CartItem } from "./cart";

export type Order = {
userId: string;
userEmail: string;
displayName: string;
orderDate: string;
createdAt: string;
orderAmount: number;
11 changes: 5 additions & 6 deletions src/type/user.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@

export type User = {
name: string;
email: string;
username: string;
id: string;
name?: string;
email?: string;
username?: string;
image?: string;
}

};

0 comments on commit ce086df

Please sign in to comment.