nextjs/server
ConvexAuthNextjsServerProvider()
Wrap your app with this provider in your root layout.tsx
.
Parameters
Parameter | Type | Description |
---|---|---|
|
| ‐ |
|
| You can customize the route path that handles authentication
actions via this prop and the Defaults to |
|
| Choose how the auth information will be stored on the client. Defaults to If you choose |
|
| 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 |
|
| Turn on debugging logs. |
|
| 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
Parameter | Type | Description |
---|---|---|
| ( | A custom handler, which you can use to decide which routes should be accessible based on the client's authentication. |
|
| ‐ |
|
| The URL of the Convex deployment to use for authentication. Defaults to |
|
| You can customize the route path that handles authentication
actions via this option and the Defaults to |
|
| The cookie config to use for the auth cookies.
See MDN Web Docs (opens in a new tab) for more information. |
|
| ‐ |
|
| 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
Parameter | Type | Description |
---|---|---|
|
| The incoming request handled by the middleware. |
|
| 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
Parameter | Type |
---|---|
|
Returns
Function
Parameters
Parameter | Type |
---|---|
|
|
Returns
boolean