-
Notifications
You must be signed in to change notification settings - Fork 0
Initial commit #4
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,30 @@ | ||
| import { db } from "@/config/db"; | ||
| import { usersTable } from "@/config/schema"; | ||
| import { currentUser } from "@clerk/nextjs/server"; | ||
| import { eq } from "drizzle-orm"; | ||
| import { point } from "drizzle-orm/pg-core"; | ||
| import { NextRequest, NextResponse } from "next/server"; | ||
|
|
||
| export async function POST(req:NextRequest) { | ||
|
|
||
| const user = await currentUser(); | ||
|
|
||
| const users = await db.select().from(usersTable) | ||
| //@ts-ignore | ||
| .where(eq(usersTable.email,user?.primaryEmailAddress?.emailAddress)) | ||
|
|
||
| if(users?.length <=0){ | ||
| const newUser = { | ||
| name: user?.fullName ?? '', | ||
| email : user?.primaryEmailAddress?.emailAddress ?? '' , | ||
| points: 0 | ||
| } | ||
| const result = await db.insert(usersTable) | ||
| .values(newUser).returning() | ||
|
|
||
| return NextResponse.json(result[0]) | ||
|
|
||
| } | ||
|
|
||
| return NextResponse.json(users[0]) | ||
| } | ||
|
Comment on lines
+8
to
+30
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Add error handling for database operations. The database operations (select, insert) can fail due to network issues, constraint violations, or other database errors, but there's no try-catch block to handle these failures gracefully. Wrap the handler logic in a try-catch block: export async function POST(req:NextRequest) {
+ try {
+ const user = await currentUser();
+
+ if (!user || !user.primaryEmailAddress?.emailAddress) {
+ return NextResponse.json(
+ { error: 'Unauthorized' },
+ { status: 401 }
+ );
+ }
+
+ const userEmail = user.primaryEmailAddress.emailAddress;
- const user = await currentUser();
-
- const users = await db.select().from(usersTable)
- //@ts-ignore
- .where(eq(usersTable.email,user?.primaryEmailAddress?.emailAddress))
+ const users = await db.select().from(usersTable)
+ .where(eq(usersTable.email, userEmail))
- if(users?.length <=0){
- const newUser = {
- name: user?.fullName ?? '',
- email : user?.primaryEmailAddress?.emailAddress ?? '' ,
- points: 0
- }
- const result = await db.insert(usersTable)
- .values(newUser).returning()
+ if(users?.length === 0){
+ const newUser = {
+ name: user.fullName ?? '',
+ email: userEmail,
+ points: 0
+ }
+ const result = await db.insert(usersTable)
+ .values(newUser).returning()
- return NextResponse.json(result[0])
+ return NextResponse.json(result[0])
+ }
- }
-
- return NextResponse.json(users[0])
+ return NextResponse.json(users[0])
+ } catch (error) {
+ console.error('Error in user route:', error);
+ return NextResponse.json(
+ { error: 'Internal server error' },
+ { status: 500 }
+ );
+ }
}🤖 Prompt for AI Agents |
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,7 @@ | ||
| import { createContext } from "react"; | ||
|
|
||
| export const UserDetailContext = createContext<any>({ | ||
| userDetails : undefined, | ||
| setUserDetail : () => { } | ||
|
|
||
| }) |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,12 +1,32 @@ | ||
| import React from 'react'; | ||
| import React, { useEffect, useState } from 'react'; | ||
| import { ThemeProvider as NextThemesProvider } from "next-themes" | ||
|
|
||
| import { useUser } from '@clerk/nextjs'; | ||
| import axios from 'axios' | ||
| import { UserDetailContext } from './context/UserDetailsContext'; | ||
| import { set } from 'date-fns'; | ||
| function Provider( { | ||
| children, | ||
| ...props | ||
| }: React.ComponentProps<typeof NextThemesProvider>) { | ||
|
|
||
| const {user} = useUser(); | ||
| const[userDetails,setUserDetail]= useState(); | ||
|
|
||
| useEffect(()=>{ | ||
| user && CreateNewUser() | ||
| },[user]) | ||
|
|
||
| const CreateNewUser = async() =>{ | ||
| const result = await axios.post('/api/user',{}) | ||
| console.log(result) | ||
| setUserDetail(result?.data) | ||
| } | ||
|
|
||
|
|
||
|
|
||
| return ( | ||
| <NextThemesProvider {...props}> | ||
| <UserDetailContext.Provider value={{userDetails,setUserDetail}}/> | ||
| {children} | ||
| </NextThemesProvider> | ||
|
Comment on lines
+29
to
31
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🧩 Analysis chainContext Provider is self-closing, so children don’t receive context. Right now the Provider is rendered as a self-closing tag, and <NextThemesProvider {...props}>
- <UserDetailContext.Provider value={{userDetails,setUserDetail}}/>
- {children}
+ <UserDetailContext.Provider value={{ userDetails, setUserDetail }}>
+ {children}
+ </UserDetailContext.Provider>
</NextThemesProvider>Please wrap the children as above and verify any consumers of 🏁 Script executed: #!/bin/bash
# Find and display the provider file
find . -type f -name "provider.tsx" | head -5Length of output: 18 🏁 Script executed: #!/bin/bash
# Display the full app/provider.tsx file
cat app/provider.tsxLength of output: 895 🏁 Script executed: #!/bin/bash
# Search for UserDetailContext usage and imports
rg -n "UserDetailContext" --type=tsx --type=ts -C3Length of output: 27 🏁 Script executed: #!/bin/bash
# Search for UserDetailContext usage without type flags
rg -n "UserDetailContext" -C3Length of output: 1089 🏁 Script executed: #!/bin/bash
# Search for useContext calls that might reference UserDetailContext
rg -n "useContext" -C2Length of output: 1860 🏁 Script executed: #!/bin/bash
# Find all files that import from context/UserDetailsContext
rg -n "from.*context/UserDetailsContext" -C2Length of output: 280 Context Provider is self-closing, so children don't receive context. Right now the Provider is rendered as a self-closing tag on line 29, and <NextThemesProvider {...props}>
- <UserDetailContext.Provider value={{userDetails,setUserDetail}}/>
- {children}
+ <UserDetailContext.Provider value={{ userDetails, setUserDetail }}>
+ {children}
+ </UserDetailContext.Provider>
</NextThemesProvider>Wrap children inside the Provider to fix the context propagation. While no current consumers of 🤖 Prompt for AI Agents |
||
| ) | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,3 @@ | ||
| import { drizzle } from 'drizzle-orm/neon-http'; | ||
|
|
||
| export const db = drizzle(process.env.DATABASE_URL!); |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,9 @@ | ||
| import { integer, pgTable, varchar } from "drizzle-orm/pg-core"; | ||
|
|
||
| export const usersTable = pgTable("users", { | ||
| id: integer().primaryKey().generatedAlwaysAsIdentity(), | ||
| name: varchar({ length: 255 }).notNull(), | ||
| email: varchar({ length: 255 }).notNull().unique(), | ||
| points: integer().default(0), | ||
| subscription: varchar() | ||
| }); |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,10 @@ | ||
| import 'dotenv/config'; | ||
| import { defineConfig } from 'drizzle-kit'; | ||
|
|
||
| export default defineConfig({ | ||
| schema: './config/schema.tsx', | ||
| dialect: 'postgresql', | ||
| dbCredentials: { | ||
| url: process.env.DATABASE_URL!, | ||
| }, | ||
| }); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Add authentication check and fix type safety issues.
Several critical issues:
Missing authentication check: If the user is not authenticated,
currentUser()returnsnull, but the code proceeds without checking, potentially causing runtime errors or querying withundefinedemail.Type suppression with
@ts-ignore: This indicates a type mismatch that should be properly resolved rather than suppressed.Unsafe email access:
user?.primaryEmailAddress?.emailAddresscould beundefined, making the query condition unreliable.Apply this diff to add proper authentication and type handling:
export async function POST(req:NextRequest) { const user = await currentUser(); + + if (!user || !user.primaryEmailAddress?.emailAddress) { + return NextResponse.json( + { error: 'Unauthorized' }, + { status: 401 } + ); + } + + const userEmail = user.primaryEmailAddress.emailAddress; const users = await db.select().from(usersTable) - //@ts-ignore - .where(eq(usersTable.email,user?.primaryEmailAddress?.emailAddress)) + .where(eq(usersTable.email, userEmail))📝 Committable suggestion
🤖 Prompt for AI Agents