First prisma integration step

This commit is contained in:
aronmal 2023-02-04 00:29:06 +01:00
parent 6918729c45
commit 0507309ab1
Signed by: aronmal
GPG key ID: 816B7707426FC612
21 changed files with 645 additions and 1 deletions

View file

@ -0,0 +1,49 @@
import { NextApiRequest, NextApiResponse } from "next"
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
}
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))
}
async function authResponse<T>(payload: {
newToken: string,
newTokenDB: Token,
tokenDB: Token,
req: NextApiRequest,
res: NextApiResponse<T>
}) {
const { newToken, newTokenDB, tokenDB, req, res } = payload
// 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[]
}
}
}