add user settings
This commit is contained in:
39
src/app/api/settings/route.ts
Normal file
39
src/app/api/settings/route.ts
Normal file
@@ -0,0 +1,39 @@
|
||||
import { NextRequest, NextResponse } from 'next/server'
|
||||
import { requireAuth } from '@/lib/auth'
|
||||
import { getUserSettings, updateUserSettings } from '@/lib/settings'
|
||||
import type { UserSettings } from '@/types'
|
||||
|
||||
export async function GET(req: NextRequest) {
|
||||
const auth = await requireAuth(req)
|
||||
if (auth instanceof NextResponse) return auth
|
||||
|
||||
const settings = getUserSettings(auth.session.userId)
|
||||
return NextResponse.json(settings)
|
||||
}
|
||||
|
||||
export async function PUT(req: NextRequest) {
|
||||
const auth = await requireAuth(req)
|
||||
if (auth instanceof NextResponse) return auth
|
||||
|
||||
let body: unknown
|
||||
try {
|
||||
body = await req.json()
|
||||
} catch {
|
||||
return NextResponse.json({ error: 'Invalid JSON' }, { status: 400 })
|
||||
}
|
||||
|
||||
const s = body as Record<string, unknown>
|
||||
const boolFields: (keyof UserSettings)[] = [
|
||||
'mixedAutoplay', 'mixedLoop', 'mixedMuted',
|
||||
'moviesAutoplay', 'moviesLoop', 'moviesMuted',
|
||||
'tvAutoplay', 'tvLoop', 'tvMuted',
|
||||
]
|
||||
for (const field of boolFields) {
|
||||
if (typeof s[field] !== 'boolean') {
|
||||
return NextResponse.json({ error: `Invalid value for ${field}` }, { status: 400 })
|
||||
}
|
||||
}
|
||||
|
||||
updateUserSettings(auth.session.userId, s as unknown as UserSettings)
|
||||
return NextResponse.json({ ok: true })
|
||||
}
|
||||
Reference in New Issue
Block a user