@@ -2,7 +2,7 @@ import { NextRequest, NextResponse } from "next/server";
22import { auth } from "@/lib/auth" ;
33import { headers } from "next/headers" ;
44import { db } from "@/lib/db" ;
5- import { userProfile , SoftwareBackground } from "@/lib/db/schema" ;
5+ import { userProfile , user , SoftwareBackground , HardwareTier } from "@/lib/db/schema" ;
66import { eq } from "drizzle-orm" ;
77import { randomUUID } from "crypto" ;
88
@@ -35,6 +35,7 @@ export async function GET() {
3535 profile : profile
3636 ? {
3737 softwareBackground : profile . softwareBackground ,
38+ hardwareTier : profile . hardwareTier ,
3839 createdAt : profile . createdAt ,
3940 updatedAt : profile . updatedAt ,
4041 }
@@ -52,19 +53,52 @@ export async function GET() {
5253// POST /api/profile - Create user profile (called after signup)
5354export async function POST ( request : NextRequest ) {
5455 try {
55- const session = await auth . api . getSession ( {
56- headers : await headers ( ) ,
57- } ) ;
58-
59- if ( ! session ) {
60- return NextResponse . json (
61- { error : "Unauthorized" } ,
62- { status : 401 }
63- ) ;
64- }
65-
6656 const body = await request . json ( ) ;
67- const { softwareBackground } = body ;
57+ const { softwareBackground, hardwareTier, userId } = body ;
58+
59+ let targetUserId : string ;
60+
61+ // Allow creating profile during signup with userId (user not verified yet)
62+ // OR with authenticated session (user is signed in)
63+ if ( userId ) {
64+ // Validate user exists and was created recently (within 5 minutes)
65+ // This prevents abuse while allowing signup-time profile creation
66+ const userRecord = await db . query . user . findFirst ( {
67+ where : eq ( user . id , userId ) ,
68+ } ) ;
69+
70+ if ( ! userRecord ) {
71+ return NextResponse . json (
72+ { error : "User not found" } ,
73+ { status : 404 }
74+ ) ;
75+ }
76+
77+ // Check if user was created recently (within 5 minutes)
78+ const fiveMinutesAgo = new Date ( Date . now ( ) - 5 * 60 * 1000 ) ;
79+ if ( userRecord . createdAt < fiveMinutesAgo ) {
80+ return NextResponse . json (
81+ { error : "Profile creation window expired. Please sign in and update your profile." } ,
82+ { status : 403 }
83+ ) ;
84+ }
85+
86+ targetUserId = userId ;
87+ } else {
88+ // Fallback to session-based authentication
89+ const session = await auth . api . getSession ( {
90+ headers : await headers ( ) ,
91+ } ) ;
92+
93+ if ( ! session ) {
94+ return NextResponse . json (
95+ { error : "Unauthorized. Provide userId for signup or sign in first." } ,
96+ { status : 401 }
97+ ) ;
98+ }
99+
100+ targetUserId = session . user . id ;
101+ }
68102
69103 // Validate software background
70104 const validBackgrounds : SoftwareBackground [ ] = [ "beginner" , "intermediate" , "advanced" ] ;
@@ -75,9 +109,18 @@ export async function POST(request: NextRequest) {
75109 ) ;
76110 }
77111
112+ // Validate hardware tier
113+ const validTiers : HardwareTier [ ] = [ "tier1" , "tier2" , "tier3" , "tier4" ] ;
114+ if ( ! hardwareTier || ! validTiers . includes ( hardwareTier ) ) {
115+ return NextResponse . json (
116+ { error : "Invalid hardware tier. Must be: tier1, tier2, tier3, or tier4" } ,
117+ { status : 400 }
118+ ) ;
119+ }
120+
78121 // Check if profile already exists
79122 const existingProfile = await db . query . userProfile . findFirst ( {
80- where : eq ( userProfile . userId , session . user . id ) ,
123+ where : eq ( userProfile . userId , targetUserId ) ,
81124 } ) ;
82125
83126 if ( existingProfile ) {
@@ -92,14 +135,16 @@ export async function POST(request: NextRequest) {
92135 . insert ( userProfile )
93136 . values ( {
94137 id : randomUUID ( ) ,
95- userId : session . user . id ,
138+ userId : targetUserId ,
96139 softwareBackground,
140+ hardwareTier,
97141 } )
98142 . returning ( ) ;
99143
100144 return NextResponse . json ( {
101145 profile : {
102146 softwareBackground : newProfile [ 0 ] . softwareBackground ,
147+ hardwareTier : newProfile [ 0 ] . hardwareTier ,
103148 createdAt : newProfile [ 0 ] . createdAt ,
104149 updatedAt : newProfile [ 0 ] . updatedAt ,
105150 } ,
@@ -128,24 +173,41 @@ export async function PUT(request: NextRequest) {
128173 }
129174
130175 const body = await request . json ( ) ;
131- const { softwareBackground } = body ;
176+ const { softwareBackground, hardwareTier } = body ;
177+
178+ // Validate software background (if provided)
179+ if ( softwareBackground ) {
180+ const validBackgrounds : SoftwareBackground [ ] = [ "beginner" , "intermediate" , "advanced" ] ;
181+ if ( ! validBackgrounds . includes ( softwareBackground ) ) {
182+ return NextResponse . json (
183+ { error : "Invalid software background. Must be: beginner, intermediate, or advanced" } ,
184+ { status : 400 }
185+ ) ;
186+ }
187+ }
132188
133- // Validate software background
134- const validBackgrounds : SoftwareBackground [ ] = [ "beginner" , "intermediate" , "advanced" ] ;
135- if ( ! validBackgrounds . includes ( softwareBackground ) ) {
136- return NextResponse . json (
137- { error : "Invalid software background. Must be: beginner, intermediate, or advanced" } ,
138- { status : 400 }
139- ) ;
189+ // Validate hardware tier (if provided)
190+ if ( hardwareTier ) {
191+ const validTiers : HardwareTier [ ] = [ "tier1" , "tier2" , "tier3" , "tier4" ] ;
192+ if ( ! validTiers . includes ( hardwareTier ) ) {
193+ return NextResponse . json (
194+ { error : "Invalid hardware tier. Must be: tier1, tier2, tier3, or tier4" } ,
195+ { status : 400 }
196+ ) ;
197+ }
140198 }
141199
200+ // Build update object with only provided fields
201+ const updateData : { softwareBackground ?: SoftwareBackground ; hardwareTier ?: HardwareTier ; updatedAt : Date } = {
202+ updatedAt : new Date ( ) ,
203+ } ;
204+ if ( softwareBackground ) updateData . softwareBackground = softwareBackground ;
205+ if ( hardwareTier ) updateData . hardwareTier = hardwareTier ;
206+
142207 // Update profile
143208 const updatedProfile = await db
144209 . update ( userProfile )
145- . set ( {
146- softwareBackground,
147- updatedAt : new Date ( ) ,
148- } )
210+ . set ( updateData )
149211 . where ( eq ( userProfile . userId , session . user . id ) )
150212 . returning ( ) ;
151213
@@ -159,6 +221,7 @@ export async function PUT(request: NextRequest) {
159221 return NextResponse . json ( {
160222 profile : {
161223 softwareBackground : updatedProfile [ 0 ] . softwareBackground ,
224+ hardwareTier : updatedProfile [ 0 ] . hardwareTier ,
162225 createdAt : updatedProfile [ 0 ] . createdAt ,
163226 updatedAt : updatedProfile [ 0 ] . updatedAt ,
164227 } ,
0 commit comments