CoastalCommitsPastes/client/lib/server/generate-access-token.ts
2022-11-09 23:11:36 -08:00

27 lines
526 B
TypeScript

import config from "@lib/config"
import { User } from "@prisma/client"
import prisma from "app/prisma"
import { sign } from "jsonwebtoken"
export async function generateAndExpireAccessToken(userId: User["id"]) {
const token = sign({ id: userId }, config.jwt_secret, { expiresIn: "2d" })
await prisma.authTokens.create({
data: {
userId: userId,
token: token
}
})
// TODO: set expiredReason?
prisma.authTokens.deleteMany({
where: {
userId: userId,
token: {
not: token
}
}
})
return token
}