-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy pathauth.ts
More file actions
111 lines (108 loc) · 3.64 KB
/
auth.ts
File metadata and controls
111 lines (108 loc) · 3.64 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
import index from "@/app/(dev)/ai/new/index";
import env from "@/libs/env";
import { betterAuth } from "better-auth";
import { prismaAdapter } from "better-auth/adapters/prisma";
import { nextCookies } from "better-auth/next-js";
import { customSession, emailOTP, magicLink, multiSession, twoFactor } from "better-auth/plugins";
import { Resend } from "resend";
import { prisma, redis } from "./src/libs/db";
const resend = new Resend(process.env.RESEND_API_KEY);
export const auth = betterAuth({
database: prismaAdapter(prisma, {
provider: "postgresql",
}),
appName: "mailbuddy.dev",
user: {
deleteUser: {
enabled: true,
beforeDelete: async (user) => {
await index.index.deleteNamespace(`user_${user.id}`);
},
},
},
socialProviders: {
google: {
clientId: env.GOOGLE_CLIENT_ID as string,
clientSecret: env.GOOGLE_CLIENT_SECRET as string,
scope: [
"email",
"https://www.googleapis.com/auth/gmail.modify",
"https://www.googleapis.com/auth/gmail.readonly",
],
accessType: "offline",
prompt: "consent",
},
},
emailAndPassword: {
enabled: true,
minPasswordLength: 8,
maxPasswordLength: 32,
requireEmailVerification: true,
sendResetPassword: async ({ user, url, token }, request) => {
await resend.emails.send({
from: "noreply@mailbuddy.dev",
to: user.email,
subject: "Reset your password",
text: `Click the link to reset your password: ${url}`,
});
},
},
emailVerification: {
autoSignInAfterVerification: true,
sendVerificationEmail: async ({ user, url, token }, request) => {
await resend.emails.send({
from: "noreply@mailbuddy.dev",
to: user.email,
subject: "Verify your email",
html: `<p>Click <a href="${url}">here</a> to verify your email</p>`,
});
},
},
session: {
expiresIn: 60 * 60 * 24 * 30, // 30 days
updateAge: 60 * 60 * 24, // 1 day
cookieCache: {
enabled: true,
maxAge: 5 * 60,
},
},
secondaryStorage: {
get: async (key) => {
const value = await redis.get(key);
return value ? value : null;
},
set: async (key, value, ttl) => {
if (ttl) await redis.set(key, value, "EX", ttl);
else await redis.set(key, value);
},
delete: async (key) => {
await redis.del(key);
},
},
plugins: [
multiSession(),
nextCookies(),
twoFactor(),
emailOTP({
sendVerificationOTP: async ({ email, otp }) => {
await resend.emails.send({
from: "noreply@mailbuddy.dev",
to: email,
subject: "Verify your email",
html: `<p>Your verification code is ${otp}</p>`,
});
},
}),
magicLink({
sendMagicLink: async ({ email, token }) => {
await resend.emails.send({
from: "noreply@mailbuddy.dev",
to: email,
subject: "Login to Mailbuddy",
html: `<p>Click <a href="${process.env.BETTER_AUTH_URL}/login?token=${token}">here</a> to login to Mailbuddy</p>`,
});
},
}),
],
});
export type User = (typeof auth)["$Infer"]["Session"]["user"];