Skip to content

Commit cef5117

Browse files
feat(cli): improve convex + better-auth frontend templates (#746)
* feat(cli): improve convex + better-auth frontend templates * docs: update CONTRIBUTING.md to recommend correct lint/format command * fix --------- Co-authored-by: Aman Varshney <amanvarshney.work@gmail.com>
1 parent dcbbbda commit cef5117

File tree

11 files changed

+54
-67
lines changed

11 files changed

+54
-67
lines changed

.github/CONTRIBUTING.md

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -157,8 +157,8 @@ cd apps/cli
157157
bun dev
158158
bun run test
159159

160-
# Format files
161-
bun run format
160+
# Lint and format files (from root, uses oxlint and oxfmt)
161+
bun run check
162162
```
163163

164164
- **Manual testing**: Test your changes manually to ensure everything works as expected
@@ -174,7 +174,6 @@ Use conventional commit messages with the appropriate scope:
174174
- `feat(web): add new web feature`
175175
- `fix(web): fix web bug`
176176
- `chore(web): update dependencies`
177-
- `docs: update documentation`
178177

179178
## Getting Help
180179

apps/cli/src/constants.ts

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -42,8 +42,8 @@ export const DEFAULT_CONFIG = getDefaultConfig();
4242
export const dependencyVersionMap = {
4343
typescript: "^5",
4444

45-
"better-auth": "^1.4.7",
46-
"@better-auth/expo": "^1.4.7",
45+
"better-auth": "^1.4.9",
46+
"@better-auth/expo": "^1.4.9",
4747

4848
"@clerk/nextjs": "^6.31.5",
4949
"@clerk/clerk-react": "^5.45.0",
@@ -141,7 +141,7 @@ export const dependencyVersionMap = {
141141
"convex-svelte": "^0.0.12",
142142
"convex-nuxt": "0.1.5",
143143
"convex-vue": "^0.1.5",
144-
"@convex-dev/better-auth": "^0.10.6",
144+
"@convex-dev/better-auth": "^0.10.9",
145145

146146
"@tanstack/svelte-query": "^5.85.3",
147147
"@tanstack/svelte-query-devtools": "^5.85.3",
@@ -151,6 +151,7 @@ export const dependencyVersionMap = {
151151

152152
"@tanstack/react-query-devtools": "^5.91.1",
153153
"@tanstack/react-query": "^5.90.12",
154+
"@tanstack/react-router-ssr-query": "^1.142.7",
154155

155156
"@tanstack/solid-query": "^5.87.4",
156157
"@tanstack/solid-query-devtools": "^5.87.4",

apps/cli/src/helpers/core/api-setup.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -154,6 +154,7 @@ function getConvexDependencies(frontend: Frontend[]) {
154154

155155
if (frontend.includes("tanstack-start")) {
156156
deps.web.dependencies.push("@convex-dev/react-query");
157+
deps.web.dependencies.push("@tanstack/react-router-ssr-query");
157158
}
158159
if (frontend.includes("svelte")) {
159160
deps.web.dependencies.push("convex-svelte");

apps/cli/src/helpers/core/auth-setup.ts

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -59,13 +59,13 @@ export async function setupAuth(config: ProjectConfig) {
5959
if (convexBackendDirExists) {
6060
await addPackageDependency({
6161
dependencies: ["better-auth", "@convex-dev/better-auth"],
62-
customDependencies: { "better-auth": "1.4.7" },
62+
customDependencies: { "better-auth": "1.4.9" },
6363
projectDir: convexBackendDir,
6464
});
6565
if (hasNativeForBA) {
6666
await addPackageDependency({
6767
dependencies: ["@better-auth/expo"],
68-
customDependencies: { "@better-auth/expo": "1.4.7" },
68+
customDependencies: { "@better-auth/expo": "1.4.9" },
6969
projectDir: convexBackendDir,
7070
});
7171
}
@@ -81,19 +81,19 @@ export async function setupAuth(config: ProjectConfig) {
8181
if (hasNextJs) {
8282
await addPackageDependency({
8383
dependencies: ["better-auth", "@convex-dev/better-auth"],
84-
customDependencies: { "better-auth": "1.4.7" },
84+
customDependencies: { "better-auth": "1.4.9" },
8585
projectDir: clientDir,
8686
});
8787
} else if (hasTanStackStart) {
8888
await addPackageDependency({
8989
dependencies: ["better-auth", "@convex-dev/better-auth"],
90-
customDependencies: { "better-auth": "1.4.7" },
90+
customDependencies: { "better-auth": "1.4.9" },
9191
projectDir: clientDir,
9292
});
9393
} else if (hasViteReactOther) {
9494
await addPackageDependency({
9595
dependencies: ["better-auth", "@convex-dev/better-auth"],
96-
customDependencies: { "better-auth": "1.4.7" },
96+
customDependencies: { "better-auth": "1.4.9" },
9797
projectDir: clientDir,
9898
});
9999
}
@@ -106,8 +106,8 @@ export async function setupAuth(config: ProjectConfig) {
106106
await addPackageDependency({
107107
dependencies: ["better-auth", "@better-auth/expo", "@convex-dev/better-auth"],
108108
customDependencies: {
109-
"better-auth": "1.4.7",
110-
"@better-auth/expo": "1.4.7",
109+
"better-auth": "1.4.9",
110+
"@better-auth/expo": "1.4.9",
111111
},
112112
projectDir: nativeDir,
113113
});

apps/cli/templates/auth/better-auth/convex/backend/convex/auth.ts.hbs

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,9 @@ import { convex, crossDomain } from "@convex-dev/better-auth/plugins";
88
import { convex } from "@convex-dev/better-auth/plugins";
99
{{/if}}
1010
import { components } from "./_generated/api";
11-
import { DataModel } from "./_generated/dataModel";
11+
import type { DataModel } from "./_generated/dataModel";
1212
import { query } from "./_generated/server";
1313
import { betterAuth } from "better-auth";
14-
import { v } from "convex/values";
1514
import authConfig from "./auth.config";
1615

1716
{{#if (or (includes frontend "tanstack-start") (includes frontend "next"))}}
@@ -63,8 +62,7 @@ export { createAuth };
6362

6463
export const getCurrentUser = query({
6564
args: {},
66-
returns: v.any(),
67-
handler: async function (ctx) {
68-
return authComponent.getAuthUser(ctx);
65+
handler: async (ctx) => {
66+
return await authComponent.safeGetAuthUser(ctx);
6967
},
7068
});
Lines changed: 13 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,17 @@
11
import { query } from "./_generated/server";
2+
import { authComponent } from "./auth";
23

34
export const get = query({
4-
args: {},
5-
handler: async (ctx) => {
6-
const identity = await ctx.auth.getUserIdentity();
7-
if (identity === null) {
8-
return {
9-
message: "Not authenticated",
10-
};
11-
}
12-
return {
13-
message: "This is private",
14-
};
15-
},
5+
args: {},
6+
handler: async (ctx) => {
7+
const authUser = await authComponent.safeGetAuthUser(ctx);
8+
if (!authUser) {
9+
return {
10+
message: "Not authenticated",
11+
};
12+
}
13+
return {
14+
message: "This is private",
15+
};
16+
},
1617
});

apps/cli/templates/auth/better-auth/convex/web/react/next/src/lib/auth-client.ts.hbs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,5 +2,5 @@ import { createAuthClient } from "better-auth/react";
22
import { convexClient } from "@convex-dev/better-auth/client/plugins";
33

44
export const authClient = createAuthClient({
5-
plugins: [convexClient()],
6-
});
5+
plugins: [convexClient()],
6+
});

apps/cli/templates/auth/better-auth/convex/web/react/next/src/lib/auth-server.ts.hbs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import { convexBetterAuthNextJs } from "@convex-dev/better-auth/nextjs";
2+
import { isAuthError } from "@/lib/utils";
23

34
export const {
45
handler,

apps/cli/templates/deploy/alchemy/alchemy.run.ts.hbs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ export const web = await Nextjs("web", {
7272
{{#if (eq auth "clerk")}}
7373
CLERK_SECRET_KEY: alchemy.secret.env.CLERK_SECRET_KEY,
7474
{{/if}}
75-
{{#if (includes examples "ai")}}
75+
{{#if (and (includes examples "ai") (ne backend "convex"))}}
7676
GOOGLE_GENERATIVE_AI_API_KEY: alchemy.secret.env.GOOGLE_GENERATIVE_AI_API_KEY,
7777
{{/if}}
7878
{{#if (eq payments "polar")}}
@@ -146,7 +146,7 @@ export const web = await TanStackStart("web", {
146146
{{#if (eq auth "clerk")}}
147147
CLERK_SECRET_KEY: alchemy.secret.env.CLERK_SECRET_KEY,
148148
{{/if}}
149-
{{#if (includes examples "ai")}}
149+
{{#if (and (includes examples "ai") (ne backend "convex"))}}
150150
GOOGLE_GENERATIVE_AI_API_KEY: alchemy.secret.env.GOOGLE_GENERATIVE_AI_API_KEY,
151151
{{/if}}
152152
{{#if (eq payments "polar")}}

apps/cli/templates/frontend/react/tanstack-start/src/router.tsx.hbs

Lines changed: 17 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,8 @@
11
{{#if (eq backend "convex")}}
22
import { createRouter as createTanStackRouter } from "@tanstack/react-router";
33
import { QueryClient } from "@tanstack/react-query";
4-
import { routerWithQueryClient } from "@tanstack/react-router-with-query";
4+
import { setupRouterSsrQueryIntegration } from "@tanstack/react-router-ssr-query";
55
import { ConvexQueryClient } from "@convex-dev/react-query";
6-
import { ConvexProvider, ConvexReactClient } from "convex/react";
76
import { routeTree } from "./routeTree.gen";
87
import Loader from "./components/loader";
98
import "./index.css";
@@ -27,21 +26,12 @@ import { orpc, queryClient } from "./utils/orpc";
2726

2827
{{#if (eq backend "convex")}}
2928
export function getRouter() {
30-
const CONVEX_URL = (import.meta as any).env.VITE_CONVEX_URL!;
31-
if (!CONVEX_URL) {
32-
console.error("missing envar VITE_CONVEX_URL");
29+
const convexUrl = (import.meta as any).env.VITE_CONVEX_URL!;
30+
if (!convexUrl) {
31+
throw new Error("VITE_CONVEX_URL is not set");
3332
}
34-
const convex = new ConvexReactClient(CONVEX_URL, {
35-
unsavedChangesWarning: false,
36-
});
3733

38-
{{#if (eq auth "better-auth")}}
39-
const convexQueryClient = new ConvexQueryClient(convex, {
40-
expectAuth: true,
41-
});
42-
{{else}}
43-
const convexQueryClient = new ConvexQueryClient(convex);
44-
{{/if}}
34+
const convexQueryClient = new ConvexQueryClient(convexUrl);
4535

4636
const queryClient: QueryClient = new QueryClient({
4737
defaultOptions: {
@@ -53,21 +43,19 @@ export function getRouter() {
5343
});
5444
convexQueryClient.connect(queryClient);
5545

56-
const router = routerWithQueryClient(
57-
createTanStackRouter({
58-
routeTree,
59-
defaultPreload: "intent",
60-
defaultPendingComponent: () => <Loader />,
61-
defaultNotFoundComponent: () => <div>Not Found</div>,
62-
context: { queryClient, convexClient: convex, convexQueryClient },
63-
Wrap: ({ children }) => (
64-
<ConvexProvider client={convexQueryClient.convexClient}>
65-
{children}
66-
</ConvexProvider>
67-
),
68-
}),
46+
const router = createTanStackRouter({
47+
routeTree,
48+
defaultPreload: "intent",
49+
defaultPendingComponent: () => <Loader />,
50+
defaultNotFoundComponent: () => <div>Not Found</div>,
51+
context: { queryClient, convexQueryClient },
52+
});
53+
54+
setupRouterSsrQueryIntegration({
55+
router,
6956
queryClient,
70-
);
57+
});
58+
7159
return router;
7260
}
7361
{{else}}

0 commit comments

Comments
 (0)