Repository navigation
Expand file tree
/
Copy pathemailToken.ts
More file actions
113 lines (107 loc) · 3.83 KB
/
Copy pathemailToken.ts
File metadata and controls
113 lines (107 loc) · 3.83 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
import { db } from "@/server/db";
import { user } from "@/server/db/schema";
import crypto from "crypto";
import sendEmail from "./sendEmail";
import { eq } from "drizzle-orm";
import { getAppOrigin } from "@/server/lib/url";
export const generateEmailToken = () => {
return crypto.randomBytes(64).toString("hex");
};
export const sendVerificationEmail = async (email: string, token: string) => {
const verificationLink = `${getAppOrigin()}/verify-email?token=${token}`;
const subject = "Confirm your email — one tap and you're in";
const htmlMessage = `
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Email Verification</title>
<style>
body {
font-family: Arial, sans-serif;
background-color: #f4f4f4;
color: #333;
margin: 0;
padding: 0;
}
.container {
width: 100%;
max-width: 600px;
margin: 0 auto;
background-color: #ffffff;
padding: 20px;
border-radius: 8px;
box-shadow: 0 2px 4px rgba(0, 0, 0, 0.1);
}
h1 {
font-size: 24px;
margin-bottom: 20px;
}
p {
font-size: 16px;
line-height: 1.6;
margin-bottom: 20px;
}
.btn {
display: inline-block;
padding: 12px 24px;
font-size: 16px;
color: #fff !important;
font-weight: bold;
background-color: #007bff;
text-decoration: none;
border-radius: 4px;
margin-top: 20px;
}
.footer {
margin-top: 40px;
font-size: 12px;
color: #777;
}
</style>
</head>
<body>
<div class="container">
<h1>Confirm your email</h1>
<p>Welcome to Codú — the community for AI builders and indie hackers. Less theory, more shipping.</p>
<p>Tap the button below to confirm your email and finish setting up your account:</p>
<a href="${verificationLink}" class="btn">Confirm my email</a>
<p>This link expires in 1 hour. If it does, just request a new one.</p>
<p>If the button doesn't work, copy and paste this URL into your browser:</p>
<p style="word-break: break-all; font-size: 14px; color: #555;">${verificationLink}</p>
<p>If you didn't sign up for Codú, you can safely ignore this email.</p>
<p>See you in the build,<br>The Codú team</p>
<div class="footer">
<p>© ${new Date().getFullYear()} Codú. All rights reserved.</p>
</div>
</div>
</body>
</html>
`;
try {
return sendEmail({ recipient: email, htmlMessage, subject });
} catch (error) {
console.error("Error sending verification email:", error);
throw new Error("Failed to send verification email");
}
};
export const updateEmail = async (userId: string, newEmail: string) => {
try {
await db.update(user).set({ email: newEmail }).where(eq(user.id, userId));
} catch (error) {
console.error("Error updating email in database:", error);
throw new Error("Failed to update email");
}
};
export const checkIfEmailExists = async (email: string) => {
try {
const existingUser = await db.query.user.findFirst({
where: (users, { eq }) => eq(users.email, email),
});
return !!existingUser;
} catch (error) {
console.error("Error checking if email exists:", error);
throw new Error("Failed to check if email exists");
}
};