CoastalCommitsPastes/client/lib/api/generate-access-token.ts

28 lines
529 B
TypeScript
Raw Normal View History

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