route handler + backend api

This commit is contained in:
2025-05-22 11:45:25 +03:00
parent 5ad12c34cd
commit 5ba9121f33
7 changed files with 220 additions and 36 deletions

View File

@@ -0,0 +1,63 @@
import { NextRequest } from 'next/server'
import { getServerSession } from 'next-auth'
import { authOptions } from '@/app/api/auth/[...nextauth]/route'
export async function POST(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 {
transport,
country_from,
city_from,
country_to,
city_to,
cargo_type,
departure,
arrival,
phone_number,
comment,
email_notification,
} = data
const response = await fetch(`${process.env.NEXT_PUBLIC_API_URL}/account/create_sender/`, {
method: 'POST',
headers: {
'Content-Type': 'application/json',
Authorization: `Bearer ${session.accessToken}`,
},
body: JSON.stringify({
transport,
country_from,
city_from,
country_to,
city_to,
cargo_type,
departure,
arrival,
phone_number,
comment,
email_notification,
}),
})
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,
})
}
}