Skip to Content
Clerk logo

Clerk Docs

Ctrl + K
Go to clerk.com

withServerAuth()

Usage

import * as React from 'react'; import { withServerAuth } from 'gatsby-plugin-clerk/ssr'; import { PageProps } from "gatsby"; export const getServerData = withServerAuth( async props => { return { props: { data: '1', auth: props.auth } }; }, { loadUser: true }, ); function SSRPage({ serverData }: PageProps) { return ( <main> <h1>SSR Page with Clerk</h1> <pre>{JSON.stringify(serverData, null, 2)}</pre> </main> ); } export default SSRPage;

Props

withServerAuth takes two arguments:

function withServer(cb: Callback, options: Options): GetServerDataReturn;

Gatsby then automatically passes the result of withServer's callback to the serverData property on the page's component props.

Callback props

NameTypeDescription
authServerSideAuthCurrent auth data, including sessionId and userId.
sessionSessionThe current session.

Requires loadSession to be set to true in options.
userUserThe current user.

Requires loadUser to be set to true in options.
organizationOrganizationThe current organization.

Requires loadOrg to be set to true in options.

Options

NameTypeDescription
loadUser?booleanIf true, load the user data for the current auth session.
loadSession?booleanIf true, load the session data for the current auth session.
loadOrg?booleanIf true, load the organization data for the current auth session.
jwtKey?stringAn optional custom JWT key to use for session token validation.

Read more about jwtKey with our docs page.
authorizedParties?string[]An optional list of domains allowed to request JWT authorization.

Read more about authorizedParties with our docs page.

What did you think of this content?

Clerk © 2023