Wire client to server API for posts and onboarding to avoid RLS issues
cgen-4d11caba61394f70aed01c897057e1b2
This commit is contained in:
parent
34c1d995e9
commit
9748c0c331
1 changed files with 20 additions and 0 deletions
|
|
@ -235,6 +235,14 @@ export const achievementService = {
|
|||
// Community Services
|
||||
export const communityService = {
|
||||
async getPosts(limit = 10): Promise<CommunityPost[]> {
|
||||
// Prefer server API (service role) to avoid RLS issues
|
||||
try {
|
||||
const resp = await fetch(`/api/posts?limit=${limit}`);
|
||||
if (resp.ok) {
|
||||
const data = await resp.json();
|
||||
if (Array.isArray(data) && data.length) return data;
|
||||
}
|
||||
} catch {}
|
||||
try {
|
||||
const { data, error } = await supabase
|
||||
.from("community_posts")
|
||||
|
|
@ -281,6 +289,14 @@ export const communityService = {
|
|||
"id" | "created_at" | "updated_at" | "likes_count" | "comments_count"
|
||||
>,
|
||||
): Promise<CommunityPost> {
|
||||
try {
|
||||
const resp = await fetch(`/api/posts`, {
|
||||
method: "POST",
|
||||
headers: { "Content-Type": "application/json" },
|
||||
body: JSON.stringify(post),
|
||||
});
|
||||
if (resp.ok) return await resp.json();
|
||||
} catch {}
|
||||
try {
|
||||
const { data, error } = await supabase
|
||||
.from("community_posts")
|
||||
|
|
@ -316,6 +332,10 @@ export const communityService = {
|
|||
},
|
||||
|
||||
async getUserPosts(userId: string): Promise<CommunityPost[]> {
|
||||
try {
|
||||
const resp = await fetch(`/api/user/${userId}/posts`);
|
||||
if (resp.ok) return await resp.json();
|
||||
} catch {}
|
||||
try {
|
||||
const { data, error } = await supabase
|
||||
.from("community_posts")
|
||||
|
|
|
|||
Loading…
Reference in a new issue