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_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
|
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_GEOAPIFY_API_KEY=0e7ccfac62054b0f846032bff6bae5c9
|
||||||
EXPO_PUBLIC_GOOGLE_API_KEY=AIzaSyD10tc4ec2FFVXQcWDXCT2CeBy-jwbRZB8
|
EXPO_PUBLIC_GOOGLE_API_KEY=AIzaSyD10tc4ec2FFVXQcWDXCT2CeBy-jwbRZB8
|
||||||
|
|
|
@ -49,13 +49,13 @@ const SignUp = () => {
|
||||||
})
|
})
|
||||||
if (CompleteSignUp.status === "complete") {
|
if (CompleteSignUp.status === "complete") {
|
||||||
const clerkId=signUp.createdUserId;
|
const clerkId=signUp.createdUserId;
|
||||||
const response= await fetchAPI("http://192.168.29.174:3000/api/users",{
|
const response= await fetchAPI("/api/users",{
|
||||||
method:"POST",
|
method:"POST",
|
||||||
headers:{"Content-Type":"application/json"},
|
headers:{"Content-Type":"application/json"},
|
||||||
body:JSON.stringify({
|
body:JSON.stringify({
|
||||||
name:form.name,
|
name:form.name,
|
||||||
email:form.email,
|
email:form.email,
|
||||||
clerkId,
|
clerkId:clerkId,
|
||||||
}),
|
}),
|
||||||
});
|
});
|
||||||
console.log("User API Response:", response);
|
console.log("User API Response:", response);
|
||||||
|
|
|
@ -1,13 +1,14 @@
|
||||||
import { neon } from "@neondatabase/serverless";
|
import { neon } from "@neondatabase/serverless";
|
||||||
|
|
||||||
export async function POST(request: Request) {
|
export async function POST(request: Request) {
|
||||||
try {
|
try {
|
||||||
const sql = neon(process.env.DATABASE_URL as string);
|
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) {
|
if (!name || !email || !clerkId) {
|
||||||
return Response.json({ error: "Miss required fields" }, { status: 400 });
|
return Response.json({ error: "Miss required fields" }, { status: 400 });
|
||||||
// return new Response(JSON.stringify({ error: "Missing required fields" }), { status: 400 });
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
const response = await sql`
|
const response = await sql`
|
||||||
INSERT INTO users (
|
INSERT INTO users (
|
||||||
name,
|
name,
|
||||||
|
@ -19,14 +20,11 @@ export async function POST(request: Request) {
|
||||||
${email},
|
${email},
|
||||||
${clerkId}
|
${clerkId}
|
||||||
)
|
)
|
||||||
RETURNING *;
|
|
||||||
`;
|
`;
|
||||||
console.log(response);
|
|
||||||
return new Response(JSON.stringify({ data: response }), { status: 201 });
|
return new Response(JSON.stringify({ data: response }), { status: 201 });
|
||||||
}
|
} catch (error) {
|
||||||
catch (error) {
|
console.log("DataBase Error:", error);
|
||||||
console.log("DataBase Error:",error);
|
return new Response(JSON.stringify({ error: error }), { status: 500 });
|
||||||
return new Response(JSON.stringify({ error: "DataBase Error" }), { status: 500 });
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
11
lib/fetch.ts
11
lib/fetch.ts
|
@ -1,17 +1,20 @@
|
||||||
import {useState, useEffect, useCallback} from "react";
|
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 {
|
try {
|
||||||
const response = await fetch(url, options);
|
const response = await fetch(`${API_BASE_URL}${endpoint}`, options);
|
||||||
if (!response.ok) {
|
if (!response.ok) {
|
||||||
throw new Error(`HTTP error! status: ${response.status}`);
|
throw new Error(`HTTP error! status: ${response.status}`);
|
||||||
}
|
}
|
||||||
return await response.json();
|
return await response.json();
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
console.error("Fetch error:", error);
|
console.error("Fetch error:", error instanceof Error ? error.message : error);
|
||||||
throw error;
|
throw error;
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
export const useFetch = <T>(url: string, options?: RequestInit) => {
|
export const useFetch = <T>(url: string, options?: RequestInit) => {
|
||||||
const [data, setData] = useState<T | null>(null);
|
const [data, setData] = useState<T | null>(null);
|
||||||
const [loading, setLoading] = useState(false);
|
const [loading, setLoading] = useState(false);
|
||||||
|
|
40
server.js
40
server.js
|
@ -1,18 +1,23 @@
|
||||||
require("dotenv").config();
|
require("dotenv").config();
|
||||||
const http = require("http");
|
const http = require("http");
|
||||||
const { neon } = require("@neondatabase/serverless");
|
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 sql = neon(process.env.DATABASE_URL);
|
||||||
|
|
||||||
const requestHandler = async (req, res) => {
|
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 = "";
|
let body = "";
|
||||||
|
|
||||||
req.on("data", (chunk) => {
|
req.on("data", (chunk) => {
|
||||||
body += chunk.toString();
|
body += chunk.toString();
|
||||||
});
|
});
|
||||||
|
|
||||||
req.on("end", async () => {
|
req.on("end", async () => {
|
||||||
console.log("🔹 Received signup request:", body);
|
|
||||||
try {
|
try {
|
||||||
const { name, email, clerkId } = JSON.parse(body);
|
const { name, email, clerkId } = JSON.parse(body);
|
||||||
|
|
||||||
|
@ -21,28 +26,43 @@ const requestHandler = async (req, res) => {
|
||||||
res.end(JSON.stringify({ error: "Missing required fields" }));
|
res.end(JSON.stringify({ error: "Missing required fields" }));
|
||||||
return;
|
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`
|
const result = await sql`
|
||||||
INSERT INTO users (name, email, clerk_id)
|
INSERT INTO users (name, email, clerk_id)
|
||||||
VALUES (${name}, ${email}, ${clerkId})
|
VALUES (${name}, ${email}, ${clerkId})
|
||||||
RETURNING *;
|
RETURNING *;
|
||||||
`;
|
`;
|
||||||
console.log("✅ User inserted:", result[0]);
|
|
||||||
res.writeHead(201, { "Content-Type": "application/json" });
|
res.writeHead(201, { "Content-Type": "application/json" });
|
||||||
res.end(JSON.stringify(result[0]));
|
res.end(JSON.stringify(result[0]));
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
console.error("Database Error:", err);
|
console.error("❌ Database Error:", err);
|
||||||
res.writeHead(500, { "Content-Type": "application/json" });
|
res.writeHead(500, { "Content-Type": "application/json" });
|
||||||
res.end(JSON.stringify({ error: "Database error" }));
|
res.end(JSON.stringify({ error: "Database error" }));
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
} else {
|
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Fallback for unrecognized routes
|
||||||
res.writeHead(404, { "Content-Type": "text/plain" });
|
res.writeHead(404, { "Content-Type": "text/plain" });
|
||||||
res.end("Not Found");
|
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");
|
console.log("✅ Server running at http://0.0.0.0:3000");
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue