Repository navigation
Expand file tree
/
Copy pathauth.ts
More file actions
118 lines (109 loc) · 3.91 KB
/
Copy pathauth.ts
File metadata and controls
118 lines (109 loc) · 3.91 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
114
115
116
117
118
import { betterAuth } from "better-auth";
import { drizzleAdapter } from "better-auth/adapters/drizzle";
import { magicLink } from "better-auth/plugins";
import { nextCookies } from "better-auth/next-js";
import { db } from "@/lib/db";
import { users, accounts, sessions, verification } from "@/lib/db/schema";
import { Resend } from "resend";
import { env } from "@/lib/env";
const resend = new Resend(env.AUTH_RESEND_KEY);
async function sendEmail({ to, subject, text, html }: { to: string; subject: string; text: string; html?: string }) {
try {
if (process.env.NODE_ENV === "development") {
console.log("EMAIL (DEV MODE)->", { to, subject, text });
return;
}
await resend.emails.send({
from: env.EMAIL_FROM || "onboarding@resend.dev",
to,
subject,
text,
html,
});
} catch (error) {
console.error("Failed to send email:", error);
}
}
export const auth = betterAuth({
database: drizzleAdapter(db, {
provider: "pg",
schema: {
user: users,
account: accounts,
session: sessions,
verification: verification,
},
}),
plugins: [
nextCookies(),
magicLink({
sendMagicLink: async ({ email, token, url }) => {
await sendEmail({
to: email,
subject: "Sign in to Coldboard",
text: `Click the link to sign in: ${url}`,
html: `
<div style="font-family: Arial, sans-serif; max-width: 600px; margin: 0 auto;">
<h2>Sign in to Coldboard</h2>
<p>Click the button below to sign in to your account:</p>
<a href="${url}" style="background-color: #3b82f6; color: white; padding: 12px 24px; text-decoration: none; border-radius: 6px; display: inline-block;">Sign In</a>
<p>If the button doesn't work, copy and paste this link: ${url}</p>
<p>This link will expire in 24 hours.</p>
</div>
`,
});
},
}),
],
emailAndPassword: {
enabled: true,
sendResetPassword: async ({user, url, token}) => {
await sendEmail({
to: user.email,
subject: "Reset your password - Coldboard",
text: `Click the link to reset your password: ${url}`,
html: `
<div style="font-family: Arial, sans-serif; max-width: 600px; margin: 0 auto;">
<h2>Reset your password</h2>
<p>Click the button below to reset your password:</p>
<a href="${url}" style="background-color: #3b82f6; color: white; padding: 12px 24px; text-decoration: none; border-radius: 6px; display: inline-block;">Reset Password</a>
<p>If the button doesn't work, copy and paste this link: ${url}</p>
<p>This link will expire in 1 hour.</p>
</div>
`,
});
},
},
emailVerification: {
sendVerificationEmail: async ({user, url, token}) => {
await sendEmail({
to: user.email,
subject: "Verify your email - Coldboard",
text: `Click the link to verify your email: ${url}`,
html: `
<div style="font-family: Arial, sans-serif; max-width: 600px; margin: 0 auto;">
<h2>Welcome to Coldboard!</h2>
<p>Click the button below to verify your email address:</p>
<a href="${url}" style="background-color: #3b82f6; color: white; padding: 12px 24px; text-decoration: none; border-radius: 6px; display: inline-block;">Verify Email</a>
<p>If the button doesn't work, copy and paste this link: ${url}</p>
<p>This link will expire in 24 hours.</p>
</div>
`,
});
},
},
socialProviders: {
google: {
clientId: env.GOOGLE_CLIENT_ID!,
clientSecret: env.GOOGLE_CLIENT_SECRET!,
},
github: {
clientId: env.GITHUB_CLIENT_ID!,
clientSecret: env.GITHUB_CLIENT_SECRET!,
},
},
session: {
expiresIn: 60 * 60 * 24 * 7, // 7 days
updateAge: 60 * 60 * 24, // 1 day
},
});