Skip to content

Commit 9f4fb60

Browse files
committed
migrate to @convex-dev/better-auth 0.8
1 parent 9ca92c0 commit 9f4fb60

8 files changed

Lines changed: 3426 additions & 311 deletions

File tree

convex/_generated/api.d.ts

Lines changed: 3326 additions & 160 deletions
Large diffs are not rendered by default.

convex/auth.ts

Lines changed: 68 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -1,64 +1,84 @@
1-
import {
2-
BetterAuth,
3-
type AuthFunctions,
4-
type PublicAuthFunctions,
5-
} from '@convex-dev/better-auth'
6-
import { api, components, internal } from './_generated/api'
1+
import { components, internal } from './_generated/api'
72
import { query, QueryCtx } from './_generated/server'
83
import type { Id, DataModel } from './_generated/dataModel'
94
import schema from './schema'
10-
11-
import { Infer } from 'convex/values'
5+
import type { Infer } from 'convex/values'
6+
import { betterAuth } from 'better-auth'
7+
import {
8+
type AuthFunctions,
9+
type GenericCtx,
10+
createClient,
11+
} from '@convex-dev/better-auth'
12+
import { convex } from '@convex-dev/better-auth/plugins'
1213

1314
// Typesafe way to pass Convex functions defined in this file
1415
const authFunctions: AuthFunctions = internal.auth
15-
const publicAuthFunctions: PublicAuthFunctions = api.auth
1616

1717
// Initialize the component
18-
export const betterAuthComponent = new BetterAuth(components.betterAuth, {
18+
export const authComponent = createClient<DataModel>(components.betterAuth, {
1919
authFunctions,
20-
publicAuthFunctions,
20+
triggers: {
21+
user: {
22+
onCreate: async (ctx, authUser) => {
23+
const userId = await ctx.db.insert('users', {
24+
createdAt: authUser.createdAt,
25+
email: authUser.email,
26+
updatedAt: authUser.updatedAt,
27+
displayUsername: authUser.displayUsername ?? '',
28+
image: authUser.image ?? '',
29+
name: authUser.name,
30+
capabilities: [],
31+
})
32+
await authComponent.setUserId(ctx, authUser._id, userId)
33+
},
34+
onUpdate: async (ctx, _, authUser) => {
35+
await ctx.db.patch(authUser.userId as Id<'users'>, {
36+
email: authUser.email,
37+
updatedAt: authUser.updatedAt,
38+
displayUsername: authUser.displayUsername ?? '',
39+
image: authUser.image ?? '',
40+
name: authUser.name,
41+
})
42+
},
43+
onDelete: async (ctx, authUser) => {
44+
await ctx.db.delete(authUser.userId as Id<'users'>)
45+
},
46+
},
47+
},
2148
})
2249

23-
// These are required named exports
24-
export const {
25-
createUser,
26-
updateUser,
27-
deleteUser,
28-
createSession,
29-
isAuthenticated,
30-
} = betterAuthComponent.createAuthFunctions<DataModel>({
31-
// Must create a user and return the user id
32-
onCreateUser: async (ctx, user) => {
33-
return ctx.db.insert('users', {
34-
createdAt: user.createdAt,
35-
email: user.email,
36-
updatedAt: user.updatedAt,
37-
displayUsername: user.displayUsername ?? '',
38-
image: user.image ?? '',
39-
name: user.name,
40-
capabilities: [],
41-
})
42-
},
50+
export const { onCreate, onUpdate, onDelete } = authComponent.triggersApi()
4351

44-
// Delete the user when they are deleted from Better Auth
45-
onDeleteUser: async (ctx, userId) => {
46-
await ctx.db.delete(userId as Id<'users'>)
47-
},
52+
export const createAuth = (
53+
ctx: GenericCtx<DataModel>,
54+
{ optionsOnly } = { optionsOnly: false }
55+
) =>
56+
betterAuth({
57+
// All auth requests will be proxied through your TanStack Start server
58+
baseURL: process.env.URL,
59+
database: authComponent.adapter(ctx),
4860

49-
onUpdateUser: async (ctx, user) => {
50-
await ctx.db.patch(user.userId as Id<'users'>, {
51-
email: user.email,
52-
updatedAt: user.updatedAt,
53-
displayUsername: user.displayUsername ?? '',
54-
image: user.image ?? '',
55-
name: user.name,
56-
})
57-
},
58-
})
61+
logger: {
62+
disabled: optionsOnly,
63+
},
64+
65+
// Simple non-verified email/password to get started
66+
socialProviders: {
67+
github: {
68+
clientId: process.env.GITHUB_OAUTH_CLIENT_ID as string,
69+
clientSecret: process.env.GITHUB_OAUTH_CLIENT_SECRET as string,
70+
},
71+
google: {
72+
clientId: process.env.GOOGLE_OAUTH_CLIENT_ID as string,
73+
clientSecret: process.env.GOOGLE_OAUTH_CLIENT_SECRET as string,
74+
},
75+
},
76+
plugins: [
77+
// The Convex plugin is required
78+
convex(),
79+
],
80+
})
5981

60-
// Example function for getting the current user
61-
// Feel free to edit, omit, etc.
6282
export const getCurrentUser = query({
6383
args: {},
6484
handler: async (ctx) => {
@@ -86,5 +106,5 @@ export type TanStackUser = Awaited<ReturnType<typeof getBetterAuthUser>> &
86106
Infer<typeof schema.tables.users.validator>
87107

88108
function getBetterAuthUser(ctx: QueryCtx) {
89-
return betterAuthComponent.getAuthUser(ctx)
109+
return authComponent.safeGetAuthUser(ctx)
90110
}

convex/http.ts

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,10 @@
11
import { ossStats } from './stats'
22
import { httpRouter } from 'convex/server'
3-
import { betterAuthComponent } from './auth'
4-
import { createAuth } from '../src/server/auth.server'
3+
import { authComponent, createAuth } from './auth'
54

65
const http = httpRouter()
76

87
ossStats.registerRoutes(http)
9-
betterAuthComponent.registerRoutes(http, createAuth)
8+
authComponent.registerRoutes(http, createAuth)
109

1110
export default http

convex/schema.ts

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
import { defineSchema, defineTable } from 'convex/server'
2-
import { authTables } from '@convex-dev/auth/server'
32
import { v } from 'convex/values'
43
import { z } from 'zod'
54

@@ -20,7 +19,6 @@ export function validateCapability(
2019
const validCapabilities = VALID_CAPABILITIES
2120

2221
const schema = defineSchema({
23-
...authTables,
2422
users: defineTable({
2523
displayUsername: v.optional(v.string()),
2624
createdAt: v.number(),

package.json

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,7 @@
1919
"dependencies": {
2020
"@auth/core": "0.37.0",
2121
"@clerk/tanstack-react-start": "^0.21.5",
22-
"@convex-dev/auth": "^0.0.88",
23-
"@convex-dev/better-auth": "^0.7.15",
22+
"@convex-dev/better-auth": "^0.8.3",
2423
"@convex-dev/react-query": "0.0.0-alpha.11",
2524
"@erquhart/convex-oss-stats": "^0.8.1",
2625
"@floating-ui/react": "^0.27.8",
@@ -31,8 +30,8 @@
3130
"@octokit/rest": "^20.0.2",
3231
"@orama/react-components": "^0.1.23",
3332
"@radix-ui/react-dropdown-menu": "^2.1.12",
34-
"@radix-ui/react-toast": "^1.2.2",
3533
"@radix-ui/react-select": "^2.2.2",
34+
"@radix-ui/react-toast": "^1.2.2",
3635
"@remix-run/node": "^2.8.1",
3736
"@sentry/react": "^8.35.0",
3837
"@sentry/vite-plugin": "^2.22.6",
@@ -54,7 +53,7 @@
5453
"@vitejs/plugin-react": "^4.3.3",
5554
"algoliasearch": "^5.23.4",
5655
"axios": "^1.6.7",
57-
"better-auth": "^1.3.7",
56+
"better-auth": "^1.3.8",
5857
"cmdk": "^1.1.1",
5958
"convex": "^1.25.4",
6059
"convex-oss-stats": "link:../../../erquhart/convex-oss-stats",

pnpm-lock.yaml

Lines changed: 26 additions & 52 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/routes/api/auth/$.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { reactStartHandler } from "~/server/auth.server"
1+
import { reactStartHandler } from '@convex-dev/better-auth/react-start'
22

33
export const ServerRoute = createServerFileRoute().methods({
44
GET: ({ request }) => {

0 commit comments

Comments
 (0)