fix(api): fix token handler
This commit is contained in:
@@ -46,32 +46,45 @@ export async function POST(req: any) {
|
||||
}
|
||||
)
|
||||
}
|
||||
|
||||
const userData = await genAccessToken(req)
|
||||
|
||||
const authorization = `bearer ${userData?.access_token}`
|
||||
let fetchOptions: any = {}
|
||||
|
||||
const fetchOptions: any = {
|
||||
method,
|
||||
headers: {
|
||||
"content-type": "application/json",
|
||||
authorization: authorization,
|
||||
},
|
||||
}
|
||||
|
||||
if (method === "POST" || method === "PUT") {
|
||||
let refreshBody = JSON.stringify({
|
||||
...JSON.parse(data),
|
||||
token: userData?.refresh_token || "",
|
||||
})
|
||||
fetchOptions.body = isRefresh ? refreshBody : data
|
||||
if (isRefresh) {
|
||||
if (!userData?.refresh_token) {
|
||||
return NextResponse.json(
|
||||
{ message: "Refresh token is empty", code: 406 },
|
||||
{ status: 200 }
|
||||
)
|
||||
}
|
||||
fetchOptions = {
|
||||
method,
|
||||
headers: {
|
||||
"content-type": "application/json",
|
||||
},
|
||||
body: JSON.stringify({
|
||||
token: userData?.refresh_token,
|
||||
}),
|
||||
}
|
||||
} else {
|
||||
if (isParamsValid(data)) {
|
||||
let test = ""
|
||||
for (const [key, value] of Object.entries(data)) {
|
||||
test += `&${key}=${value}`
|
||||
const authorization = `bearer ${userData?.access_token}`
|
||||
fetchOptions = {
|
||||
method,
|
||||
headers: {
|
||||
"content-type": "application/json",
|
||||
authorization: authorization,
|
||||
},
|
||||
}
|
||||
if (method === "POST" || method === "PUT") {
|
||||
fetchOptions.body = data
|
||||
} else {
|
||||
if (isParamsValid(data)) {
|
||||
let test = ""
|
||||
for (const [key, value] of Object.entries(data)) {
|
||||
test += `&${key}=${value}`
|
||||
}
|
||||
url += `?${test}`
|
||||
}
|
||||
url += `?${test}`
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user