| 1 | import { type Database } from '@ghost/db'; |
| 2 | import { betterAuth } |
| 3 | import { drizzleAdapter } |
| 4 | import { organization, username |
| 5 | import { apiKey } |
| 6 | |
| 7 | import { ac, roles |
| 8 | |
| 9 | export type AuthConfig |
| 10 | secret: string |
| 11 | baseURL: string |
| 12 | trustedOrigins?: string |
| 13 | /** |
| 14 | * Registrable domain to pin session cookies to, e.g. `.example.com`, when the |
| 15 | * web app and the API sit on sibling subdomains. Unset in local development, |
| 16 | * where both share `localhost`. |
| 17 | */ |
| 18 | cookieDomain?: string |
| 19 | }; |
| 20 | |
| 21 | export function createAuth(db |
| 22 | return betterAuth({ |
| 23 | secret: config.secret |
| 24 | baseURL: config.baseURL |
| 25 | trustedOrigins: config.trustedOrigins |
| 26 | advanced: config.cookieDomain |
| 27 | ? { |
| 28 | crossSubDomainCookies: { |
| 29 | enabled: true, |
| 30 | domain: config.cookieDomain |
| 31 | }, |
| 32 | } |
| 33 | : undefined, |
| 34 | database: drizzleAdapter(db |
| 35 | provider: 'pg', |
| 36 | }), |
| 37 | emailAndPassword: { |
| 38 | enabled: true, |
| 39 | sendResetPassword: async ( |
| 40 | console.log(data |
| 41 | }, |
| 42 | }, |
| 43 | plugins: [ |
| 44 | organization({ |
| 45 | ac, |
| 46 | roles, |
| 47 | creatorRole: 'owner', |
| 48 | defaultRole: 'read', |
| 49 | dynamicAccessControl: { |
| 50 | enabled: true, |
| 51 | }, |
| 52 | teams: { |
| 53 | enabled: true, |
| 54 | allowRemovingAllTeams: true, |
| 55 | }, |
| 56 | }), |
| 57 | username({}), |
| 58 | apiKey({ |
| 59 | defaultPrefix: 'ghost_pat_', |
| 60 | rateLimit: { |
| 61 | enabled: true, |
| 62 | maxRequests: 120, |
| 63 | timeWindow: 60000, // |
| 64 | }, |
| 65 | }), |
| 66 | ], |
| 67 | }); |
| 68 | } |
| 69 | |
| 70 | export type Auth |