Skip to content

Commit

Permalink
update docs
Browse files Browse the repository at this point in the history
  • Loading branch information
thomasballinger committed Nov 27, 2024
1 parent f1626fd commit b591430
Show file tree
Hide file tree
Showing 5 changed files with 16 additions and 13 deletions.
3 changes: 2 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.

Expand Down
8 changes: 3 additions & 5 deletions docs/pages/authz/nextjs.mdx
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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");
}
});
Expand Down
2 changes: 1 addition & 1 deletion docs/pages/index.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion docs/pages/setup.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ and choose `React (Vite)` and then `Convex Auth`.

</Tabs.Tab>
<Tabs.Tab>
**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

Expand Down
14 changes: 9 additions & 5 deletions src/nextjs/server/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

/**
Expand All @@ -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");
* }
* };
Expand Down Expand Up @@ -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;
}

Expand Down Expand Up @@ -280,7 +284,7 @@ export function nextjsMiddlewareRedirect(
return NextResponse.redirect(url);
}

async function convexAuthNextjsServerState(): Promise<ConvexAuthServerState> {
async function convexAuthNextjsServerState(): Promise<ConvexAuthServerState> {
const { token } = await getRequestCookies();
return {
// The server doesn't share the refresh token with the client
Expand Down

0 comments on commit b591430

Please sign in to comment.