Skip to content

Commit efc90c3

Browse files
committed
fix bug in auth;
1 parent 6b9e3e0 commit efc90c3

File tree

3 files changed

+75
-22
lines changed

3 files changed

+75
-22
lines changed

app/api/stories/feed/route.ts

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
// app/api/stories/feed/route.ts
2+
import { NextRequest, NextResponse } from "next/server";
3+
import { createSupabaseAdminClient } from "@/app/utils/supabase/supabaseAdmin";
4+
5+
/**
6+
* GET /api/stories/feed — Fetch public stories feed with author info (bypasses RLS)
7+
* No auth required — this is public content.
8+
*/
9+
export async function GET(req: NextRequest) {
10+
try {
11+
const admin = createSupabaseAdminClient();
12+
13+
const { data, error } = await admin
14+
.from("stories")
15+
.select(
16+
`
17+
id, numeric_id, title, content, created_at, likes, comments_count, shares,
18+
has_audio, audio_url, mood, tags, paywall_amount, teaser, is_public,
19+
author:users!stories_author_wallet_fkey (
20+
id, name, username, avatar, wallet_address, followers_count, badges
21+
)
22+
`
23+
)
24+
.order("created_at", { ascending: false });
25+
26+
if (error) {
27+
console.error("[API /stories/feed] fetch error:", error);
28+
return NextResponse.json(
29+
{ error: "Failed to fetch feed" },
30+
{ status: 500 }
31+
);
32+
}
33+
34+
return NextResponse.json({ stories: data || [] });
35+
} catch (err: unknown) {
36+
console.error("[API /stories/feed] unexpected error:", err);
37+
return NextResponse.json(
38+
{ error: "Internal server error" },
39+
{ status: 500 }
40+
);
41+
}
42+
}

app/api/stories/route.ts

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
// app/api/stories/route.ts
22
import { NextRequest, NextResponse } from "next/server";
33
import { createSupabaseAdminClient } from "@/app/utils/supabase/supabaseAdmin";
4-
import { validateAuthOrReject, isAuthError } from "@/lib/auth";
4+
import { validateAuthOrReject, isAuthError, resolveUserId } from "@/lib/auth";
55

66
/**
77
* GET /api/stories — Fetch authenticated user's own stories (bypasses RLS)
@@ -10,14 +10,15 @@ export async function GET(req: NextRequest) {
1010
try {
1111
const authResult = await validateAuthOrReject(req);
1212
if (isAuthError(authResult)) return authResult;
13-
const authenticatedUserId = authResult;
13+
// Resolve JWT user ID → users table ID (wallet users may differ)
14+
const userId = await resolveUserId(authResult);
1415

1516
const admin = createSupabaseAdminClient();
1617

1718
const { data, error } = await admin
1819
.from("stories")
1920
.select("*")
20-
.eq("author_id", authenticatedUserId)
21+
.eq("author_id", userId)
2122
.order("story_date", { ascending: false });
2223

2324
if (error) {

app/social/SocialPageClient.tsx

Lines changed: 29 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -114,31 +114,41 @@ export default function SocialPage() {
114114

115115
// --- Fetch Data ---
116116
useEffect(() => {
117-
if (!supabase) {
118-
setIsLoading(false);
119-
return;
120-
}
121-
122117
const fetchSupabaseData = async () => {
123118
setIsLoading(true);
124119
try {
125-
// 1. Fetch ALL stories (public feed)
126-
const { data, error } = await supabase
127-
.from("stories")
128-
.select(
120+
// 1. Fetch ALL stories via API route (bypasses RLS, uses admin client)
121+
let data: any[] = [];
122+
try {
123+
const res = await fetch("/api/stories/feed");
124+
if (res.ok) {
125+
const json = await res.json();
126+
data = json.stories || [];
127+
}
128+
} catch {
129+
// API route failed — try direct Supabase as fallback
130+
}
131+
132+
// Fallback: direct Supabase query (may fail for wallet users due to RLS)
133+
if (data.length === 0 && supabase) {
134+
const { data: directData, error } = await supabase
135+
.from("stories")
136+
.select(
137+
`
138+
id, numeric_id, title, content, created_at, likes, comments_count, shares, has_audio, audio_url, mood, tags, paywall_amount, teaser,
139+
author:users!stories_author_wallet_fkey (
140+
id, name, username, avatar, wallet_address, followers_count, badges
141+
)
129142
`
130-
id, numeric_id, title, content, created_at, likes, comments_count, shares, has_audio, audio_url, mood, tags, paywall_amount, teaser,
131-
author:users!stories_author_wallet_fkey (
132-
id, name, username, avatar, wallet_address, followers_count, badges
133143
)
134-
`
135-
)
136-
.order("created_at", { ascending: false });
144+
.order("created_at", { ascending: false });
137145

138-
if (error) throw error;
146+
if (error) throw error;
147+
data = directData || [];
148+
}
139149

140-
// 2. Filter out stories with missing authors (data integrity)
141-
const validStories = (data?.filter((s) => s.author) as any[]) || [];
150+
// 2. Filter out stories with missing authors (data integrity) and non-public
151+
const validStories = (data?.filter((s: any) => s.author) as any[]) || [];
142152

143153
// 3. Collect unique author wallet addresses for follow status check
144154
const authorWallets = [
@@ -232,7 +242,7 @@ export default function SocialPage() {
232242
};
233243

234244
fetchSupabaseData();
235-
}, [supabase, address]);
245+
}, [address]);
236246

237247
// --- Event Handlers ---
238248

0 commit comments

Comments
 (0)