Complete backend rework
This commit is contained in:
parent
53d4ab527a
commit
eb09017dab
25 changed files with 958 additions and 866 deletions
|
@ -1,13 +1,13 @@
|
|||
import { NextApiRequest, NextApiResponse } from "next"
|
||||
import { Logging } from "../../lib/backend/logging"
|
||||
import { Token } from "@prisma/client"
|
||||
import sendError from "../../lib/backend/components/sendError"
|
||||
import getTokenFromCookie from "../../lib/backend/components/getTokenFromCookie"
|
||||
import checkTokenIsValid from "../../lib/backend/components/checkTokenIsValid"
|
||||
import getTokenDB from "../../lib/backend/components/getTokenDB"
|
||||
import getPlayerByIdDB from "../../lib/backend/components/getPlayerByIdDB"
|
||||
import createTokenDB from "../../lib/backend/components/createTokenDB"
|
||||
import sendResponse from "../../lib/backend/components/sendResponse"
|
||||
import sendError from "../../lib/backend/components/sendError"
|
||||
import { Logging } from "../../lib/backend/logging"
|
||||
import { Token } from "@prisma/client"
|
||||
|
||||
interface Data {
|
||||
token: string
|
||||
|
@ -17,41 +17,26 @@ export default async function auth(
|
|||
req: NextApiRequest,
|
||||
res: NextApiResponse<Data>
|
||||
) {
|
||||
return getTokenFromCookie({
|
||||
req,
|
||||
res,
|
||||
newTokenType: "ACCESS" as Token["type"],
|
||||
})
|
||||
.then(checkTokenIsValid)
|
||||
.then(getTokenDB)
|
||||
.then(getPlayerByIdDB)
|
||||
.then(createTokenDB)
|
||||
.then(authResponse<Data>)
|
||||
.then(sendResponse<Data>)
|
||||
.catch((err) => sendError(req, res, err))
|
||||
getTokenFromCookie(req, res, (token) => {
|
||||
checkTokenIsValid(req, res, token, (tokenBody) => {
|
||||
getTokenDB(req, res, tokenBody, token[1], (tokenDB) => {
|
||||
getPlayerByIdDB(req, res, tokenDB, (player) => {
|
||||
createTokenDB(player, "ACCESS", (newToken) => {
|
||||
sendResponse(req, res, authResponse(newToken, tokenDB))
|
||||
})
|
||||
})
|
||||
})
|
||||
})
|
||||
}).catch((err) => sendError(req, res, err))
|
||||
}
|
||||
|
||||
async function authResponse<T>(payload: {
|
||||
newToken: string
|
||||
newTokenDB: Token
|
||||
tokenDB: Token
|
||||
req: NextApiRequest
|
||||
res: NextApiResponse<T>
|
||||
}) {
|
||||
const { newToken, newTokenDB, tokenDB, req, res } = payload
|
||||
function authResponse(newToken: [string, Token], tokenDB: Token) {
|
||||
const [newTokenValue, newTokenDB] = newToken
|
||||
|
||||
// Successfull response
|
||||
return {
|
||||
req,
|
||||
res,
|
||||
result: {
|
||||
message:
|
||||
"Access-Token generated: " +
|
||||
newTokenDB.id +
|
||||
" with Refreshtoken-Token: " +
|
||||
tokenDB.id,
|
||||
body: { token: newToken },
|
||||
type: ["debug", "info.cyan"] as Logging[],
|
||||
},
|
||||
message: `Access-Token generated: ${newTokenDB.id} with Refreshtoken-Token: ${tokenDB.id}`,
|
||||
body: { token: newTokenValue },
|
||||
type: ["debug", "info.cyan"] as Logging[],
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue