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
10 changes: 9 additions & 1 deletion app/api/debug/user-stream/route.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { NextResponse } from "next/server";
import { sql } from "@vercel/postgres";
import { isValidStellarAddress } from "@/utils/stellar";

export async function GET(req: Request) {
try {
Expand All @@ -13,6 +14,13 @@ export async function GET(req: Request) {
);
}

if (!isValidStellarAddress(wallet)) {
return NextResponse.json(
{ error: "Invalid wallet address. Must be a valid Stellar public key." },
{ status: 400 }
);
}

const result = await sql`
SELECT
id,
Expand All @@ -24,7 +32,7 @@ export async function GET(req: Request) {
is_live,
creator
FROM users
WHERE LOWER(wallet) = LOWER(${wallet})
WHERE wallet = ${wallet}
`;

if (result.rows.length === 0) {
Expand Down
8 changes: 8 additions & 0 deletions app/api/fetch-username/route.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { NextResponse } from "next/server";
import { sql } from "@vercel/postgres";
import { withCorsResponse } from "@/lib/with-cors-response";
import { resolve } from "path/posix";
import { isValidStellarAddress } from "@/utils/stellar";

export async function GET(req: Request) {
const { searchParams } = new URL(req.url);
Expand All @@ -15,6 +16,13 @@ export async function GET(req: Request) {
);
}

if (wallet && !isValidStellarAddress(wallet)) {
return withCorsResponse(
{ error: "Invalid wallet address. Must be a valid Stellar public key." },
400
);
}

try {
const result = wallet
? await sql`SELECT username FROM users WHERE wallet = ${wallet}`
Expand Down
8 changes: 8 additions & 0 deletions app/api/streams/[wallet]/route.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { NextResponse } from "next/server";
import { sql } from "@vercel/postgres";
import { getMuxStreamHealth } from "@/lib/mux/server";
import { isValidStellarAddress } from "@/utils/stellar";

export async function GET(
req: Request,
Expand All @@ -16,6 +17,13 @@ export async function GET(
);
}

if (!isValidStellarAddress(wallet)) {
return NextResponse.json(
{ error: "Invalid wallet address. Must be a valid Stellar public key." },
{ status: 400 }
);
}

const result = await sql`
SELECT
u.id,
Expand Down
13 changes: 11 additions & 2 deletions app/api/streams/create/route.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { NextResponse } from "next/server";
import { sql } from "@vercel/postgres";
import { createMuxStream } from "@/lib/mux/server";
import { checkExistingTableDetail } from "@/utils/validators";
import { isValidStellarAddress } from "@/utils/stellar";

export async function POST(req: Request) {
try {
Expand All @@ -24,6 +25,14 @@ export async function POST(req: Request) {
);
}

if (!isValidStellarAddress(wallet)) {
console.log("❌ Validation failed: invalid Stellar address");
return NextResponse.json(
{ error: "Invalid wallet address. Must be a valid Stellar public key." },
{ status: 400 }
);
}

if (title.length > 100) {
console.log("❌ Validation failed: title too long");
return NextResponse.json(
Expand Down Expand Up @@ -54,7 +63,7 @@ export async function POST(req: Request) {

console.log("🔍 Fetching user data...");
const userResult = await sql`
SELECT id, username, creator, mux_stream_id FROM users WHERE LOWER(wallet) = LOWER(${wallet})
SELECT id, username, creator, mux_stream_id FROM users WHERE wallet = ${wallet}
`;

if (userResult.rows.length === 0) {
Expand Down Expand Up @@ -173,7 +182,7 @@ export async function POST(req: Request) {
streamkey = ${muxStream.streamKey},
creator = ${JSON.stringify(updatedCreator)},
updated_at = CURRENT_TIMESTAMP
WHERE LOWER(wallet) = LOWER(${wallet})
WHERE wallet = ${wallet}
`;
console.log("✅ User updated successfully with stream data");
} catch (dbError) {
Expand Down
12 changes: 10 additions & 2 deletions app/api/streams/delete-get/route.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { NextResponse } from "next/server";
import { sql } from "@vercel/postgres";
import { deleteMuxStream } from "@/lib/mux/server";
import { isValidStellarAddress } from "@/utils/stellar";

export async function GET(req: Request) {
try {
Expand All @@ -14,12 +15,19 @@ export async function GET(req: Request) {
);
}

if (!isValidStellarAddress(wallet)) {
return NextResponse.json(
{ error: "Invalid wallet address. Must be a valid Stellar public key." },
{ status: 400 }
);
}

console.log(`🔧 Force deleting stream for wallet: ${wallet}`);

const userResult = await sql`
SELECT id, username, mux_stream_id, is_live
FROM users
WHERE LOWER(wallet) = LOWER(${wallet})
WHERE wallet = ${wallet}
`;

if (userResult.rows.length === 0) {
Expand Down Expand Up @@ -74,7 +82,7 @@ export async function GET(req: Request) {
current_viewers = 0,
stream_started_at = NULL,
updated_at = CURRENT_TIMESTAMP
WHERE LOWER(wallet) = LOWER(${wallet})
WHERE wallet = ${wallet}
`;

console.log("✅ Force delete completed!");
Expand Down
8 changes: 8 additions & 0 deletions app/api/streams/delete/route.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { NextResponse } from "next/server";
import { sql } from "@vercel/postgres";
import { deleteMuxStream } from "@/lib/mux/server";
import { isValidStellarAddress } from "@/utils/stellar";

export async function DELETE(req: Request) {
try {
Expand All @@ -13,6 +14,13 @@ export async function DELETE(req: Request) {
);
}

if (!isValidStellarAddress(wallet)) {
return NextResponse.json(
{ error: "Invalid wallet address. Must be a valid Stellar public key." },
{ status: 400 }
);
}

const userResult = await sql`
SELECT id, username, mux_stream_id, is_live
FROM users
Expand Down
10 changes: 9 additions & 1 deletion app/api/streams/key/route.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { NextResponse } from "next/server";
import { sql } from "@vercel/postgres";
import { getWalletOrDevDefault } from "@/lib/dev-mode";
import { isValidStellarAddress } from "@/utils/stellar";

/**
* GET /api/streams/key
Expand All @@ -21,6 +22,13 @@ export async function GET(req: Request) {
);
}

if (!isValidStellarAddress(wallet)) {
return NextResponse.json(
{ error: "Invalid wallet address. Must be a valid Stellar public key." },
{ status: 400 }
);
}

const userResult = await sql`
SELECT
id,
Expand All @@ -30,7 +38,7 @@ export async function GET(req: Request) {
mux_playback_id,
is_live
FROM users
WHERE LOWER(wallet) = LOWER(${wallet})
WHERE wallet = ${wallet}
`;

if (userResult.rows.length === 0) {
Expand Down
15 changes: 15 additions & 0 deletions app/api/streams/start/route.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { NextResponse } from "next/server";
import { sql } from "@vercel/postgres";
import { getMuxStreamHealth } from "@/lib/mux/server";
import { isValidStellarAddress } from "@/utils/stellar";

export async function POST(req: Request) {
try {
Expand All @@ -13,6 +14,13 @@ export async function POST(req: Request) {
);
}

if (!isValidStellarAddress(wallet)) {
return NextResponse.json(
{ error: "Invalid wallet address. Must be a valid Stellar public key." },
{ status: 400 }
);
}

const userResult = await sql`
SELECT id, username, mux_stream_id, is_live, mux_playback_id
FROM users
Expand Down Expand Up @@ -101,6 +109,13 @@ export async function DELETE(req: Request) {
);
}

if (!isValidStellarAddress(wallet)) {
return NextResponse.json(
{ error: "Invalid wallet address. Must be a valid Stellar public key." },
{ status: 400 }
);
}

const userResult = await sql`
SELECT id, mux_stream_id, is_live
FROM users
Expand Down
8 changes: 8 additions & 0 deletions app/api/streams/update/route.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { NextResponse } from "next/server";
import { sql } from "@vercel/postgres";
import { uploadImage } from "@/utils/upload/cloudinary";
import { isValidStellarAddress } from "@/utils/stellar";

export async function PATCH(req: Request) {
try {
Expand All @@ -14,6 +15,13 @@ export async function PATCH(req: Request) {
);
}

if (!isValidStellarAddress(wallet)) {
return NextResponse.json(
{ error: "Invalid wallet address. Must be a valid Stellar public key." },
{ status: 400 }
);
}

if (title && title.length > 100) {
return NextResponse.json(
{ error: "Title must be 100 characters or less" },
Expand Down
11 changes: 10 additions & 1 deletion app/api/users/follow/route.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,17 @@
import { NextRequest, NextResponse } from "next/server";
import { sql } from "@vercel/postgres";
import { isValidStellarAddress } from "@/utils/stellar";

export async function POST(req: NextRequest) {
const { callerUsername, receiverUsername, action } = await req.json();
const { callerUsername, receiverUsername, action, wallet } = await req.json();

// Validate wallet if provided (for future wallet-based follow operations)
if (wallet && !isValidStellarAddress(wallet)) {
return NextResponse.json(
{ error: "Invalid wallet address. Must be a valid Stellar public key." },
{ status: 400 }
);
}

if (
!callerUsername ||
Expand Down
8 changes: 8 additions & 0 deletions app/api/users/register/route.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { checkExistingTableDetail, validateEmail } from "@/utils/validators";
import { sql } from "@vercel/postgres";
import { sendWelcomeRegistrationEmail } from "@/utils/send-email";
import { createMuxStream } from "@/lib/mux/server";
import { isValidStellarAddress } from "@/utils/stellar";

async function handler(req: Request) {
try {
Expand Down Expand Up @@ -78,6 +79,13 @@ async function handler(req: Request) {
return NextResponse.json({ error: "Wallet is required" }, { status: 400 });
}

if (!isValidStellarAddress(wallet)) {
return NextResponse.json(
{ error: "Invalid wallet address. Must be a valid Stellar public key." },
{ status: 400 }
);
}

if (!email) {
return NextResponse.json({ error: "Email is required" }, { status: 400 });
}
Expand Down
17 changes: 12 additions & 5 deletions app/api/users/updates/[wallet]/route.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,18 +7,25 @@ import os from "os";
import { validateEmail } from "@/utils/validators";
import { validateUserUpdate } from "../../../../../utils/userValidators";
import { UserUpdateInput } from "../../../../../types/user";
import { isValidStellarAddress } from "@/utils/stellar";

export async function PUT(
req: NextRequest,
{ params }: { params: Promise<{ wallet: string }> }
) {
try {
const { wallet } = await params;
const normalizedWallet = wallet.toLowerCase();

if (!isValidStellarAddress(wallet)) {
return NextResponse.json(
{ error: "Invalid wallet address. Must be a valid Stellar public key." },
{ status: 400 }
);
}

// Fetching current user data
const existingResult = await sql`
SELECT * FROM users WHERE LOWER(wallet) = LOWER(${normalizedWallet})
SELECT * FROM users WHERE wallet = ${wallet}
`;
const user = existingResult.rows[0];
if (!user) {
Expand Down Expand Up @@ -111,7 +118,7 @@ export async function PUT(

if (email && email !== user.email) {
const emailExists = await sql`
SELECT id FROM users WHERE email = ${email} AND wallet != ${normalizedWallet}
SELECT id FROM users WHERE email = ${email} AND wallet != ${wallet}
`;
if (emailExists.rows.length > 0) {
return NextResponse.json(
Expand All @@ -124,7 +131,7 @@ export async function PUT(
// Username uniqueness
if (username && username !== user.username) {
const usernameExists = await sql`
SELECT id FROM users WHERE username = ${username} AND wallet != ${normalizedWallet}
SELECT id FROM users WHERE username = ${username} AND wallet != ${wallet}
`;
if (usernameExists.rows.length > 0) {
return NextResponse.json(
Expand Down Expand Up @@ -169,7 +176,7 @@ export async function PUT(
emailnotifications = ${emailNotifications},
creator = ${creator ? JSON.stringify(creator) : user.creator},
updated_at = CURRENT_TIMESTAMP
WHERE LOWER(wallet) = LOWER(${normalizedWallet})
WHERE wallet = ${wallet}
RETURNING id, username, email, streamkey, avatar, bio, sociallinks, emailverified, emailnotifications, creator, wallet, created_at, updated_at
`;

Expand Down
11 changes: 8 additions & 3 deletions app/api/users/wallet/[wallet]/route.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { NextResponse } from "next/server";
import { sql } from "@vercel/postgres";
import { isValidStellarAddress } from "@/utils/stellar";

export async function GET(
req: Request,
Expand All @@ -9,11 +10,15 @@ export async function GET(
const { wallet } = await params;
console.log("API: Fetching user for wallet:", wallet);

// Normalize the wallet address to lowercase for consistent comparison
const normalizedWallet = wallet.toLowerCase();
if (!isValidStellarAddress(wallet)) {
return NextResponse.json(
{ error: "Invalid wallet address. Must be a valid Stellar public key." },
{ status: 400 }
);
}

const result = await sql`
SELECT * FROM users WHERE LOWER(wallet) = ${normalizedWallet}
SELECT * FROM users WHERE wallet = ${wallet}
`;

console.log("API: Query result rows:", result.rowCount);
Expand Down
5 changes: 5 additions & 0 deletions utils/stellar.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import { StrKey } from "@stellar/stellar-sdk";

export function isValidStellarAddress(address: string): boolean {
return StrKey.isValidEd25519PublicKey(address);
}
Loading