Skip to content

Commit ed3f125

Browse files
author
topstar210
committed
feat: email verify when register, error message
1 parent dfa647f commit ed3f125

File tree

5 files changed

+159
-19
lines changed

5 files changed

+159
-19
lines changed

app/(auth)/layout.tsx

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,9 @@ import { NextAuthProvider } from "../Provider";
22
import Header from "@/components/Header";
33
import "./globals.css";
44

5+
import 'react-toastify/dist/ReactToastify.css';
6+
import { ToastContainer } from 'react-toastify';
7+
58
export default function RootLayout({
69
children,
710
}: {
@@ -14,6 +17,7 @@ export default function RootLayout({
1417
<Header />
1518
{children}
1619
</NextAuthProvider>
20+
<ToastContainer />
1721
</body>
1822
</html>
1923
);

app/(auth)/register/Form.tsx

Lines changed: 13 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,9 @@ import { useRouter } from "next/navigation";
66
import { useState } from "react";
77
import { useForm, SubmitHandler } from "react-hook-form";
88
import Loader from "../loading";
9-
import { useSession } from "next-auth/react";
9+
import { useSession, signIn } from "next-auth/react";
10+
import axios from "axios";
11+
import { toast } from "react-toastify";
1012

1113
type Inputs = {
1214
email: string;
@@ -38,23 +40,16 @@ const Form = () => {
3840

3941
const formSubmit: SubmitHandler<Inputs> = async (form) => {
4042
const { fullName, email, password } = form;
41-
try {
42-
const res = await fetch("/api/auth/register", {
43-
method: "POST",
44-
headers: {
45-
"Content-Type": "application/json",
46-
},
47-
body: JSON.stringify({
48-
fullName,
49-
email,
50-
password,
51-
}),
52-
});
53-
res.status === 201 &&
54-
router.push("/login?success=Account has been created");
55-
} catch (err: any) {
56-
setMessage(err);
57-
}
43+
44+
await axios.post("/api/auth/register", {
45+
fullName,
46+
email,
47+
password,
48+
}).then((res:any) => {
49+
signIn("email", { email })
50+
}).catch((err:any) => {
51+
toast(err?.response?.data, { type: 'error' })
52+
})
5853
};
5954

6055
return (

package-lock.json

Lines changed: 121 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,18 +14,21 @@
1414
"@types/react": "18.2.21",
1515
"@types/react-dom": "18.2.7",
1616
"autoprefixer": "10.4.15",
17+
"axios": "^1.6.2",
1718
"bcryptjs": "^2.4.3",
1819
"eslint": "8.47.0",
1920
"eslint-config-next": "13.4.19",
2021
"hamburger-react": "^2.5.0",
2122
"mongoose": "^7.4.5",
2223
"next": "13.4.19",
2324
"next-auth": "^4.23.1",
25+
"nodemailer": "^6.9.7",
2426
"postcss": "8.4.28",
2527
"react": "18.2.0",
2628
"react-dom": "18.2.0",
2729
"react-hook-form": "^7.45.4",
2830
"react-icons": "^4.10.1",
31+
"react-toastify": "^9.1.3",
2932
"tailwindcss": "3.3.3",
3033
"typescript": "5.2.2"
3134
},

utilities/auth.ts

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import type { NextAuthOptions } from "next-auth";
33
import { MongoDBAdapter } from "@next-auth/mongodb-adapter";
44
import GoogleProvider from "next-auth/providers/google";
55
import CredentialsProvider from "next-auth/providers/credentials";
6+
import EmailProvider from "next-auth/providers/email"
67
import dbConnect from "./dbConnect";
78
import clientPromise from "./clientPromise";
89
import bcrypt from "bcryptjs";
@@ -59,11 +60,27 @@ export const authOptions: NextAuthOptions = {
5960
}
6061
},
6162
}),
63+
64+
EmailProvider({
65+
server: {
66+
host: process.env.EMAIL_SERVER_HOST,
67+
port: process.env.EMAIL_SERVER_PORT,
68+
secureConnection: false,
69+
requiresAuth: true,
70+
domains: ["gmail.com", "googlemail.com"],
71+
auth: {
72+
user: process.env.EMAIL_SERVER_USER,
73+
pass: process.env.EMAIL_SERVER_PASSWORD
74+
},
75+
tls: { rejectUnauthorized: false }
76+
},
77+
from: process.env.EMAIL_FROM,
78+
maxAge: 2 * 60 * 60, // How long email links are valid for (default 24h)
79+
}),
6280
],
6381
pages: {
6482
signIn: "/login",
6583
newUser: "/my/dashboard",
66-
error: "/login",
6784
},
6885
callbacks: {
6986
// We can pass in additional information from the user document MongoDB returns

0 commit comments

Comments
 (0)