Skip to content

Commit

Permalink
Database is conected , Table is created for users
Browse files Browse the repository at this point in the history
  • Loading branch information
Satora1 committed Sep 27, 2024
1 parent af86ac1 commit 39ff369
Show file tree
Hide file tree
Showing 6 changed files with 188 additions and 2 deletions.
4 changes: 3 additions & 1 deletion .env.local
Original file line number Diff line number Diff line change
@@ -1 +1,3 @@
EXPO_PUBLIC_CLERK_PUBLISHABLE_KEY=pk_test_cHJvbXB0LW1hZ2dvdC02MC5jbGVyay5hY2NvdW50cy5kZXYk
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/
34 changes: 34 additions & 0 deletions app/(api)/user+api.ts
Original file line number Diff line number Diff line change
@@ -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 });
}
}
9 changes: 8 additions & 1 deletion app/(auth)/sign-up.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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()

Expand Down Expand Up @@ -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 {
Expand Down
40 changes: 40 additions & 0 deletions lib/fetch.ts
Original file line number Diff line number Diff line change
@@ -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 = <T>(url: string, options?: RequestInit) => {
const [data, setData] = useState<T | null>(null);
const [loading, setLoading] = useState(false);
const [error, setError] = useState<string | null>(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};
};
102 changes: 102 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down

0 comments on commit 39ff369

Please sign in to comment.