From 39ff36941317bbcb2b0ff44044fc3bc32c755bec Mon Sep 17 00:00:00 2001 From: Jakub Satora Date: Fri, 27 Sep 2024 12:19:16 +0200 Subject: [PATCH] Database is conected , Table is created for users --- .env.local | 4 +- app/(api)/user+api.ts | 34 ++++++++++++++ app/(auth)/sign-up.tsx | 9 +++- lib/fetch.ts | 40 ++++++++++++++++ package-lock.json | 102 +++++++++++++++++++++++++++++++++++++++++ package.json | 1 + 6 files changed, 188 insertions(+), 2 deletions(-) create mode 100644 app/(api)/user+api.ts create mode 100644 lib/fetch.ts diff --git a/.env.local b/.env.local index 3afd263..6ee300b 100644 --- a/.env.local +++ b/.env.local @@ -1 +1,3 @@ -EXPO_PUBLIC_CLERK_PUBLISHABLE_KEY=pk_test_cHJvbXB0LW1hZ2dvdC02MC5jbGVyay5hY2NvdW50cy5kZXYk \ No newline at end of file +EXPO_PUBLIC_CLERK_PUBLISHABLE_KEY=pk_test_cHJvbXB0LW1hZ2dvdC02MC5jbGVyay5hY2NvdW50cy5kZXYk +DATABASE_URL=postgresql://ryde_owner:0OwLX1QrRPqT@ep-rapid-water-a2owt30g.eu-central-1.aws.neon.tech/ryde?sslmode=require +EXPO_PUBLIC_SERVER_URL=https://uber.dev/ \ No newline at end of file diff --git a/app/(api)/user+api.ts b/app/(api)/user+api.ts new file mode 100644 index 0000000..95396e4 --- /dev/null +++ b/app/(api)/user+api.ts @@ -0,0 +1,34 @@ +import { neon } from "@neondatabase/serverless"; + +export async function POST(request: Request) { + try { + const sql = neon(`${process.env.DATABASE_URL}`); + const { name, email, clerkId } = await request.json(); + + if (!name || !email || !clerkId) { + return Response.json( + { error: "Missing required fields" }, + { status: 400 }, + ); + } + + const response = await sql` + INSERT INTO users ( + name, + email, + clerk_id + ) + VALUES ( + ${name}, + ${email}, + ${clerkId} + );`; + + return new Response(JSON.stringify({ data: response }), { + status: 201, + }); + } catch (error) { + console.error("Error creating user:", error); + return Response.json({ error: "Internal Server Error" }, { status: 500 }); + } +} \ No newline at end of file diff --git a/app/(auth)/sign-up.tsx b/app/(auth)/sign-up.tsx index 4ea6db3..39add10 100644 --- a/app/(auth)/sign-up.tsx +++ b/app/(auth)/sign-up.tsx @@ -7,6 +7,7 @@ import CustomButton from "@/components/CustomButton"; import InputField from "@/components/InputField"; import OAuth from "@/components/OAuth"; import { icons, images } from "@/constants"; +import { fetchAPI } from "@/lib/fetch"; const SignUp = () => { const { isLoaded, signUp, setActive } = useSignUp() @@ -56,7 +57,13 @@ const SignUp = () => { }) if (completeSignUp.status === 'complete') { - +await fetchAPI("/(api)/user",{method:"POST", + body:JSON.stringify({ + name:form.name, + email:form.email, + clerkId:completeSignUp.createdUserId + }), +}) await setActive({ session: completeSignUp.createdSessionId }) setVerification({ ...verification, state: "sucess" }) } else { diff --git a/lib/fetch.ts b/lib/fetch.ts new file mode 100644 index 0000000..b261985 --- /dev/null +++ b/lib/fetch.ts @@ -0,0 +1,40 @@ +import {useState, useEffect, useCallback} from "react"; + +export const fetchAPI = async (url: string, options?: RequestInit) => { + try { + const response = await fetch(url, options); + if (!response.ok) { + new Error(`HTTP error! status: ${response.status}`); + } + return await response.json(); + } catch (error) { + console.error("Fetch error:", error); + throw error; + } +}; + +export const useFetch = (url: string, options?: RequestInit) => { + const [data, setData] = useState(null); + const [loading, setLoading] = useState(false); + const [error, setError] = useState(null); + + const fetchData = useCallback(async () => { + setLoading(true); + setError(null); + + try { + const result = await fetchAPI(url, options); + setData(result.data); + } catch (err) { + setError((err as Error).message); + } finally { + setLoading(false); + } + }, [url, options]); + + useEffect(() => { + fetchData(); + }, [fetchData]); + + return {data, loading, error, refetch: fetchData}; +}; \ No newline at end of file diff --git a/package-lock.json b/package-lock.json index f774046..8950453 100644 --- a/package-lock.json +++ b/package-lock.json @@ -11,6 +11,7 @@ "@clerk/clerk-expo": "^2.2.14", "@expo/metro-runtime": "~3.2.3", "@expo/vector-icons": "^14.0.2", + "@neondatabase/serverless": "^0.9.5", "@react-navigation/native": "^6.0.2", "expo": "~51.0.28", "expo-constants": "~16.0.2", @@ -4782,6 +4783,14 @@ "@jridgewell/sourcemap-codec": "^1.4.14" } }, + "node_modules/@neondatabase/serverless": { + "version": "0.9.5", + "resolved": "https://registry.npmjs.org/@neondatabase/serverless/-/serverless-0.9.5.tgz", + "integrity": "sha512-siFas6gItqv6wD/pZnvdu34wEqgG3nSE6zWZdq5j2DEsa+VvX8i/5HXJOo06qrw5axPXn+lGCxeR+NLaSPIXug==", + "dependencies": { + "@types/pg": "8.11.6" + } + }, "node_modules/@nodelib/fs.scandir": { "version": "2.1.5", "resolved": "https://registry.npmjs.org/@nodelib/fs.scandir/-/fs.scandir-2.1.5.tgz", @@ -7341,6 +7350,16 @@ "resolved": "https://registry.npmjs.org/@types/parse-json/-/parse-json-4.0.2.tgz", "integrity": "sha512-dISoDXWWQwUquiKsyZ4Ng+HX2KsPL7LyHKHQwgGFEA3IaKac4Obd+h2a/a6waisAoepJlBcx9paWqjA8/HVjCw==" }, + "node_modules/@types/pg": { + "version": "8.11.6", + "resolved": "https://registry.npmjs.org/@types/pg/-/pg-8.11.6.tgz", + "integrity": "sha512-/2WmmBXHLsfRqzfHW7BNZ8SbYzE8OSk7i3WjFYvfgRHj7S1xj+16Je5fUKv3lVdVzk/zn9TXOqf+avFCFIE0yQ==", + "dependencies": { + "@types/node": "*", + "pg-protocol": "*", + "pg-types": "^4.0.1" + } + }, "node_modules/@types/prop-types": { "version": "15.7.13", "resolved": "https://registry.npmjs.org/@types/prop-types/-/prop-types-15.7.13.tgz", @@ -17012,6 +17031,11 @@ "url": "https://github.com/sponsors/ljharb" } }, + "node_modules/obuf": { + "version": "1.1.2", + "resolved": "https://registry.npmjs.org/obuf/-/obuf-1.1.2.tgz", + "integrity": "sha512-PX1wu0AmAdPqOL1mWhqmlOd8kOIZQwGZw6rh7uby9fTc5lhaOWFLX3I6R1hrF9k3zUY40e6igsLGkDXK92LJNg==" + }, "node_modules/on-finished": { "version": "2.3.0", "resolved": "https://registry.npmjs.org/on-finished/-/on-finished-2.3.0.tgz", @@ -17332,6 +17356,44 @@ "node": ">=8" } }, + "node_modules/pg-int8": { + "version": "1.0.1", + "resolved": "https://registry.npmjs.org/pg-int8/-/pg-int8-1.0.1.tgz", + "integrity": "sha512-WCtabS6t3c8SkpDBUlb1kjOs7l66xsGdKpIPZsg4wR+B3+u9UAum2odSsF9tnvxg80h4ZxLWMy4pRjOsFIqQpw==", + "engines": { + "node": ">=4.0.0" + } + }, + "node_modules/pg-numeric": { + "version": "1.0.2", + "resolved": "https://registry.npmjs.org/pg-numeric/-/pg-numeric-1.0.2.tgz", + "integrity": "sha512-BM/Thnrw5jm2kKLE5uJkXqqExRUY/toLHda65XgFTBTFYZyopbKjBe29Ii3RbkvlsMoFwD+tHeGaCjjv0gHlyw==", + "engines": { + "node": ">=4" + } + }, + "node_modules/pg-protocol": { + "version": "1.7.0", + "resolved": "https://registry.npmjs.org/pg-protocol/-/pg-protocol-1.7.0.tgz", + "integrity": "sha512-hTK/mE36i8fDDhgDFjy6xNOG+LCorxLG3WO17tku+ij6sVHXh1jQUJ8hYAnRhNla4QVD2H8er/FOjc/+EgC6yQ==" + }, + "node_modules/pg-types": { + "version": "4.0.2", + "resolved": "https://registry.npmjs.org/pg-types/-/pg-types-4.0.2.tgz", + "integrity": "sha512-cRL3JpS3lKMGsKaWndugWQoLOCoP+Cic8oseVcbr0qhPzYD5DWXK+RZ9LY9wxRf7RQia4SCwQlXk0q6FCPrVng==", + "dependencies": { + "pg-int8": "1.0.1", + "pg-numeric": "1.0.2", + "postgres-array": "~3.0.1", + "postgres-bytea": "~3.0.0", + "postgres-date": "~2.1.0", + "postgres-interval": "^3.0.0", + "postgres-range": "^1.1.1" + }, + "engines": { + "node": ">=10" + } + }, "node_modules/picocolors": { "version": "1.1.0", "resolved": "https://registry.npmjs.org/picocolors/-/picocolors-1.1.0.tgz", @@ -17665,6 +17727,46 @@ "resolved": "https://registry.npmjs.org/postcss-value-parser/-/postcss-value-parser-4.2.0.tgz", "integrity": "sha512-1NNCs6uurfkVbeXG4S8JFT9t19m45ICnif8zWLd5oPSZ50QnwMfK+H3jv408d4jw/7Bttv5axS5IiHoLaVNHeQ==" }, + "node_modules/postgres-array": { + "version": "3.0.2", + "resolved": "https://registry.npmjs.org/postgres-array/-/postgres-array-3.0.2.tgz", + "integrity": "sha512-6faShkdFugNQCLwucjPcY5ARoW1SlbnrZjmGl0IrrqewpvxvhSLHimCVzqeuULCbG0fQv7Dtk1yDbG3xv7Veog==", + "engines": { + "node": ">=12" + } + }, + "node_modules/postgres-bytea": { + "version": "3.0.0", + "resolved": "https://registry.npmjs.org/postgres-bytea/-/postgres-bytea-3.0.0.tgz", + "integrity": "sha512-CNd4jim9RFPkObHSjVHlVrxoVQXz7quwNFpz7RY1okNNme49+sVyiTvTRobiLV548Hx/hb1BG+iE7h9493WzFw==", + "dependencies": { + "obuf": "~1.1.2" + }, + "engines": { + "node": ">= 6" + } + }, + "node_modules/postgres-date": { + "version": "2.1.0", + "resolved": "https://registry.npmjs.org/postgres-date/-/postgres-date-2.1.0.tgz", + "integrity": "sha512-K7Juri8gtgXVcDfZttFKVmhglp7epKb1K4pgrkLxehjqkrgPhfG6OO8LHLkfaqkbpjNRnra018XwAr1yQFWGcA==", + "engines": { + "node": ">=12" + } + }, + "node_modules/postgres-interval": { + "version": "3.0.0", + "resolved": "https://registry.npmjs.org/postgres-interval/-/postgres-interval-3.0.0.tgz", + "integrity": "sha512-BSNDnbyZCXSxgA+1f5UU2GmwhoI0aU5yMxRGO8CdFEcY2BQF9xm/7MqKnYoM1nJDk8nONNWDk9WeSmePFhQdlw==", + "engines": { + "node": ">=12" + } + }, + "node_modules/postgres-range": { + "version": "1.1.4", + "resolved": "https://registry.npmjs.org/postgres-range/-/postgres-range-1.1.4.tgz", + "integrity": "sha512-i/hbxIE9803Alj/6ytL7UHQxRvZkI9O4Sy+J3HGc4F4oo/2eQAjTSNJ0bfxyse3bH0nuVesCk+3IRLaMtG3H6w==" + }, "node_modules/preact": { "version": "10.24.0", "resolved": "https://registry.npmjs.org/preact/-/preact-10.24.0.tgz", diff --git a/package.json b/package.json index 9716b88..587ecd3 100644 --- a/package.json +++ b/package.json @@ -18,6 +18,7 @@ "@clerk/clerk-expo": "^2.2.14", "@expo/metro-runtime": "~3.2.3", "@expo/vector-icons": "^14.0.2", + "@neondatabase/serverless": "^0.9.5", "@react-navigation/native": "^6.0.2", "expo": "~51.0.28", "expo-constants": "~16.0.2",