Docs

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;

Properties

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

  • Name
    auth
    Type
    AuthObject
    Description

    The authentication data for the active user and their current session.

  • Name
    session
    Type
    Session
    Description

    The current session.

  • Name
    user
    Type
    User
    Description

    The current user.

  • Name
    organization
    Type
    Organization
    Description

    The current organization.

  • Name
    sessionId
    Type
    string | null
    Description

    The ID of the current session.

  • Name
    userId
    Type
    string | null
    Description

    The ID of the current user.

  • Name
    actor
    Type
    ActJWTClaim | null
    Description

    The JWT actor for the session.

  • Name
    getToken
    Type
    () => Promise<string | null>
    Description

    A function that returns a promise that resolves to the current user's session token; can also be used to retrieve a custom JWT template

  • Name
    sessionClaims
    Type
    ClerkJWTClaims | null
    Description

    The JWT claims for the session.

  • Name
    orgId
    Type
    string | undefined
    Description

    The ID of the current organization.

  • Name
    orgRole
    Type
    string | undefined
    Description

    The role of the current user in the current organization.

  • Name
    orgSlug
    Type
    string | undefined
    Description

    The slug of the current organization.

  • Name
    orgPermissions
    Type
    string[] | undefined
    Description

    The permissions of the current user in the current organization.

  • Name
    loadUser?
    Type
    boolean
    Description

    If true, load the user data for the current auth session.

  • Name
    loadSession?
    Type
    boolean
    Description

    If true, load the session data for the current auth session.

  • Name
    loadOrg?
    Type
    boolean
    Description

    If true, load the organization data for the current auth session.

  • Name
    jwtKey?
    Type
    string
    Description

    An optional custom JWT key to use for session token validation.

  • Name
    authorizedParties?
    Type
    string[]
    Description

    An allowlist of origins to verify against, to protect your application from the subdomain cookie leaking attack.
    For example:
    ['http://localhost:3000', 'https://example.com']
    For more information, refer to the reference guide.

Feedback

What did you think of this content?