-
Notifications
You must be signed in to change notification settings - Fork 0
feat(auth): replace Supabase Auth with self-hosted Better Auth #24
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,79 @@ | ||
| /** | ||
| * Better Auth server configuration — the single source of identity for the app. | ||
| * | ||
| * Replaces Supabase Auth. Sessions and accounts live in the same Neon database | ||
| * as application data, so there is no external identity provider that can pause | ||
| * or change terms underneath us. | ||
| */ | ||
|
|
||
| import { betterAuth } from 'better-auth' | ||
| import { prismaAdapter } from 'better-auth/adapters/prisma' | ||
| import { env } from './env.js' | ||
| import { prisma } from './prisma.js' | ||
| import { sendVerificationEmail, sendPasswordResetEmail } from '../services/emailService.js' | ||
|
|
||
| export const auth = betterAuth({ | ||
| baseURL: env.BETTER_AUTH_URL, | ||
| secret: env.BETTER_AUTH_SECRET, | ||
| database: prismaAdapter(prisma, { provider: 'postgresql' }), | ||
| trustedOrigins: [env.FRONTEND_URL], | ||
|
|
||
| emailAndPassword: { | ||
| enabled: true, | ||
| // Matches the retired Supabase `enable_confirmations = true`: a new account | ||
| // cannot sign in until its address is verified. | ||
| requireEmailVerification: true, | ||
| autoSignIn: false, | ||
| sendResetPassword: async ({ user, url }) => { | ||
| await sendPasswordResetEmail(user.email, url) | ||
| } | ||
| }, | ||
|
|
||
| emailVerification: { | ||
| sendOnSignUp: true, | ||
| sendOnSignIn: true, | ||
| sendVerificationEmail: async ({ user, url }) => { | ||
| await sendVerificationEmail(user.email, url) | ||
| } | ||
| }, | ||
|
|
||
| socialProviders: { | ||
| google: { | ||
| clientId: env.GOOGLE_OAUTH_CLIENT_ID, | ||
| clientSecret: env.GOOGLE_OAUTH_SECRET | ||
| } | ||
| }, | ||
|
|
||
| account: { | ||
| // Google access/refresh tokens are stored in the `account` table; encrypt | ||
| // them at rest rather than accepting the plaintext default. | ||
| encryptOAuthTokens: true, | ||
| accountLinking: { | ||
| // Mirrors the retired `enable_manual_linking = false`: signing in with | ||
| // Google using an address that already has a password account lands on | ||
| // that same account instead of creating a duplicate. | ||
| enabled: true, | ||
| trustedProviders: ['google'] | ||
| } | ||
| }, | ||
|
|
||
| user: { | ||
| additionalFields: { | ||
| // Subscription tier drives daily project quota. Server-owned: `input: false` | ||
| // stops a client from promoting itself by posting a tier on signup. | ||
| tier: { | ||
| type: 'string', | ||
| required: false, | ||
| defaultValue: 'FREE', | ||
| input: false | ||
| }, | ||
| // UI language, persisted server-side so the choice follows the user | ||
| // across devices. Client-writable, unlike tier. | ||
| locale: { | ||
| type: 'string', | ||
| required: false, | ||
| input: true | ||
| } | ||
| } | ||
| } | ||
| }) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,27 @@ | ||
| /** | ||
| * Transactional email dispatch. | ||
| * | ||
| * Placeholder implementation: issue #7 replaces these bodies with direct Resend | ||
| * calls and the branded templates ported out of `supabase/templates/`. Until | ||
| * then the links are logged so local sign-up flows remain completable. | ||
| */ | ||
|
|
||
| /** | ||
| * Sends an address-verification link to a newly registered user. | ||
| * | ||
| * @param email - Recipient address | ||
| * @param url - Better Auth verification link | ||
| */ | ||
| export async function sendVerificationEmail(email: string, url: string): Promise<void> { | ||
| console.info(`[Email] verification for ${email}: ${url}`) | ||
| } | ||
|
|
||
| /** | ||
| * Sends a password-reset link. | ||
| * | ||
| * @param email - Recipient address | ||
| * @param url - Better Auth password-reset link | ||
| */ | ||
| export async function sendPasswordResetEmail(email: string, url: string): Promise<void> { | ||
| console.info(`[Email] password reset for ${email}: ${url}`) | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🔒 Security & Privacy | 🔴 Critical | 🏗️ Heavy lift
Replace token logging with real email delivery before enabling this flow.
requireEmailVerificationmakes new accounts unusable without delivery, while logging raw reset URLs exposes bearer credentials to anyone with log access and enables account takeover. Send these links through the email provider and never log the URL or recipient email in production.🧰 Tools
🪛 ast-grep (0.45.0)
[warning] 15-15: Avoid logging sensitive data
Context: console.info(
[Email] verification for ${email}: ${url})Note: [CWE-532] Insertion of Sensitive Information into Log File.
(log-sensitive-data-typescript)
[warning] 25-25: Avoid logging sensitive data
Context: console.info(
[Email] password reset for ${email}: ${url})Note: [CWE-532] Insertion of Sensitive Information into Log File.
(log-sensitive-data-typescript)
🤖 Prompt for AI Agents
Source: Linters/SAST tools