neon db load user
parent
efcd5b54ed
commit
af0438fa91
1
.env
1
.env
|
@ -1,4 +1,5 @@
|
|||
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
|
||||
|
|
|
@ -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);
|
||||
|
|
|
@ -1,13 +1,14 @@
|
|||
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,
|
||||
|
@ -19,14 +20,11 @@ export async function POST(request: Request) {
|
|||
${email},
|
||||
${clerkId}
|
||||
)
|
||||
RETURNING *;
|
||||
`;
|
||||
console.log(response);
|
||||
|
||||
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 });
|
||||
}
|
||||
}
|
||||
|
||||
|
|
11
lib/fetch.ts
11
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 = <T>(url: string, options?: RequestInit) => {
|
||||
const [data, setData] = useState<T | null>(null);
|
||||
const [loading, setLoading] = useState(false);
|
||||
|
|
40
server.js
40
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 {
|
||||
|
||||
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");
|
||||
});
|
||||
|
||||
|
|
Loading…
Reference in New Issue