44 lines
955 B
TypeScript
44 lines
955 B
TypeScript
import prisma from "../../prisma"
|
|
import type { Player, Token, TokenType } from "@prisma/client"
|
|
import jwt from "jsonwebtoken"
|
|
|
|
export interface RawToken {
|
|
value: string
|
|
type: TokenType
|
|
}
|
|
export interface IdToken {
|
|
id: string
|
|
type: TokenType
|
|
}
|
|
|
|
export const tokenLifetime = {
|
|
REFRESH: 172800,
|
|
ACCESS: 15,
|
|
}
|
|
|
|
export default async function createTokenDB(
|
|
player: Player,
|
|
newTokenType: TokenType
|
|
) {
|
|
// Create token entry in DB
|
|
const newTokenDB = await prisma.token.create({
|
|
data: {
|
|
type: newTokenType,
|
|
// expires: new Date(Date.now() + tokenLifetime[newTokenType] + "000"),
|
|
owner: {
|
|
connect: {
|
|
id: player.id,
|
|
},
|
|
},
|
|
},
|
|
})
|
|
|
|
// Sign a new access token
|
|
const newToken = jwt.sign(
|
|
{ id: newTokenDB.id },
|
|
process.env.TOKEN_SECRET as string,
|
|
{ expiresIn: tokenLifetime[newTokenType] }
|
|
)
|
|
|
|
return { newToken: { value: newToken, type: newTokenType }, newTokenDB }
|
|
}
|