|
1 | 1 | "use server" |
2 | 2 |
|
3 | | -import bcrypt from "bcrypt" |
| 3 | +import bcryptjs from "bcryptjs" |
4 | 4 | import { signIn, signOut } from "configs/auth" |
5 | 5 | import { Prisma } from "database" |
6 | 6 | import { createUser } from "database/src/users/queries" |
| 7 | +import { sendEmail } from "emails" |
| 8 | +import VerifyEmail from "emails/verify-email" |
7 | 9 |
|
8 | 10 | import { redirect } from "@/utils/navigation" |
9 | 11 |
|
@@ -33,15 +35,40 @@ export const signUp = async ( |
33 | 35 | try { |
34 | 36 | // hash password |
35 | 37 | const { email, password } = data |
36 | | - const hashedPassword = await bcrypt.hash(password, 10) |
| 38 | + const hashedPassword = await bcryptjs.hash(password, 10) |
37 | 39 |
|
38 | 40 | await createUser({ |
39 | 41 | data: { |
40 | 42 | email, |
41 | 43 | password: hashedPassword, |
42 | 44 | }, |
43 | 45 | }) |
| 46 | + |
| 47 | + console.log("create user...successfully") |
| 48 | + |
| 49 | + // create verification code |
| 50 | + const token = crypto.randomUUID() |
| 51 | + await prisma.verificationToken.create({ |
| 52 | + data: { |
| 53 | + token, // 6 digits code |
| 54 | + identifier: email, |
| 55 | + expires: new Date(Date.now() + 1000 * 60 * 60 * 24), // 1 day |
| 56 | + }, |
| 57 | + }) |
| 58 | + |
| 59 | + console.log("verification token...successfully") |
| 60 | + |
| 61 | + // send email |
| 62 | + await sendEmail({ |
| 63 | + email, |
| 64 | + subject: "Welcome to Next Forum", |
| 65 | + react: VerifyEmail({ |
| 66 | + token, |
| 67 | + email, |
| 68 | + }), |
| 69 | + }) |
44 | 70 | } catch (error) { |
| 71 | + console.error("signUp.error", error) |
45 | 72 | if (error?.error?.code === "P2002") { |
46 | 73 | return { |
47 | 74 | formErrors: null, |
|
0 commit comments