diff --git a/.env b/.env index 1e3887b..9af0e16 100644 --- a/.env +++ b/.env @@ -1,6 +1,7 @@ EXPO_PUBLIC_CLERK_PUBLISHABLE_KEY=pk_test_bWVhc3VyZWQtcGFudGhlci04Mi5jbGVyay5hY2NvdW50cy5kZXYk +EXPO_PUBLIC_API_URL=http://192.168.29.174:3000 DATABASE_URL=postgresql://jsm_uber_owner:npg_wsprVk4dJ8XW@ep-rapid-field-a1prd9ss-pooler.ap-southeast-1.aws.neon.tech/jsm_uber?sslmode=require EXPO_PUBLIC_GEOAPIFY_API_KEY=0e7ccfac62054b0f846032bff6bae5c9 EXPO_PUBLIC_GOOGLE_API_KEY=AIzaSyD10tc4ec2FFVXQcWDXCT2CeBy-jwbRZB8 EXPO_PUBLIC_STRIPE_PUBLISHABLE_KEY=pk_test_51RBCg0EEiRKTsjR3t4krSuirMpl6cf28QcyIpMlWD6eIZoU9qBCGBhoSwr9zlRLEQsoUwCusZupgnOMmOAyYG19U00S86heUPX -STRIPE_SECRET_KEY=sk_test_51RBCg0EEiRKTsjR3wFKcVMiOhedbosVqKnl5fCYhTDsQkXZVx7UJqUiXRu6b9ghbP6yrqMnom1GePKtUAgVl9twg00dJzPIgBz \ No newline at end of file +STRIPE_SECRET_KEY=sk_test_51RBCg0EEiRKTsjR3wFKcVMiOhedbosVqKnl5fCYhTDsQkXZVx7UJqUiXRu6b9ghbP6yrqMnom1GePKtUAgVl9twg00dJzPIgBz diff --git a/app/(auth)/sign-up.tsx b/app/(auth)/sign-up.tsx index a9ea1e5..2e18569 100644 --- a/app/(auth)/sign-up.tsx +++ b/app/(auth)/sign-up.tsx @@ -49,13 +49,13 @@ const SignUp = () => { }) if (CompleteSignUp.status === "complete") { const clerkId=signUp.createdUserId; - const response= await fetchAPI("http://192.168.29.174:3000/api/users",{ + const response= await fetchAPI("/api/users",{ method:"POST", headers:{"Content-Type":"application/json"}, body:JSON.stringify({ name:form.name, email:form.email, - clerkId, + clerkId:clerkId, }), }); console.log("User API Response:", response); diff --git a/app/api/user+api.ts b/app/api/user+api.ts index 9c70e6e..704fe9d 100644 --- a/app/api/user+api.ts +++ b/app/api/user+api.ts @@ -1,32 +1,30 @@ import { neon } from "@neondatabase/serverless"; + export async function POST(request: Request) { try { const sql = neon(process.env.DATABASE_URL as string); - const { name, email, clerkId } = await request.json() + const { name, email, clerkId } = await request.json(); + if (!name || !email || !clerkId) { return Response.json({ error: "Miss required fields" }, { status: 400 }); - // return new Response(JSON.stringify({ error: "Missing required fields" }), { status: 400 }); - } + const response = await sql` - INSERT INTO users ( - name, - email, - clerk_id - ) - VALUES ( - ${name}, - ${email}, - ${clerkId} - ) - RETURNING *; - `; - console.log(response); + INSERT INTO users ( + name, + email, + clerk_id + ) + VALUES ( + ${name}, + ${email}, + ${clerkId} + ) + `; + return new Response(JSON.stringify({ data: response }), { status: 201 }); - } - catch (error) { - console.log("DataBase Error:",error); - return new Response(JSON.stringify({ error: "DataBase Error" }), { status: 500 }); + } catch (error) { + console.log("DataBase Error:", error); + return new Response(JSON.stringify({ error: error }), { status: 500 }); } } - diff --git a/lib/fetch.ts b/lib/fetch.ts index b510fb3..85aa1ab 100644 --- a/lib/fetch.ts +++ b/lib/fetch.ts @@ -1,17 +1,20 @@ import {useState, useEffect, useCallback} from "react"; -const API_BASE_URL = "http://192.168.29.1:3000" -export const fetchAPI = async (url: string, options?: RequestInit) => { + +const API_BASE_URL = "http://192.168.29.174:3000"; + +export const fetchAPI = async (endpoint: string, options?: RequestInit) => { try { - const response = await fetch(url, options); + const response = await fetch(`${API_BASE_URL}${endpoint}`, options); if (!response.ok) { throw new Error(`HTTP error! status: ${response.status}`); } return await response.json(); } catch (error) { - console.error("Fetch error:", error); + console.error("Fetch error:", error instanceof Error ? error.message : error); throw error; } }; + export const useFetch = (url: string, options?: RequestInit) => { const [data, setData] = useState(null); const [loading, setLoading] = useState(false); @@ -29,11 +32,11 @@ export const useFetch = (url: string, options?: RequestInit) => { } finally { setLoading(false); } - }, [url, options]); + }, [url, options]); useEffect(() => { fetchData(); }, [fetchData]); return {data, loading, error, refetch: fetchData}; -}; \ No newline at end of file +}; diff --git a/server.js b/server.js index 299deb6..846c46e 100644 --- a/server.js +++ b/server.js @@ -1,18 +1,23 @@ require("dotenv").config(); const http = require("http"); const { neon } = require("@neondatabase/serverless"); -console.log("Server Started") -console.log("DATABASE_URL:", process.env.DATABASE_URL); + const sql = neon(process.env.DATABASE_URL); + const requestHandler = async (req, res) => { - if (req.method === "POST" && req.url === "/signup") { + if (req.method === "GET" && req.url === "/api/users") { + res.writeHead(200, { "Content-Type": "application/json" }); + res.end(JSON.stringify({ message: "GET is working" })); + return; + } + if (req.method === "POST" && req.url === "/api/users") { let body = ""; + req.on("data", (chunk) => { body += chunk.toString(); }); req.on("end", async () => { - console.log("🔹 Received signup request:", body); try { const { name, email, clerkId } = JSON.parse(body); @@ -21,28 +26,43 @@ const requestHandler = async (req, res) => { res.end(JSON.stringify({ error: "Missing required fields" })); return; } - console.log("🔹 Inserting user into database:", { name, email, clerkId }); + + // Check if user already exists + const existing = await sql` + SELECT * FROM users WHERE clerk_id = ${clerkId} + `; + + if (existing.length > 0) { + res.writeHead(200, { "Content-Type": "application/json" }); + res.end(JSON.stringify(existing[0])); + return; + } + + // Insert new user const result = await sql` INSERT INTO users (name, email, clerk_id) VALUES (${name}, ${email}, ${clerkId}) RETURNING *; `; - console.log("✅ User inserted:", result[0]); + res.writeHead(201, { "Content-Type": "application/json" }); res.end(JSON.stringify(result[0])); } catch (err) { - console.error("Database Error:", err); + console.error("❌ Database Error:", err); res.writeHead(500, { "Content-Type": "application/json" }); res.end(JSON.stringify({ error: "Database error" })); } }); - } else { - res.writeHead(404, { "Content-Type": "text/plain" }); - res.end("Not Found"); + + return; } + + // Fallback for unrecognized routes + res.writeHead(404, { "Content-Type": "text/plain" }); + res.end("Not Found"); }; -http.createServer(requestHandler).listen(3000,"0.0.0.0", () => { +http.createServer(requestHandler).listen(3000, "0.0.0.0", () => { console.log("✅ Server running at http://0.0.0.0:3000"); });