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

28 lines
526 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-10 02:11:36 -05:00
token: token
2022-11-09 21:38:05 -05:00
}
})
2022-11-10 02:11:36 -05:00
// TODO: set expiredReason?
2022-11-09 21:38:05 -05:00
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
}