Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -39,3 +39,6 @@ yarn-error.log*
# typescript
*.tsbuildinfo
next-env.d.ts

# Supabase Local Settings & Tokens
.supabase/
2 changes: 2 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@
"@radix-ui/react-tabs": "^1.1.14",
"@radix-ui/react-tooltip": "^1.2.9",
"@radix-ui/react-visually-hidden": "^1.2.5",
"@supabase/ssr": "^0.12.0",
"@supabase/supabase-js": "^2.108.2",
"@tanstack/react-query": "^5.101.0",
"axios": "^1.17.0",
"class-variance-authority": "^0.7.1",
Expand Down
142 changes: 142 additions & 0 deletions src/actions/auth.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,142 @@
"use server";

import { createClient } from "@/lib/supabase/server";

interface SignUpResult {
success: boolean;
error?: string;
user?: {
id: string;
email: string;
nickname: string;
created_at: string;
is_no_spoiler_mode: boolean;
profile_url: string | null;
updated_at: string;
};
}

export const signUpWithEmail = async (
email: string,
password: string,
nickname: string
): Promise<SignUpResult> => {
const supabase = await createClient();

const { data: authData, error: authError } = await supabase.auth.signUp({
email,
password,
options: {
data: {
nickname: nickname,
},
},
});

if (authError || !authData.user) {
// console.error("회원가입 에러:", authError?.message);
console.error("❌ 프로필 로드 진짜 에러 원인:", {
name: authError?.name,
status: authError?.status,
message: authError?.message,
});
return { success: false, error: authError?.message || "SignUp Failed" };
}

const { data: profileData, error: profileError } = await supabase
.from("profiles")
.select("*")
.eq("id", authData.user.id)
.single();

if (profileError || !profileData) {
console.error("프로필 로드 실패:", profileError?.message);
// 프로필 조회 실패 시 가입은 되었으므로 최소한의 유저 정보라도 리턴
return {
success: true,
user: {
id: authData.user.id,
email: authData.user.email || "",
nickname,
created_at: new Date().toISOString(),
is_no_spoiler_mode: false,
profile_url: null,
updated_at: new Date().toISOString(),
},
};
}

return {
success: true,
user: {
id: profileData.id,
email: profileData.email || "",
nickname: profileData.nickname,
created_at: profileData.created_at,
is_no_spoiler_mode: profileData.is_no_spoiler_mode,
profile_url: profileData.profile_url,
updated_at: profileData.updated_at,
},
};
};

export const signInWithEmail = async (email: string, password: string) => {
const supabase = await createClient();

const { data, error } = await supabase.auth.signInWithPassword({
email,
password,
});

if (error || !data.user) {
console.error("로그인 에러:", error?.message);
return { success: false, error: error?.message || "Login Failed" };
}

const { data: profileData, error: profileError } = await supabase
.from("profiles")
.select("*")
.eq("id", data.user.id)
.single();

if (profileError || !profileData) {
console.error("프로필 로드 실패:", profileError?.message);
return {
success: true,
user: {
id: data.user.id,
email: data.user.email || "",
nickname: data.user.user_metadata?.nickname || "",
created_at: new Date().toISOString(),
is_no_spoiler_mode: false,
profile_url: null,
updated_at: new Date().toISOString(),
},
};
}

return {
success: true,
user: {
id: profileData.id,
email: profileData.email || "",
nickname: profileData.nickname,
created_at: profileData.created_at,
is_no_spoiler_mode: profileData.is_no_spoiler_mode,
profile_url: profileData.profile_url,
updated_at: profileData.updated_at,
},
};
};

export const signOut = async () => {
const supabase = await createClient();
const { error } = await supabase.auth.signOut();

if (error) {
console.error("로그아웃 에러:", error.message);
return { success: false, error: error.message };
}

return { success: true };
};
131 changes: 131 additions & 0 deletions src/actions/commentary.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,131 @@
"use server";

import { createClient } from "@/lib/supabase/server";
import { Commentary } from "@/types/commentary";
import { TablesUpdate } from "@/types/database.types";

const SELECT_COMMENTARY_FIELDS = `
id, content, episode, img_urls, is_spoiler, created_at, updated_at, author_id, work_id,
works(title),
profiles!commentaries_author_id_fkey(nickname, profile_url)
`;

const mapRowToCommentary = (item: any): Commentary => {
const work = item.works as { title: string } | null;
const profile = item.profiles as { nickname: string; profile_url: string | null } | null;

return {
id: item.id,
content: item.content,
authorId: item.author_id,
authorNickName: profile?.nickname ?? "",
authorProfileUrl: profile?.profile_url ?? null,
categoryTitle: work?.title ?? "",
categoryId: item.work_id,
imgUrlList: item.img_urls,
isSpoiler: item.is_spoiler,
episode: item.episode ?? undefined,
createdAt: new Date(item.created_at),
updatedAt: new Date(item.updated_at),
};
};

export const getMyCommentaries = async (authorId: string): Promise<Commentary[]> => {
const supabase = await createClient();

const { data, error } = await supabase
.from("commentaries")
.select(SELECT_COMMENTARY_FIELDS)
.eq("author_id", authorId)
.order("created_at", { ascending: false });

if (error || !data) {
console.error("코멘터리 목록 조회 실패:", error?.message);
return [];
}

return data.map(mapRowToCommentary);
};

export const getCommentariesByWorkIds = async (workIds: string[]): Promise<Commentary[]> => {
if (!workIds.length) return [];

const supabase = await createClient();

const { data, error } = await supabase
.from("commentaries")
.select(SELECT_COMMENTARY_FIELDS)
.in("work_id", workIds)
.order("created_at", { ascending: false });

if (error || !data) {
console.error("코멘터리 목록 조회 실패:", error?.message);
return [];
}

return data.map(mapRowToCommentary);
};

export const getCommentary = async (commentaryId: string): Promise<Commentary | null> => {
const supabase = await createClient();

const { data, error } = await supabase
.from("commentaries")
.select(SELECT_COMMENTARY_FIELDS)
.eq("id", commentaryId)
.single();

if (error || !data) {
console.error("코멘터리 조회 실패:", error?.message);
return null;
}

return mapRowToCommentary(data);
};

export const createCommentary = async (
body: Omit<Commentary, "id" | "createdAt" | "updatedAt" | "authorNickName" | "authorProfileUrl">
): Promise<void> => {
const supabase = await createClient();

const { error } = await supabase.from("commentaries").insert({
author_id: body.authorId,
work_id: body.categoryId,
content: body.content,
episode: body.episode ?? null,
img_urls: body.imgUrlList ?? [],
is_spoiler: body.isSpoiler ?? false,
});

if (error) throw new Error(error.message);
};

export const editCommentary = async (
body: { id: string } & Partial<
Omit<Commentary, "id" | "createdAt" | "updatedAt" | "authorNickName" | "authorProfileUrl">
>
): Promise<void> => {
const supabase = await createClient();

const updates: TablesUpdate<"commentaries"> = {};
if (body.content !== undefined) updates.content = body.content;
if (body.episode !== undefined) updates.episode = body.episode ?? null;
if (body.imgUrlList !== undefined) updates.img_urls = body.imgUrlList;
if (body.isSpoiler !== undefined) updates.is_spoiler = body.isSpoiler;
if (body.categoryId !== undefined) updates.work_id = body.categoryId;

const { error } = await supabase
.from("commentaries")
.update(updates)
.eq("id", body.id);

if (error) throw new Error(error.message);
};

export const deleteCommentary = async (commentaryId: string): Promise<void> => {
const supabase = await createClient();

const { error } = await supabase.from("commentaries").delete().eq("id", commentaryId);

if (error) throw new Error(error.message);
};
98 changes: 98 additions & 0 deletions src/actions/subscription.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
"use server";

import { createClient } from "@/lib/supabase/server";
import { SubscribeCategory } from "@/types/subscription";

export const getUserSubscriptions = async (userId: string): Promise<SubscribeCategory[]> => {
const supabase = await createClient();

const { data, error } = await supabase
.from("subscriptions")
.select("id, episode, works(id, title, author, subscribe_count, usage_count, created_at)")
.eq("user_id", userId);

if (error || !data) {
console.error("구독 목록 조회 실패:", error?.message);
return [];
}

return data.map(sub => {
const work = sub.works as {
id: string;
title: string;
author: string;
subscribe_count: number;
usage_count: number;
created_at: string;
};

return {
id: sub.id,
episode: sub.episode,
detail: {
id: work.id,
title: work.title,
author: work.author,
createdAt: new Date(work.created_at),
subscribeCount: work.subscribe_count,
usageCount: work.usage_count,
},
};
});
};

export const addSubscription = async (
userId: string,
body: { id: string; episode: number | null }
): Promise<void> => {
const supabase = await createClient();

const { data: existing } = await supabase
.from("subscriptions")
.select("id")
.eq("user_id", userId)
.eq("work_id", body.id)
.maybeSingle();

if (existing) {
throw new Error("Already Subscribed");
}

const { error } = await supabase.from("subscriptions").insert({
user_id: userId,
work_id: body.id,
episode: body.episode,
});

if (error) throw new Error(error.message);
};

export const updateSubscription = async (
userId: string,
body: { id: string; episode: number | null }
): Promise<void> => {
const supabase = await createClient();

const { error } = await supabase
.from("subscriptions")
.update({ episode: body.episode })
.eq("user_id", userId)
.eq("work_id", body.id);

if (error) throw new Error(error.message);
};

export const deleteSubscription = async (
userId: string,
subscriptionId: string
): Promise<void> => {
const supabase = await createClient();

const { error } = await supabase
.from("subscriptions")
.delete()
.eq("id", subscriptionId)
.eq("user_id", userId);

if (error) throw new Error(error.message);
};
Loading