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,9 +1,13 @@
'use server'
import db from '@/lib/db'
import journal from '@/actions/logger'
export const getPasswordResetTokenByToken = async (token: string) => {
try {
return await db.passwordResetToken.findUnique({ where: { token } })
} catch {
} catch (err) {
journal.error({ getPasswordResetTokenByToken: err, token })
return null
}
}
@@ -11,7 +15,8 @@ export const getPasswordResetTokenByToken = async (token: string) => {
export const getPasswordResetTokenByEmail = async (email: string) => {
try {
return await db.passwordResetToken.findFirst({ where: { email } })
} catch {
} catch (err) {
journal.error({ getPasswordResetTokenByEmail: err, email })
return null
}
}

View File

@@ -0,0 +1,33 @@
'use server'
import db from '@/lib/db'
import journal from '@/actions/logger'
export const createTwoFactoComfirmation = async (userId: string) => {
try {
return await db.twoFactorComfirmation.create({ data: { userId } })
} catch (err) {
journal.error({ createTwoFactoComfirmation: err, userId })
return null
}
}
export const getTwoFactorConfirmationByUserId = async (userId: string) => {
try {
return await db.twoFactorComfirmation.findUnique({
where: { userId },
})
} catch (err) {
journal.error({ getTwoFactorConfirmationByUserId: err, userId })
return null
}
}
export const deleteTwoFactoComfirmation = async (id: string) => {
try {
return await db.twoFactorComfirmation.delete({ where: { id } })
} catch (err) {
journal.error({ deleteTwoFactoComfirmation: err, id })
return null
}
}

33
data/two-factor-token.ts Normal file
View File

@@ -0,0 +1,33 @@
'use server'
import db from '@/lib/db'
import journal from '@/actions/logger'
export const getTwoFactorTokenByToken = async (token: string) => {
try {
return await db.twoFactorToken.findUnique({
where: { token },
})
} catch (err) {
journal.error({ getTwoFactorTokenByToken: err, token })
return null
}
}
export const getTwoFactorTokenByEmail = async (email: string) => {
try {
return await db.twoFactorToken.findFirst({ where: { email } })
} catch (err) {
journal.error({ getTwoFactorTokenByEmail: err, email })
return null
}
}
export const deleteTwoFactorToken = async (id: string) => {
try {
return await db.twoFactorToken.delete({ where: { id } })
} catch (err) {
journal.error({ deleteTwoFactorToken: err, id })
return null
}
}

View File

@@ -1,10 +1,14 @@
'use server'
import { User } from '@prisma/client'
import db from '@/lib/db'
import journal from '@/actions/logger'
export const getUserByEmail = async (email: string): Promise<User | null> => {
try {
return await db.user.findUnique({ where: { email } })
} catch {
} catch (err) {
journal.error({ getUserByEmail: err, email })
return null
}
}
@@ -12,7 +16,22 @@ export const getUserByEmail = async (email: string): Promise<User | null> => {
export const getUserById = async (id: string): Promise<User | null> => {
try {
return await db.user.findUnique({ where: { id } })
} catch {
} catch (err) {
journal.error({ getUserById: err, id })
return null
}
}
export const updateUserEmailVerified = async (id: string, email: string) => {
try {
await db.user.update({
where: { id },
data: {
email, emailVerified: new Date(),
},
})
} catch (err) {
journal.error({ updateUserEmailVerified: err, id, email })
return { error: 'db.error.update.user_data' }
}
}

View File

@@ -1,9 +1,13 @@
'use server'
import db from '@/lib/db'
import journal from '@/actions/logger'
export const getVerificationTokenByToken = async (token: string) => {
try {
return await db.verificationToken.findUnique({ where: { token } })
} catch {
} catch (err) {
journal.error({ getVerificationTokenByToken: err, token })
return null
}
}
@@ -11,7 +15,18 @@ export const getVerificationTokenByToken = async (token: string) => {
export const getVerificationTokenByEmail = async (email: string) => {
try {
return await db.verificationToken.findFirst({ where: { email } })
} catch {
} catch (err) {
journal.error({ getVerificationTokenByEmail: err, email })
return null
}
}
export const deleteVerificationToken = async (id: string) => {
try {
await db.verificationToken.delete({
where: { id },
})
} catch (err) {
journal.error({ deleteVerificationToken: err, id })
}
}