|
1 | 1 | import { betterAuth } from "better-auth"; |
2 | 2 | import { drizzleAdapter } from "better-auth/adapters/drizzle"; |
3 | 3 | import { db } from "db"; // your drizzle instance |
4 | | -import { env } from "../env"; |
5 | | -import {APP_NAME, AUTH_CONFIG} from "shared/constants" |
| 4 | +import { APP_NAME, AUTH_CONFIG } from "shared/constants"; |
6 | 5 |
|
7 | 6 | export const auth = betterAuth({ |
8 | | - database: drizzleAdapter(db, { |
9 | | - provider: "sqlite", |
10 | | - debugLogs: true, |
11 | | - }), |
12 | | - databaseHooks: { |
13 | | - user: { |
14 | | - create: { |
15 | | - // used in order to break up the first and last name into separate fields |
16 | | - before: async (user) => { |
17 | | - // split the name into first and last name (name object is mapped to the first name by the config) |
18 | | - const [firstName, ...rest] = user.name.split(" "); |
19 | | - const lastName = rest.join(" "); |
20 | | - return { |
21 | | - data: { ...user, firstName, lastName }, |
22 | | - }; |
23 | | - }, |
24 | | - }, |
25 | | - }, |
26 | | - }, |
27 | | - user: { |
28 | | - // this maps the default "name" field to the "firstName" field in the database |
29 | | - fields: { |
30 | | - name: "firstName", |
31 | | - }, |
32 | | - // this declares the extra fields that are not in the default user schema that better auth creates, but are in the database |
33 | | - additionalFields: { |
34 | | - firstName: { |
35 | | - type: "string", |
36 | | - defaultValue: "", |
37 | | - }, |
38 | | - lastName: { |
39 | | - type: "string", |
40 | | - defaultValue: "", |
41 | | - }, |
42 | | - lastSeen: { |
43 | | - type: "date", |
44 | | - required: true, |
45 | | - defaultValue: Date.now(), |
46 | | - input: false, |
47 | | - }, |
48 | | - // role: { |
49 | | - // type: "string", |
50 | | - // defaultValue: "user", |
51 | | - // validator: { |
52 | | - // input: z.enum(["user", "admin"]), |
53 | | - // output: z.enum(["user", "admin"]), |
54 | | - // }, |
55 | | - // }, |
56 | | - }, |
57 | | - }, |
58 | | - advanced: { |
59 | | - cookiePrefix: APP_NAME, |
60 | | - }, |
61 | | - emailAndPassword: { |
62 | | - enabled: AUTH_CONFIG.emailAndPassword.enabled, |
63 | | - minPasswordLength: AUTH_CONFIG.emailAndPassword.minPasswordLength, |
64 | | - maxPasswordLength: AUTH_CONFIG.emailAndPassword.maxPasswordLength, |
65 | | - }, |
66 | | - // TODO: Reference the following link to see if it is easier to have the social provider's returned values map to first and last name instead |
67 | | - // https://www.better-auth.com/docs/concepts/database#extending-core-schema:~:text=Example%3A%20Mapping%20Profile%20to%20User%20For%20firstName%20and%20lastName |
68 | | - // socialProviders: { |
69 | | - // google: { |
70 | | - // clientId: env.GOOGLE_CLIENT_ID, |
71 | | - // clientSecret: env.GOOGLE_CLIENT_SECRET, |
72 | | - // }, |
73 | | - // discord: { |
74 | | - // clientId: env.DISCORD_CLIENT_ID, |
75 | | - // clientSecret: env.DISCORD_CLIENT_SECRET, |
76 | | - // }, |
77 | | - // github: { |
78 | | - // clientId: env.GITHUB_CLIENT_ID, |
79 | | - // clientSecret: env.GITHUB_CLIENT_SECRET, |
80 | | - // }, |
81 | | - // linear: { |
82 | | - // clientId: env.LINEAR_CLIENT_ID, |
83 | | - // clientSecret: env.LINEAR_CLIENT_SECRET, |
84 | | - // }, |
85 | | - // }, |
86 | | - rateLimit: { |
87 | | - window: 10, // time window in seconds |
88 | | - max: 100, // max requests in the window |
89 | | - }, |
90 | | - session: { |
91 | | - expiresIn: 60 * 60 * 24 * 7, // 7 days |
92 | | - updateAge: 60 * 60 * 24, // 1 day (every 1 day the session expiration is updated) |
93 | | - cookieCache: { |
94 | | - enabled: true, |
95 | | - maxAge: 5 * 60, |
96 | | - }, |
97 | | - }, |
| 7 | + database: drizzleAdapter(db, { |
| 8 | + provider: "sqlite", |
| 9 | + debugLogs: true, |
| 10 | + }), |
| 11 | + databaseHooks: { |
| 12 | + user: { |
| 13 | + create: { |
| 14 | + // used in order to break up the first and last name into separate fields |
| 15 | + before: async (user) => { |
| 16 | + // split the name into first and last name (name object is mapped to the first name by the config) |
| 17 | + const [firstName, ...rest] = user.name.split(" "); |
| 18 | + const lastName = rest.join(" "); |
| 19 | + return { |
| 20 | + data: { ...user, firstName, lastName }, |
| 21 | + }; |
| 22 | + }, |
| 23 | + }, |
| 24 | + }, |
| 25 | + }, |
| 26 | + user: { |
| 27 | + // this maps the default "name" field to the "firstName" field in the database |
| 28 | + fields: { |
| 29 | + name: "firstName", |
| 30 | + }, |
| 31 | + // this declares the extra fields that are not in the default user schema that better auth creates, but are in the database |
| 32 | + additionalFields: { |
| 33 | + firstName: { |
| 34 | + type: "string", |
| 35 | + defaultValue: "", |
| 36 | + }, |
| 37 | + lastName: { |
| 38 | + type: "string", |
| 39 | + defaultValue: "", |
| 40 | + }, |
| 41 | + lastSeen: { |
| 42 | + type: "date", |
| 43 | + required: true, |
| 44 | + defaultValue: Date.now(), |
| 45 | + input: false, |
| 46 | + }, |
| 47 | + // role: { |
| 48 | + // type: "string", |
| 49 | + // defaultValue: "user", |
| 50 | + // validator: { |
| 51 | + // input: z.enum(["user", "admin"]), |
| 52 | + // output: z.enum(["user", "admin"]), |
| 53 | + // }, |
| 54 | + // }, |
| 55 | + }, |
| 56 | + }, |
| 57 | + advanced: { |
| 58 | + cookiePrefix: APP_NAME, |
| 59 | + }, |
| 60 | + emailAndPassword: { |
| 61 | + enabled: AUTH_CONFIG.emailAndPassword.enabled, |
| 62 | + minPasswordLength: AUTH_CONFIG.emailAndPassword.minPasswordLength, |
| 63 | + maxPasswordLength: AUTH_CONFIG.emailAndPassword.maxPasswordLength, |
| 64 | + }, |
| 65 | + // TODO: Reference the following link to see if it is easier to have the social provider's returned values map to first and last name instead |
| 66 | + // https://www.better-auth.com/docs/concepts/database#extending-core-schema:~:text=Example%3A%20Mapping%20Profile%20to%20User%20For%20firstName%20and%20lastName |
| 67 | + // socialProviders: { |
| 68 | + // google: { |
| 69 | + // clientId: env.GOOGLE_CLIENT_ID, |
| 70 | + // clientSecret: env.GOOGLE_CLIENT_SECRET, |
| 71 | + // }, |
| 72 | + // discord: { |
| 73 | + // clientId: env.DISCORD_CLIENT_ID, |
| 74 | + // clientSecret: env.DISCORD_CLIENT_SECRET, |
| 75 | + // }, |
| 76 | + // github: { |
| 77 | + // clientId: env.GITHUB_CLIENT_ID, |
| 78 | + // clientSecret: env.GITHUB_CLIENT_SECRET, |
| 79 | + // }, |
| 80 | + // linear: { |
| 81 | + // clientId: env.LINEAR_CLIENT_ID, |
| 82 | + // clientSecret: env.LINEAR_CLIENT_SECRET, |
| 83 | + // }, |
| 84 | + // }, |
| 85 | + rateLimit: { |
| 86 | + window: 10, // time window in seconds |
| 87 | + max: 100, // max requests in the window |
| 88 | + }, |
| 89 | + session: { |
| 90 | + expiresIn: 60 * 60 * 24 * 7, // 7 days |
| 91 | + updateAge: 60 * 60 * 24, // 1 day (every 1 day the session expiration is updated) |
| 92 | + cookieCache: { |
| 93 | + enabled: true, |
| 94 | + maxAge: 5 * 60, |
| 95 | + }, |
| 96 | + }, |
98 | 97 | }); |
0 commit comments