Skip to content

Commit 626846e

Browse files
Template suggestions (#6)
Co-authored-by: Nick Nisi <nick.nisi@workos.com>
1 parent 9da4c50 commit 626846e

File tree

6 files changed

+20
-20
lines changed

6 files changed

+20
-20
lines changed

.env.local.example

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,3 @@ WORKOS_CLIENT_ID=client_your_client_id_here
33
WORKOS_API_KEY=sk_test_your_api_key_here
44
WORKOS_COOKIE_PASSWORD=your_secure_password_here_must_be_at_least_32_characters_long
55
NEXT_PUBLIC_WORKOS_REDIRECT_URI=http://localhost:3000/callback
6-
7-
# Convex Configuration
8-
NEXT_PUBLIC_CONVEX_URL=https://your-convex-url.convex.cloud
9-
CONVEX_DEPLOY_KEY=your_convex_deploy_key_here

app/page.tsx

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -50,11 +50,7 @@ function Content() {
5050
const addNumber = useMutation(api.myFunctions.addNumber);
5151

5252
if (viewer === undefined || numbers === undefined) {
53-
return (
54-
<div className="mx-auto">
55-
<p>loading... (consider a loading skeleton)</p>
56-
</div>
57-
);
53+
return <div className="mx-auto"></div>;
5854
}
5955

6056
return (

app/server/inner.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ export default function Home({ preloaded }: { preloaded: Preloaded<typeof api.my
99
return (
1010
<>
1111
<div className="flex flex-col gap-4 bg-slate-200 dark:bg-slate-800 p-4 rounded-md">
12-
<h2 className="text-xl font-bold">Reactive client-loaded data</h2>
12+
<h2 className="text-xl font-bold">Reactive client-loaded data (using server data during hydration)</h2>
1313
<code>
1414
<pre>{JSON.stringify(data, null, 2)}</pre>
1515
</code>

app/server/page.tsx

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,17 @@
11
import Home from './inner';
22
import { preloadQuery, preloadedQueryResult } from 'convex/nextjs';
33
import { api } from '@/convex/_generated/api';
4+
import { withAuth } from '@workos-inc/authkit-nextjs';
45

56
export default async function ServerPage() {
6-
const preloaded = await preloadQuery(api.myFunctions.listNumbers, {
7-
count: 3,
8-
});
7+
const { accessToken } = await withAuth();
8+
const preloaded = await preloadQuery(
9+
api.myFunctions.listNumbers,
10+
{
11+
count: 3,
12+
},
13+
{ token: accessToken },
14+
);
915

1016
const data = preloadedQueryResult(preloaded);
1117

components/ConvexClientProvider.tsx

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,14 @@
11
'use client';
22

3-
import { ReactNode, useCallback, useRef } from 'react';
3+
import { ReactNode, useCallback, useRef, useState } from 'react';
44
import { ConvexReactClient } from 'convex/react';
55
import { ConvexProviderWithAuth } from 'convex/react';
66
import { AuthKitProvider, useAuth, useAccessToken } from '@workos-inc/authkit-nextjs/components';
77

8-
const convex = new ConvexReactClient(process.env.NEXT_PUBLIC_CONVEX_URL!);
9-
108
export function ConvexClientProvider({ children }: { children: ReactNode }) {
9+
const [convex] = useState(() => {
10+
return new ConvexReactClient(process.env.NEXT_PUBLIC_CONVEX_URL!);
11+
});
1112
return (
1213
<AuthKitProvider>
1314
<ConvexProviderWithAuth client={convex} useAuth={useAuthFromAuthKit}>
@@ -20,8 +21,9 @@ export function ConvexClientProvider({ children }: { children: ReactNode }) {
2021
function useAuthFromAuthKit() {
2122
const { user, loading: isLoading } = useAuth();
2223
const { accessToken, loading: tokenLoading, error: tokenError } = useAccessToken();
23-
const loading = (isLoading ?? false) || (tokenLoading ?? false) || !accessToken;
24-
const authenticated = !!user && !!accessToken && !loading;
24+
const hasIncompleteAuth = (!!user && !accessToken) || (!user && !!accessToken);
25+
const loading = (isLoading ?? false) || (tokenLoading ?? false) || hasIncompleteAuth;
26+
const authenticated = !!user && !!accessToken;
2527

2628
// Memoize the token to prevent unnecessary changes
2729
const stableAccessToken = useRef<string | null>(null);

package.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,8 @@
2222
"format": "prettier --write ."
2323
},
2424
"dependencies": {
25-
"@workos-inc/authkit-nextjs": "^2.4.4",
26-
"convex": "^1.25.4",
25+
"@workos-inc/authkit-nextjs": "^2.6.0",
26+
"convex": "^1.26.2",
2727
"next": "15.4.3",
2828
"react": "^19.0.0",
2929
"react-dom": "^19.0.0"

0 commit comments

Comments
 (0)