Skip to content

Commit

Permalink
Added Email templates and mail to all email addresses
Browse files Browse the repository at this point in the history
  • Loading branch information
anoopkarnik committed Jun 13, 2024
1 parent 47b41c6 commit e532fca
Show file tree
Hide file tree
Showing 11 changed files with 76 additions and 19 deletions.
2 changes: 1 addition & 1 deletion apps/dashboard-app/ actions/login.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ export const login = async (values:z.infer<typeof LoginSchema>) =>{
}

try{
await signIn('credentials', {email, password,redirect:true, redirectTo: DEFAULT_LOGIN_REDIRECT });
await signIn('credentials', {email, password});
}
catch(err){
if (err instanceof AuthError ){
Expand Down
3 changes: 1 addition & 2 deletions apps/dashboard-app/app/dashboard/layout.tsx
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
import NavbarClient from "../../components/NavbarClient";
import {auth} from '@repo/next-auth/auth'

export default async function Layout({children}:{children: React.ReactNode}){
const session = await auth()

return (
<div className="w-full overflow-hidden">
<NavbarClient />
Expand Down
9 changes: 9 additions & 0 deletions apps/dashboard-app/app/dashboard/page.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import React from 'react'

const page = () => {
return (
<div></div>
)
}

export default page
3 changes: 2 additions & 1 deletion apps/dashboard-app/components/NavbarClient.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import { useRouter } from "next/navigation";
import { logout } from "../ actions/logout";
import { useCurrentUser } from "../hooks/useCurrentUser";
import { resetPasswordSettings } from "../ actions/reset-password-settings";
import { signOut } from "next-auth/react";

export default function NavbarClient() {
const { theme, setTheme } = useTheme()
Expand All @@ -21,7 +22,7 @@ export default function NavbarClient() {
user= {user}
setTheme={setTheme}
onSignin={()=>{router.push('/auth/login')}}
onSignout={async () =>{await logout();router.push('/auth/login');}}
onSignout={async() => await signOut()}
resetFunction={resetPasswordSettings}
/>
)
Expand Down
2 changes: 1 addition & 1 deletion apps/dashboard-app/routes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,4 @@ export const authRoutes =[

export const apiAuthPrefix = "/api/auth"

export const DEFAULT_LOGIN_REDIRECT = "/dashboard/practise-apps"
export const DEFAULT_LOGIN_REDIRECT = "/"
1 change: 1 addition & 0 deletions packages/next-auth/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
.env
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
-- CreateTable
CREATE TABLE "EmailTemplate" (
"id" TEXT NOT NULL,
"name" TEXT NOT NULL,
"from" TEXT NOT NULL,
"subject" TEXT NOT NULL,
"html" TEXT NOT NULL,
"createdAt" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
"updatedAt" TIMESTAMP(3) NOT NULL,

CONSTRAINT "EmailTemplate_pkey" PRIMARY KEY ("id")
);
10 changes: 10 additions & 0 deletions packages/prisma-db/prisma/schema.prisma
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,16 @@ model User {
updatedAt DateTime @updatedAt
}

model EmailTemplate {
id String @id @default(cuid())
name String
from String
subject String
html String
createdAt DateTime @default(now())
updatedAt DateTime @updatedAt
}

enum UserRole {
USER
ADMIN
Expand Down
14 changes: 14 additions & 0 deletions packages/prisma-db/src/email-template.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import db from './index'
import {v4 as uuidv4} from 'uuid';

export const getEmailTemplateByName = async (name: string) => {
try{
const emailTemplate = await db.emailTemplate.findFirst({
where:{name}
});
return emailTemplate;
}
catch (error){
return null;
}
}
3 changes: 2 additions & 1 deletion packages/resend-email/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
"./mail": "./src/index.ts"
},
"dependencies": {
"resend": "^3.2.0"
"resend": "^3.2.0",
"@repo/prisma-db": "*"
}
}
36 changes: 23 additions & 13 deletions packages/resend-email/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,30 +1,40 @@
import {Resend} from 'resend';
import { getEmailTemplateByName } from '@repo/prisma-db/repo/email-template';

const resend = new Resend(process.env.RESEND_API_KEY)

export const sendVerificationEmail = async (email: string, token:string) => {
const confirmLink = `http://localhost:4000/auth/new-verification?token=${token}`

const emailTemplate = await getEmailTemplateByName('verification-mail');
if (!emailTemplate) {
return {error: "Verification Mail Template Not Found!"}
}
let html = emailTemplate.html;
html = html.replace('{{confirmation_link}}', confirmLink);
let from = emailTemplate.from;
let subject = emailTemplate.subject;
await resend.emails.send({
from: "[email protected]",
from: from,
to: email,
subject: "Please confirm your email",
html: `<p>
Click <a href="${confirmLink}">here</a> to confirm your email
</p>`
subject: subject,
html: html
})
}


export const sendResetEmail = async (email: string, token:string) => {
const confirmLink = `http://localhost:4000/auth/reset-password?token=${token}`

const emailTemplate = await getEmailTemplateByName('reset-password');
if (!emailTemplate) {
return {error: "Email template not found!"}
}
let html = emailTemplate.html;
html = html.replace('{{reset_password_link}}', confirmLink);
let from = emailTemplate.from;
let subject = emailTemplate.subject;
await resend.emails.send({
from: "[email protected]",
from: from,
to: email,
subject: "Reset your password",
html: `<p>
Click <a href="${confirmLink}">here</a> to modify your password
</p>`
subject: subject,
html: html
})
}

0 comments on commit e532fca

Please sign in to comment.