-
Notifications
You must be signed in to change notification settings - Fork 26
Expand file tree
/
Copy pathauth.ts
More file actions
75 lines (72 loc) · 2.37 KB
/
auth.ts
File metadata and controls
75 lines (72 loc) · 2.37 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
import { betterAuth, BetterAuthOptions } from "better-auth";
import { prismaAdapter } from "better-auth/adapters/prisma";
import prisma from "@/lib/prisma";
import { sendEmail } from "@/actions/email";
import { openAPI } from "better-auth/plugins";
import { admin } from "better-auth/plugins";
export const auth = betterAuth({
database: prismaAdapter(prisma, {
provider: "mongodb",
}),
session: {
expiresIn: 60 * 60 * 24 * 7, // 7 days
// BUG: Prob a bug with updateAge method. It throws an error - Argument `where` of type SessionWhereUniqueInput needs at least one of `id` arguments.
// As a workaround, set updateAge to a large value for now.
updateAge: 60 * 60 * 24 * 7, // 7 days (every 7 days the session expiration is updated)
cookieCache: {
enabled: true,
maxAge: 5 * 60 // Cache duration in seconds
}
},
user: {
additionalFields: {
premium: {
type: "boolean",
required: false,
},
},
changeEmail: {
enabled: true,
sendChangeEmailVerification: async ({ newEmail, url }) => {
await sendEmail({
to: newEmail,
subject: 'Verify your email change',
text: `Click the link to verify: ${url}`
})
}
}
},
socialProviders: {
github: {
clientId: process.env.GITHUB_CLIENT_ID as string,
clientSecret: process.env.GITHUB_CLIENT_SECRET as string,
},
},
plugins: [openAPI(), admin({
impersonationSessionDuration: 60 * 60 * 24 * 7, // 7 days
})], // api/auth/reference
emailAndPassword: {
enabled: true,
requireEmailVerification: true,
sendResetPassword: async ({ user, url }) => {
await sendEmail({
to: user.email,
subject: "Reset your password",
text: `Click the link to reset your password: ${url}`,
});
},
},
emailVerification: {
sendOnSignUp: true,
autoSignInAfterVerification: true,
sendVerificationEmail: async ({ user, token }) => {
const verificationUrl = `${process.env.BETTER_AUTH_URL}/api/auth/verify-email?token=${token}&callbackURL=${process.env.EMAIL_VERIFICATION_CALLBACK_URL}`;
await sendEmail({
to: user.email,
subject: "Verify your email address",
text: `Click the link to verify your email: ${verificationUrl}`,
});
},
}
} satisfies BetterAuthOptions);
export type Session = typeof auth.$Infer.Session;