Fix req.body with JSON.parse()

This commit is contained in:
aronmal 2023-02-13 23:21:38 +01:00
parent 15296f9163
commit da9337a50b
Signed by: aronmal
GPG key ID: 816B7707426FC612
3 changed files with 6 additions and 5 deletions

View file

@ -2,7 +2,7 @@ import { NextApiRequest } from "next"
async function getTokenFromBody<T>(payload: T & { req: NextApiRequest }) { async function getTokenFromBody<T>(payload: T & { req: NextApiRequest }) {
const { req } = payload const { req } = payload
const token: string = req.body.token const token: string = JSON.parse(req.body).token
// Checking for cookie presens, because it is necessary // Checking for cookie presens, because it is necessary
if (!token) { if (!token) {

View file

@ -17,14 +17,15 @@ export default async function login(
req: NextApiRequest, req: NextApiRequest,
res: NextApiResponse<any> res: NextApiResponse<any>
) { ) {
const { username, password } = req.body const { username, password } = JSON.parse(req.body)
return preCheck({ const payload = {
req, req,
res, res,
username, username,
password, password,
newTokenType: "REFRESH" as Token["type"], newTokenType: "REFRESH" as Token["type"],
}) }
return preCheck(payload)
.then(getPlayerByNameDB) .then(getPlayerByNameDB)
.then(checkPasswordIsValid) .then(checkPasswordIsValid)
.then(createTokenDB) .then(createTokenDB)

View file

@ -13,7 +13,7 @@ export default async function register(
req: NextApiRequest, req: NextApiRequest,
res: NextApiResponse<any> res: NextApiResponse<any>
) { ) {
const { username, password } = req.body const { username, password } = JSON.parse(req.body)
return createPlayerDB({ req, res, username, password }) return createPlayerDB({ req, res, username, password })
.then(registerResponse<Data>) .then(registerResponse<Data>)
.then(sendResponse<Data>) .then(sendResponse<Data>)