@rimelight/auth 0.0.2 → 0.0.3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,149 +0,0 @@
1
- import type { BetterAuthOptions } from "better-auth"
2
- import { betterAuth } from "better-auth"
3
- import { createSyntheticUserWrapper } from "../plugins/synthetic-user"
4
- import { createTagAndUsernameHook } from "../plugins/discriminator-tag"
5
-
6
- export interface RimelightAuthFactoryOptions {
7
- database: BetterAuthOptions["database"]
8
- db?: any
9
- userTable?: any
10
- emailHandlers?: {
11
- sendVerificationEmail?: (data: any, request?: Request) => Promise<void>
12
- sendPasswordResetEmail?: (data: any, request?: Request) => Promise<void>
13
- sendExistingUserSignUpNotification?: (existingUser: any) => Promise<void>
14
- }
15
- additionalUserFields?: Record<string, any>
16
- plugins?: BetterAuthOptions["plugins"]
17
- statements?: any
18
- advanced?: BetterAuthOptions["advanced"]
19
- session?: BetterAuthOptions["session"]
20
- rateLimit?: BetterAuthOptions["rateLimit"]
21
- customDatabaseHooks?: BetterAuthOptions["databaseHooks"]
22
- enableTagGenerator?: boolean
23
- restrictedUsernameBlacklist?: string[]
24
- }
25
-
26
- export function createRimelightAuth(options: RimelightAuthFactoryOptions) {
27
- const enableTag = options.enableTagGenerator ?? (options.db && options.userTable)
28
-
29
- const defaultUserHooks =
30
- enableTag && options.db && options.userTable
31
- ? {
32
- create: {
33
- before: createTagAndUsernameHook(options.db, options.userTable)
34
- }
35
- }
36
- : undefined
37
-
38
- const emailAndPasswordConfig: BetterAuthOptions["emailAndPassword"] = {
39
- enabled: true,
40
- autoSignIn: false,
41
- requireEmailVerification: true,
42
- minPasswordLength: 8,
43
- maxPasswordLength: 128,
44
- customSyntheticUser: createSyntheticUserWrapper
45
- }
46
-
47
- if (options.emailHandlers?.sendPasswordResetEmail) {
48
- emailAndPasswordConfig.sendResetPassword = options.emailHandlers.sendPasswordResetEmail
49
- }
50
- if (options.emailHandlers?.sendExistingUserSignUpNotification) {
51
- const handler = options.emailHandlers.sendExistingUserSignUpNotification
52
- emailAndPasswordConfig.onExistingUserSignUp = async (existingUser: any) => {
53
- await handler(existingUser)
54
- }
55
- }
56
-
57
- const emailVerificationConfig: BetterAuthOptions["emailVerification"] = {
58
- autoSignInAfterVerification: true
59
- }
60
-
61
- if (options.emailHandlers?.sendVerificationEmail) {
62
- emailVerificationConfig.sendVerificationEmail = options.emailHandlers.sendVerificationEmail
63
- }
64
-
65
- return betterAuth({
66
- database: options.database,
67
- emailAndPassword: emailAndPasswordConfig,
68
- emailVerification: emailVerificationConfig,
69
- user: {
70
- changeEmail: {
71
- enabled: true
72
- },
73
- deleteUser: {
74
- enabled: true
75
- },
76
- additionalFields: {
77
- tag: {
78
- type: "string",
79
- required: false,
80
- input: false
81
- },
82
- firstName: {
83
- type: "string",
84
- required: true,
85
- default: "",
86
- input: true
87
- },
88
- lastName: {
89
- type: "string",
90
- required: true,
91
- default: "",
92
- input: true
93
- },
94
- role: {
95
- type: "string",
96
- required: false,
97
- default: "user",
98
- input: false
99
- },
100
- availability: {
101
- type: "string",
102
- required: false,
103
- default: "available"
104
- },
105
- status: {
106
- type: "string",
107
- required: false,
108
- default: ""
109
- },
110
- publicKey: {
111
- type: "string",
112
- required: false,
113
- input: true
114
- },
115
- ...options.additionalUserFields
116
- }
117
- },
118
- session: {
119
- expiresIn: 60 * 60 * 24 * 7,
120
- updateAge: 60 * 60 * 24,
121
- freshAge: 60 * 15,
122
- cookieCache: {
123
- enabled: true,
124
- maxAge: 60 * 5
125
- },
126
- ...options.session
127
- },
128
- rateLimit: {
129
- window: 10,
130
- max: 100,
131
- storage: "database",
132
- modelName: "rateLimit",
133
- ...options.rateLimit
134
- },
135
- plugins: options.plugins ?? [],
136
- auth: options.statements ? { statements: options.statements } : undefined,
137
- advanced: {
138
- useSecureCookies:
139
- (import.meta as any).env?.MODE === "production" || process.env.NODE_ENV === "production",
140
- cookiePrefix: "auth",
141
- database: {
142
- generateId: () => crypto.randomUUID()
143
- },
144
- ...options.advanced
145
- },
146
- databaseHooks:
147
- options.customDatabaseHooks ?? (defaultUserHooks ? { user: defaultUserHooks } : undefined)
148
- })
149
- }