-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathauth.ts
More file actions
168 lines (158 loc) · 4.84 KB
/
Copy pathauth.ts
File metadata and controls
168 lines (158 loc) · 4.84 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
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
import { betterAuth } from "better-auth"
import { nextCookies } from "better-auth/next-js"
import { admin, multiSession, twoFactor, organization, jwt } from "better-auth/plugins"
import { passkey } from "better-auth/plugins/passkey"
import { pgConnection } from "@/lib/database/postgres"
import { AUTH_MESSAGES, CONFIG } from "@/lib/constants"
export const auth = betterAuth({
database: pgConnection,
basePath: "/api/auth",
baseURL: process.env.BETTER_AUTH_URL || "http://localhost:3000",
session: {
cookieCache: {
enabled: true,
maxAge: 60 * 5,
},
expiresIn: 60 * 60 * 24 * 7,
updateAge: 60 * 60 * 24,
cookieAttributes: {
sameSite: "lax",
secure: process.env.NODE_ENV === "production",
httpOnly: true,
domain: process.env.NODE_ENV === "production" ? "dataatmos.ai" : undefined,
},
},
emailAndPassword: {
enabled: true,
requireEmailVerification: true,
minPasswordLength: 8,
maxPasswordLength: 128,
sendResetPassword: async ({ user, url, token }) => {
const { sendPasswordResetEmail } = await import("@/lib/services/email-service")
await sendPasswordResetEmail({ user, url, token })
},
},
emailVerification: {
sendOnSignUp: true,
autoSignInAfterVerification: true,
sendVerificationEmail: async ({ user, url, token }) => {
const { sendVerificationEmail } = await import("@/lib/services/email-service")
await sendVerificationEmail({ user, url, token })
},
},
socialProviders: {
google: {
clientId: process.env.GOOGLE_CLIENT_ID as string,
clientSecret: process.env.GOOGLE_CLIENT_SECRET as string,
redirectURI: `${process.env.BETTER_AUTH_URL}/api/auth/callback/google`,
},
},
rateLimit: {
window: 60,
max: 10,
},
plugins: [
nextCookies(),
jwt({
jwks: {
keyPairConfig: {
alg: "EdDSA",
crv: "Ed25519"
}
},
jwt: {
definePayload: ({ user }) => {
return {
id: user.id,
username: user.name,
email: user.email,
role: user.role,
}
},
issuer: process.env.BETTER_AUTH_URL || "http://localhost:3000",
audience: process.env.BETTER_AUTH_URL || "http://localhost:3000",
expirationTime: "1h",
getSubject: (session) => {
return session.user.id
}
}
}),
organization({
allowUserToCreateOrganization: true,
organizationLimit: 2,
membershipLimit: 5,
creatorRole: "owner",
invitationExpiresIn: 60 * 60 * 48,
cancelPendingInvitationsOnReInvite: true,
invitationLimit: 20,
async sendInvitationEmail(data) {
const { sendOrganizationInvitationEmail } = await import("@/lib/services/email-service")
await sendOrganizationInvitationEmail({
email: data.email,
invitedByUsername: data.inviter.user.name,
invitedByEmail: data.inviter.user.email,
organizationName: data.organization.name,
inviteLink: `${process.env.BETTER_AUTH_URL}/accept-invitation/${data.id}`,
invitationId: data.id,
})
},
}),
admin({
adminUserIds: [process.env.RAGHU_USER_ID as string],
defaultRole: "user",
adminRoles: ["admin"],
impersonationSessionDuration: 60 * 60 * 2,
defaultBanReason: AUTH_MESSAGES.DEFAULT_BAN_REASON,
defaultBanExpiresIn: 60 * 60 * 24 * 7,
bannedUserMessage:
"Your account has been suspended. Please contact support at support@dataatmos.ai if you believe this is an error.",
}),
twoFactor({
issuer: "dataatmos.ai",
totpOptions: {
digits: 6,
period: 30,
},
otpOptions: {
async sendOTP({ user, otp }) {
const { sendOTPEmail } = await import("@/lib/services/email-service")
await sendOTPEmail({ user, otp })
},
period: 5,
storeOTP: "plain",
},
backupCodeOptions: {
amount: 10,
length: 10,
storeBackupCodes: "plain",
},
skipVerificationOnEnable: false,
}),
multiSession({
maximumSessions: 5,
}),
passkey({
rpID: "dataatmos.ai",
rpName: "Data Atmos",
origin:
process.env.NODE_ENV === "production" ? "https://dataatmos.ai" : "http://localhost:3000",
}),
],
trustedOrigins: ["exp://", process.env.BETTER_AUTH_URL || "http://localhost:3000"],
advanced: {
crossSubDomainCookies: {
enabled: process.env.NODE_ENV === "production",
domain: ".dataatmos.ai",
},
},
...(process.env.NODE_ENV === "production" && {
strictMode: true,
forceHTTPS: true,
securityHeaders: {
contentSecurityPolicy: true,
xFrameOptions: CONFIG.SECURITY.X_FRAME_OPTIONS,
xContentTypeOptions: "nosniff",
referrerPolicy: "strict-origin-when-cross-origin",
},
}),
})