nextjs/server

ConvexAuthNextjsServerProvider()

Wrap your app with this provider in your root layout.tsx.

Parameters

ParameterTypeDescription

props

object

props.apiRoute?

string

You can customize the route path that handles authentication actions via this prop and the apiRoute option to convexAuthNextjsMiddleWare.

Defaults to /api/auth.

props.storage?

"localStorage" | "inMemory"

Choose how the auth information will be stored on the client.

Defaults to "localStorage".

If you choose "inMemory", different browser tabs will not have a synchronized authentication state.

props.storageNamespace?

string

Optional namespace for keys used to store tokens. The keys determine whether the tokens are shared or not.

Any non-alphanumeric characters will be ignored.

Defaults to process.env.NEXT_PUBLIC_CONVEX_URL.

props.verbose?

boolean

Turn on debugging logs.

props.children

ReactNode

Children components can call Convex hooks and useAuthActions.

Returns

Element

Defined in

src/nextjs/server/index.tsx:27 (opens in a new tab)


convexAuthNextjsToken()

Retrieve the token for authenticating calls to your Convex backend from Server Components, Server Actions and Route Handlers.

Returns

undefined | string

The token if the the client is authenticated, otherwise undefined.

Defined in

src/nextjs/server/index.tsx:82 (opens in a new tab)


isAuthenticatedNextjs()

Whether the client is authenticated, which you can check in Server Actions, Route Handlers and Middleware.

Avoid the pitfall of checking authentication state in layouts, since they won't stop nested pages from rendering.

Returns

boolean

Defined in

src/nextjs/server/index.tsx:93 (opens in a new tab)


ConvexAuthNextjsMiddlewareContext

In convexAuthNextjsMiddleware, you can use this context to get the token and check if the client is authenticated in place of convexAuthNextjsToken and isAuthenticatedNextjs.

export function convexAuthNextjsMiddleware(handler, options) {
  return async (request, event, convexAuth) => {
    if (!convexAuth.isAuthenticated()) {
      return nextjsMiddlewareRedirect(request, "/login");
    }
  };
}

Type declaration

getToken()

Returns

string | undefined

isAuthenticated()

Returns

boolean

Defined in

src/nextjs/server/index.tsx:112 (opens in a new tab)


convexAuthNextjsMiddleware()

Use in your middleware.ts to enable your Next.js app to use Convex Auth for authentication on the server.

Parameters

ParameterTypeDescription

handler?

(request, ctx) => NextMiddlewareResult | Promise<NextMiddlewareResult>

A custom handler, which you can use to decide which routes should be accessible based on the client's authentication.

options?

object

options.convexUrl?

string

The URL of the Convex deployment to use for authentication.

Defaults to process.env.NEXT_PUBLIC_CONVEX_URL.

options.apiRoute?

string

You can customize the route path that handles authentication actions via this option and the apiRoute prop of ConvexAuthNextjsProvider.

Defaults to /api/auth.

options.cookieConfig?

object

The cookie config to use for the auth cookies.

maxAge is the number of seconds the cookie will be valid for. If this is not set, the cookie will be a session cookie.

See MDN Web Docs (opens in a new tab) for more information.

options.cookieConfig.maxAge?

null | number

options.verbose?

boolean

Turn on debugging logs.

Returns

NextMiddleware

A Next.js middleware.

Defined in

src/nextjs/server/index.tsx:123 (opens in a new tab)


nextjsMiddlewareRedirect()

Helper for redirecting to a different route from a Next.js middleware.

return nextjsMiddlewareRedirect(request, "/login");

Parameters

ParameterTypeDescription

request

NextRequest

The incoming request handled by the middleware.

pathname

string

The route path to redirect to.

Returns

NextResponse<unknown>

Defined in

src/nextjs/server/index.tsx:267 (opens in a new tab)


RouteMatcherParam

See createRouteMatcher for more information.

Defined in

src/nextjs/server/routeMatcher.ts:44 (opens in a new tab)


createRouteMatcher()

Returns a function that accepts a Request object and returns whether the request matches the list of predefined routes that can be passed in as the first argument.

You can use glob patterns to match multiple routes or a function to match against the request object. Path patterns and regular expressions are supported, for example: ['/foo', '/bar(.*)'] or [/^/foo/.*$/]` For more information, see: https://github.com/pillarjs/path-to-regexp (opens in a new tab)

Parameters

ParameterType

routes

RouteMatcherParam

Returns

Function

Parameters

ParameterType

req

NextRequest

Returns

boolean

Defined in

src/nextjs/server/routeMatcher.ts:58 (opens in a new tab)