route handler for account/main

This commit is contained in:
2025-05-20 13:04:50 +03:00
parent a132c8a314
commit 73e206be5d
7 changed files with 408 additions and 16 deletions

View File

@@ -1,5 +1,49 @@
import { NextRequest } from 'next/server'
import { getServerSession } from 'next-auth'
import { authOptions } from '@/app/api/auth/[...nextauth]/route'
export async function PUT(req: NextRequest) {
// обновляем данные в аккаунте
export async function PATCH(req: NextRequest) {
try {
const session = await getServerSession(authOptions)
if (!session) {
return new Response(JSON.stringify({ error: 'Unauthorized' }), {
status: 401,
})
}
const data = await req.json()
const { firstName, lastName, email, phone_number, country, city } = data
const response = await fetch(
`${process.env.NEXT_PUBLIC_API_URL}/account/change_main_data/`,
{
method: 'PATCH',
headers: {
'Content-Type': 'application/json',
Authorization: `Bearer ${session.accessToken}`,
},
body: JSON.stringify({
firstName,
lastName,
email,
phone_number,
country,
city,
}),
}
)
if (!response.ok) {
const error = await response.json()
return new Response(JSON.stringify(error), { status: response.status })
}
const result = await response.json()
return new Response(JSON.stringify(result), { status: 200 })
} catch (error) {
return new Response(JSON.stringify({ error: 'Internal Server Error' }), {
status: 500,
})
}
}