From e03adfacde8a36260567a054e39ad58900c47a3b Mon Sep 17 00:00:00 2001 From: Moinul Moin Date: Sat, 29 Jul 2023 11:53:33 +0600 Subject: [PATCH] refactor: set resend as custom email provider and remove nodemailer --- .eslintrc.json | 2 +- components.json | 2 +- next.config.js | 4 +++ package.json | 1 - pnpm-lock.yaml | 15 ++-------- src/components/layout/auth-form.tsx | 2 +- src/lib/auth.ts | 38 ++++++++++++++---------- tailwind.config.ts => tailwind.config.js | 0 8 files changed, 32 insertions(+), 32 deletions(-) rename tailwind.config.ts => tailwind.config.js (100%) diff --git a/.eslintrc.json b/.eslintrc.json index bd381c9..35fb232 100644 --- a/.eslintrc.json +++ b/.eslintrc.json @@ -26,6 +26,6 @@ ] }, "ignorePatterns": [ - "next.config.js" + "*.config.js" ] } \ No newline at end of file diff --git a/components.json b/components.json index 04da60b..a204109 100644 --- a/components.json +++ b/components.json @@ -4,7 +4,7 @@ "rsc": true, "tsx": true, "tailwind": { - "config": "tailwind.config.ts", + "config": "tailwind.config.js", "css": "src/app/globals.css", "baseColor": "slate", "cssVariables": true diff --git a/next.config.js b/next.config.js index 907fcd3..c49f4ff 100644 --- a/next.config.js +++ b/next.config.js @@ -1,6 +1,7 @@ const { withContentlayer } = require("next-contentlayer"); const withPWA = require("@ducanh2912/next-pwa").default({ dest: "public", + disable: process.env.NODE_ENV !== "production", }); /** @type {import('next').NextConfig} */ @@ -9,6 +10,9 @@ const nextConfig = { swcMinify: true, experimental: { serverActions: true, + serverComponentsExternalPackages: [ + "@prisma/client", + ], }, }; diff --git a/package.json b/package.json index 60c1cfd..7925246 100644 --- a/package.json +++ b/package.json @@ -38,7 +38,6 @@ "next-auth": "^4.22.1", "next-contentlayer": "^0.3.3", "next-themes": "^0.2.1", - "nodemailer": "^6.9.3", "postcss": "8.4.24", "prisma": "^5.0.0", "react": "18.2.0", diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index 15f9de3..430e17d 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -76,16 +76,13 @@ dependencies: version: 13.4.9(@babel/core@7.22.9)(@opentelemetry/api@1.4.1)(react-dom@18.2.0)(react@18.2.0) next-auth: specifier: ^4.22.1 - version: 4.22.1(next@13.4.9)(nodemailer@6.9.3)(react-dom@18.2.0)(react@18.2.0) + version: 4.22.1(next@13.4.9)(react-dom@18.2.0)(react@18.2.0) next-contentlayer: specifier: ^0.3.3 version: 0.3.3(esbuild@0.18.12)(next@13.4.9)(react-dom@18.2.0)(react@18.2.0) next-themes: specifier: ^0.2.1 version: 0.2.1(next@13.4.9)(react-dom@18.2.0)(react@18.2.0) - nodemailer: - specifier: ^6.9.3 - version: 6.9.3 postcss: specifier: 8.4.24 version: 8.4.24 @@ -2008,7 +2005,7 @@ packages: next-auth: ^4 dependencies: '@prisma/client': 5.0.0(prisma@5.0.0) - next-auth: 4.22.1(next@13.4.9)(nodemailer@6.9.3)(react-dom@18.2.0)(react@18.2.0) + next-auth: 4.22.1(next@13.4.9)(react-dom@18.2.0)(react@18.2.0) dev: false /@next/env@13.4.9: @@ -7234,7 +7231,7 @@ packages: resolution: {integrity: sha512-Yd3UES5mWCSqR+qNT93S3UoYUkqAZ9lLg8a7g9rimsWmYGK8cVToA4/sF3RrshdyV3sAGMXVUmpMYOw+dLpOuw==} dev: false - /next-auth@4.22.1(next@13.4.9)(nodemailer@6.9.3)(react-dom@18.2.0)(react@18.2.0): + /next-auth@4.22.1(next@13.4.9)(react-dom@18.2.0)(react@18.2.0): resolution: {integrity: sha512-NTR3f6W7/AWXKw8GSsgSyQcDW6jkslZLH8AiZa5PQ09w1kR8uHtR9rez/E9gAq/o17+p0JYHE8QjF3RoniiObA==} peerDependencies: next: ^12.2.5 || ^13 @@ -7250,7 +7247,6 @@ packages: cookie: 0.5.0 jose: 4.14.4 next: 13.4.9(@babel/core@7.22.9)(@opentelemetry/api@1.4.1)(react-dom@18.2.0)(react@18.2.0) - nodemailer: 6.9.3 oauth: 0.9.15 openid-client: 5.4.2 preact: 10.15.0 @@ -7375,11 +7371,6 @@ packages: resolution: {integrity: sha512-uYr7J37ae/ORWdZeQ1xxMJe3NtdmqMC/JZK+geofDrkLUApKRHPd18/TxtBOJ4A0/+uUIliorNrfYV6s1b02eQ==} dev: false - /nodemailer@6.9.3: - resolution: {integrity: sha512-fy9v3NgTzBngrMFkDsKEj0r02U7jm6XfC3b52eoNV+GCrGj+s8pt5OqhiJdWKuw51zCTdiNR/IUD1z33LIIGpg==} - engines: {node: '>=6.0.0'} - dev: false - /nopt@6.0.0: resolution: {integrity: sha512-ZwLpbTgdhuZUnZzjd7nb1ZV+4DoiC6/sfiVKok72ym/4Tlf+DFdlHYmT2JPmcNNWV6Pi3SDf1kT+A4r9RTuT9g==} engines: {node: ^12.13.0 || ^14.15.0 || >=16.0.0} diff --git a/src/components/layout/auth-form.tsx b/src/components/layout/auth-form.tsx index 9c179bc..9bda747 100644 --- a/src/components/layout/auth-form.tsx +++ b/src/components/layout/auth-form.tsx @@ -38,7 +38,7 @@ export default function AuthForm() { saCheckEmailExists(data.email.toLowerCase()) .then(async () => { try { - const res = await signIn("email", { + const res = await signIn("resend", { email: data.email.toLowerCase(), redirect: false, }); diff --git a/src/lib/auth.ts b/src/lib/auth.ts index 36ca994..ebf9377 100644 --- a/src/lib/auth.ts +++ b/src/lib/auth.ts @@ -1,42 +1,48 @@ import { PrismaAdapter } from "@next-auth/prisma-adapter"; import { type NextAuthOptions } from "next-auth"; -import EmailProvider, { - type SendVerificationRequestParams, -} from "next-auth/providers/email"; +import { type SendVerificationRequestParams } from "next-auth/providers/email"; import GithubProvider from "next-auth/providers/github"; import db from "~/lib/db"; import { sendMail } from "~/lib/resend"; const sendVerificationRequest = async ({ - identifier, + identifier: email, url, }: SendVerificationRequestParams) => { const user = await db.user.findFirst({ where: { - email: identifier, + email, }, select: { - emailVerified: true, name: true, }, }); - await sendMail({ - toMail: identifier, - type: "verification", - data: { - name: user?.name as string, - url, - }, - }); + try { + await sendMail({ + toMail: email, + type: "verification", + data: { + name: user?.name as string, + url, + }, + }); + } catch (error) { + throw new Error(JSON.stringify(error)); + } }; export const authOptions: NextAuthOptions = { adapter: PrismaAdapter(db), providers: [ - EmailProvider({ + { + id: "resend", + type: "email", + name: "Email Provider", + server: null, + options: {}, sendVerificationRequest, - }), + }, GithubProvider({ clientId: process.env.GITHUB_CLIENT_ID as string, clientSecret: process.env.GITHUB_CLIENT_SECRET as string, diff --git a/tailwind.config.ts b/tailwind.config.js similarity index 100% rename from tailwind.config.ts rename to tailwind.config.js