initial commit

This commit is contained in:
Harivansh Rathi 2024-11-24 20:56:03 -05:00
commit ef9ccf22d3
133 changed files with 20802 additions and 0 deletions

9
lib/db.ts Normal file
View file

@ -0,0 +1,9 @@
import { PrismaClient } from '@prisma/client'
declare global {
var prisma: PrismaClient | undefined
}
export const db = globalThis.prisma || new PrismaClient()
if (process.env.NODE_ENV !== 'production') globalThis.prisma = db

35
lib/mail.ts Normal file
View file

@ -0,0 +1,35 @@
import ResetPassword from '@/emails/reset-email'
import LinkEmail from '@/emails/verify-email'
import { render } from '@react-email/components'
import { Link } from 'lucide-react'
import { Resend } from 'resend'
const resend = new Resend(process.env.RESEND_API_KEY)
// Send a verification email to the user
export const sendVerificationEmail = async (email: string, token: string) => {
const confirmLink = `http://localhost:3000/new-verification?token=${token}`
await resend.emails.send({
from: 'Nizar <noreply@resend.dev>',
to: email,
subject: 'Confirm your email',
html: render(LinkEmail({ token }))
})
resend.contacts.create({
email: email,
audienceId: process.env.RESEND_AUDIENCE as string
})
}
// Send password reset token to user
export const sendPasswordResetEmail = async (email: string, token: string) => {
const resetLink = `${process.env.APP_URL}/new-password?token=${token}`
await resend.emails.send({
from: 'Nizar <noreply@resend.dev>',
to: email,
subject: 'Reset your password',
html: render(ResetPassword({ token }))
})
}

6
lib/stripe.ts Normal file
View file

@ -0,0 +1,6 @@
import Stripe from 'stripe'
export const stripe = new Stripe(process.env.STRIPE_API_KEY!, {
apiVersion: '2023-10-16',
typescript: true
})

2
lib/theme-constant.ts Normal file
View file

@ -0,0 +1,2 @@
export const light_theme = 'light'
export const dark_theme = 'dark'

54
lib/tokens.ts Normal file
View file

@ -0,0 +1,54 @@
import { getVerificationTokenByEmail } from '@/data/verification-token'
import { db } from '@/lib/db'
import { v4 as uuidv4 } from 'uuid'
import { getPasswordResetTokenByEmail } from '@/data/password-reset-token'
export const generateVerificationToken = async (email: string) => {
// Generate Verification Token
const token = uuidv4()
// expires token in 1 hour
const expires = new Date(new Date().getTime() + 3600 * 1000)
// check if we have an exisiting token sent to his email, if it is, remove it
const existingToken = await getVerificationTokenByEmail(email)
// delete function
if (existingToken) {
await db.verificationToken.delete({
where: {
id: existingToken.id
}
})
}
// create new token
const verificationToken = await db.verificationToken.create({
data: {
email,
token,
expires
}
})
return verificationToken
}
export const generatePasswordResetToken = async (email: string) => {
const token = uuidv4()
// expires token in 1 hour
const expires = new Date(new Date().getTime() + 3600 * 1000)
// check if we have an exisiting token sent to his email, if it is, remove it
const existingToken = await getPasswordResetTokenByEmail(email)
// delete function
if (existingToken) {
await db.passwordResetToken.delete({
where: { id: existingToken.id }
})
}
// create new token
const passwordResetToken = await db.passwordResetToken.create({
data: {
email,
token,
expires
}
})
return passwordResetToken
}

6
lib/utils.ts Normal file
View file

@ -0,0 +1,6 @@
import { type ClassValue, clsx } from 'clsx'
import { twMerge } from 'tailwind-merge'
export function cn(...inputs: ClassValue[]) {
return twMerge(clsx(inputs))
}