diff --git a/CHANGELOG.md b/CHANGELOG.md index 9f8f292..2582eca 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,7 +2,8 @@ ## Unreleased -- BREAKING: `convexAuthNextjsToken()` is now async and must be `await`ed. +- BREAKING: `convexAuthNextjsToken()` and `isAuthenticatedNextjs()` now return + promises so must be `await`ed. - Support for Next.js 15. - Update convex peer dependency to ^1.17. diff --git a/docs/pages/authz/nextjs.mdx b/docs/pages/authz/nextjs.mdx index 0ec902a..3e84a6b 100644 --- a/docs/pages/authz/nextjs.mdx +++ b/docs/pages/authz/nextjs.mdx @@ -1,12 +1,10 @@ import { Callout } from "nextra/components"; -# Server-side authentication in Next.js 14 +# Server-side authentication in Next.js You can set up your Next.js App Router app to have access to the authentication state on the server. -Next.js 15 is not supported yet. - ## Setup Make sure your React providers and middleware are @@ -28,10 +26,10 @@ const isSignInPage = createRouteMatcher(["/signin"]); const isProtectedRoute = createRouteMatcher(["/product(.*)"]); export default convexAuthNextjsMiddleware((request, { convexAuth }) => { - if (isSignInPage(request) && convexAuth.isAuthenticated()) { + if (isSignInPage(request) && (await convexAuth.isAuthenticated())) { return nextjsMiddlewareRedirect(request, "/product"); } - if (isProtectedRoute(request) && !convexAuth.isAuthenticated()) { + if (isProtectedRoute(request) && !(await convexAuth.isAuthenticated())) { return nextjsMiddlewareRedirect(request, "/signin"); } }); diff --git a/docs/pages/index.mdx b/docs/pages/index.mdx index 40bbab4..0bbf584 100644 --- a/docs/pages/index.mdx +++ b/docs/pages/index.mdx @@ -6,7 +6,7 @@ without needing an authentication service or even a hosting server. Your application can be: - a React SPA served from a CDN -- a full-stack Next.js 14 app +- a full-stack Next.js app - a React Native mobile app **NOTE:** Convex Auth is in beta. Please share any feedback you have on diff --git a/docs/pages/setup.mdx b/docs/pages/setup.mdx index 18c0842..a0c31d0 100644 --- a/docs/pages/setup.mdx +++ b/docs/pages/setup.mdx @@ -18,7 +18,7 @@ and choose `React (Vite)` and then `Convex Auth`. -**NOTE:** Convex Auth support for Next.js with server-side authentication (SSA) is experimental. Only Next.js 14 is currently supported, Next.js 15 is coming soon. +**NOTE:** Convex Auth support for Next.js with server-side authentication (SSA) is experimental. To start a new project from scratch with Convex and Convex Auth, run diff --git a/src/nextjs/server/index.tsx b/src/nextjs/server/index.tsx index 1ac8e85..23adf3e 100644 --- a/src/nextjs/server/index.tsx +++ b/src/nextjs/server/index.tsx @@ -91,8 +91,8 @@ export async function convexAuthNextjsToken() { * Avoid the pitfall of checking authentication state in layouts, * since they won't stop nested pages from rendering. */ -export function isAuthenticatedNextjs() { - return convexAuthNextjsToken() !== undefined; +export async function isAuthenticatedNextjs() { + return (await convexAuthNextjsToken()) !== undefined; } /** @@ -103,7 +103,7 @@ export function isAuthenticatedNextjs() { * ```ts * export function convexAuthNextjsMiddleware(handler, options) { * return async (request, event, convexAuth) => { - * if (!convexAuth.isAuthenticated()) { + * if (!(await convexAuth.isAuthenticated())) { * return nextjsMiddlewareRedirect(request, "/login"); * } * }; @@ -247,7 +247,11 @@ export function convexAuthNextjsMiddleware( authResult.refreshTokens !== undefined ) { const nextResponse = NextResponse.next(response); - await setAuthCookies(nextResponse, authResult.refreshTokens, cookieConfig); + await setAuthCookies( + nextResponse, + authResult.refreshTokens, + cookieConfig, + ); return nextResponse; } @@ -280,7 +284,7 @@ export function nextjsMiddlewareRedirect( return NextResponse.redirect(url); } -async function convexAuthNextjsServerState(): Promise { +async function convexAuthNextjsServerState(): Promise { const { token } = await getRequestCookies(); return { // The server doesn't share the refresh token with the client