yo-next-auth/lib/tokens.ts

32 lines
751 B
TypeScript

import { v4 as uuid } from 'uuid'
import {
VERIFICATION_TOKEN_EXPIRATION_DURATION,
} from '@/config/auth'
import { db } from '@/lib/db'
import { getVerificationTokenByEmail } from '@/data/verification-token'
export const generateVerificationToken = async (email: string) => {
const token = uuid()
const expires = new Date(
new Date().getTime() + VERIFICATION_TOKEN_EXPIRATION_DURATION)
const existingToken = await getVerificationTokenByEmail(email)
if (existingToken) {
await db.verificationToken.delete({
where: {
id: existingToken.id,
},
})
}
const verificationToken = await db.verificationToken.create({
data: {
email,
token,
expires,
},
})
return verificationToken
}