mirror of
https://github.com/Tony0410/nextstep.git
synced 2026-05-25 05:41:39 +08:00
Initial commit: Next Step health management app
A calm, reliable app to help manage appointments, medications, and notes for chemo patients and their families. Features: - Today dashboard with next appointment and medications due - Medication tracking with multiple schedule types (fixed times, interval, weekdays, PRN) - One-tap dose logging with 5-minute undo window - Questions for doctor tracking - Family sharing with workspace model and invite links - Offline-first with IndexedDB and sync - Docker Compose deployment with Tailscale Funnel support Tech stack: - Next.js 14 (App Router) + TypeScript + Tailwind CSS - PostgreSQL + Prisma - Argon2 password hashing + session cookies - Dexie.js for IndexedDB Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
This commit is contained in:
71
src/app/api/auth/register/route.ts
Normal file
71
src/app/api/auth/register/route.ts
Normal file
@@ -0,0 +1,71 @@
|
||||
import { NextRequest, NextResponse } from 'next/server'
|
||||
import { prisma } from '@/lib/db/prisma'
|
||||
import { hashPassword, createSession, getSessionCookieConfig } from '@/lib/auth'
|
||||
import { registerSchema } from '@/lib/validation'
|
||||
import { withRateLimit } from '@/lib/auth/middleware'
|
||||
|
||||
async function handler(req: NextRequest) {
|
||||
try {
|
||||
const body = await req.json()
|
||||
const result = registerSchema.safeParse(body)
|
||||
|
||||
if (!result.success) {
|
||||
return NextResponse.json(
|
||||
{ error: 'Invalid input', details: result.error.flatten() },
|
||||
{ status: 400 }
|
||||
)
|
||||
}
|
||||
|
||||
const { email, password, name } = result.data
|
||||
|
||||
// Check if user exists
|
||||
const existing = await prisma.user.findUnique({
|
||||
where: { email: email.toLowerCase() },
|
||||
})
|
||||
|
||||
if (existing) {
|
||||
return NextResponse.json(
|
||||
{ error: 'An account with this email already exists' },
|
||||
{ status: 409 }
|
||||
)
|
||||
}
|
||||
|
||||
// Create user
|
||||
const passwordHash = await hashPassword(password)
|
||||
const user = await prisma.user.create({
|
||||
data: {
|
||||
email: email.toLowerCase(),
|
||||
passwordHash,
|
||||
name,
|
||||
},
|
||||
select: {
|
||||
id: true,
|
||||
email: true,
|
||||
name: true,
|
||||
},
|
||||
})
|
||||
|
||||
// Create session
|
||||
const userAgent = req.headers.get('user-agent') || undefined
|
||||
const ipAddress = req.headers.get('x-forwarded-for')?.split(',')[0]
|
||||
const token = await createSession(user.id, userAgent, ipAddress)
|
||||
const cookieConfig = getSessionCookieConfig(token)
|
||||
|
||||
const response = NextResponse.json({
|
||||
user,
|
||||
message: 'Account created successfully',
|
||||
})
|
||||
|
||||
response.cookies.set(cookieConfig)
|
||||
|
||||
return response
|
||||
} catch (error) {
|
||||
console.error('Registration error:', error)
|
||||
return NextResponse.json(
|
||||
{ error: 'Registration failed' },
|
||||
{ status: 500 }
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
export const POST = withRateLimit(handler)
|
||||
Reference in New Issue
Block a user