added 2FA

This commit is contained in:
2024-04-26 22:16:21 +03:00
parent 53cadc289a
commit f17a002ac6
38 changed files with 1036 additions and 414 deletions

View File

@@ -1,10 +1,28 @@
import crypto from 'crypto'
import { v4 as uuidV4 } from 'uuid'
import {
VERIFICATION_TOKEN_EXPIRATION_DURATION,
} from '@/config/auth'
import { VERIFICATION_TOKEN_EXPIRATION_DURATION } from '@/config/auth'
import db from '@/lib/db'
import { getVerificationTokenByEmail } from '@/data/verification-token'
import { getPasswordResetTokenByEmail } from '@/data/password-reset-token'
import { deleteTwoFactorToken, getTwoFactorTokenByEmail } from '@/data/two-factor-token'
export const generateTwoFactorToken = async (email: string) => {
const token = crypto.randomInt(100_000, 1_000_000).toString()
const expires = new Date(new Date().getTime() + VERIFICATION_TOKEN_EXPIRATION_DURATION)
const existingToken = await getTwoFactorTokenByEmail(email)
if (existingToken) {
await deleteTwoFactorToken(existingToken.id)
}
try {
return await db.twoFactorToken.create({ data: { email, token, expires } })
} catch (err) {
console.log(err)
return null
}
}
export const generatePasswordResetToken = async (email: string) => {
const token = uuidV4()
@@ -21,9 +39,7 @@ export const generatePasswordResetToken = async (email: string) => {
const passwordResetToken = await db.passwordResetToken.create({
data: {
email,
token,
expires,
email, token, expires,
},
})
@@ -46,9 +62,7 @@ export const generateVerificationToken = async (email: string) => {
const verificationToken = await db.verificationToken.create({
data: {
email,
token,
expires,
email, token, expires,
},
})