JSPM

@clerk/tanstack-start

0.2.2-canary.v1a2805b
  • ESM via JSPM
  • ES Module Entrypoint
  • Export Map
  • Keywords
  • License
  • Repository URL
  • TypeScript Types
  • README
  • Created
  • Published
  • Downloads 1841
  • Score
    100M100P100Q120647F
  • License MIT

Clerk SDK for TanStack Start

Package Exports

  • @clerk/tanstack-start
  • @clerk/tanstack-start/package.json
  • @clerk/tanstack-start/server

Readme


@clerk/tanstack-start


Overview

Clerk is the easiest way to add authentication and user management to your TanStack Start application. Add sign up, sign in, and profile management to your application in minutes.

Getting Started

Prerequisites

  • React 18 or later
  • Node.js >=18.17.0 or later

Installation

npm install @clerk/tanstack-start

Build

To build the package locally with the TypeScript compiler, run:

npm run build

To build the package in watch mode, run the following:

npm run dev

Usage

Make sure the following environment variables are set in a .env file:

CLERK_PUBLISHABLE_KEY=[publishable-key]
CLERK_SECRET_KEY=[backend-secret-key]

You can get these from the API Keys screen in your Clerk dashboard.

To initialize Clerk with your TanStack Start application, you will need to make one modification to app/routes/_root.tsx:

  • Wrap the children of the RootComponent with <ClerkProvider/>
import { ClerkProvider } from '@clerk/tanstack-start'
import { createRootRoute } from '@tanstack/react-router'
import { Link, Outlet, ScrollRestoration } from '@tanstack/react-router'
import { TanStackRouterDevtools } from '@tanstack/router-devtools'
import { Body, Head, Html, Meta, Scripts } from '@tanstack/start'
import * as React from 'react'
import { DefaultCatchBoundary } from '~/components/DefaultCatchBoundary'
import { NotFound } from '~/components/NotFound'

export const Route = createRootRoute({
  meta: () => [
    {
      charSet: 'utf-8',
    },
    {
      name: 'viewport',
      content: 'width=device-width, initial-scale=1',
    },
  ],
  errorComponent: (props) => {
    return (
      <RootDocument>
        <DefaultCatchBoundary {...props} />
      </RootDocument>
    )
  },
  notFoundComponent: () => <NotFound />,
  component: RootComponent,
})

function RootComponent() {
  return (
    <ClerkProvider>
      <RootDocument>
          <Outlet />
      </RootDocument>
    </ClerkProvider>
  )
}

function RootDocument({ children }: { children: React.ReactNode }) { ... }

Setup clerkHandler in the SSR entrypoint

You will also need to make on more modification to you SSR entrypoint (default: app/ssr.tsx):

  • Wrap the createStartHandler with createClerkHandler
import { createStartHandler, defaultStreamHandler } from '@tanstack/start/server';
import { getRouterManifest } from '@tanstack/start/router-manifest';
import { createRouter } from './router';
import { createClerkHandler } from '@clerk/tanstack-start/server';

const handler = createStartHandler({
  createRouter,
  getRouterManifest,
});

const clerkHandler = createClerkHandler(handler);

/*
 * // You can also override Clerk options by passing an object as second argument
 * const clerkHandler = createClerkHandler(handler, {
 *   afterSignInUrl: '/dashboard',
 * });
 */

export default clerkHandler(defaultStreamHandler);

After those changes are made, you can use Clerk components in your routes.

For example, in app/routes/index.tsx:

import { SignIn, SignedIn, SignedOut, UserButton } from '@clerk/tanstack-start';
import { createFileRoute } from '@tanstack/react-router';

export const Route = createFileRoute('/')({
  component: Home,
});

function Home() {
  return (
    <div className='p-2'>
      <h1>Hello Clerk!</h1>
      <SignedIn>
        <UserButton />
      </SignedIn>
      <SignedOut>
        <SignIn />
      </SignedOut>
    </div>
  );
}

Support

You can get in touch with us in any of the following ways:

Contributing

We're open to all community contributions! If you'd like to contribute in any way, please read our contribution guidelines) .

Security

@clerk/nextjs follows good practices of security, but 100% security cannot be assured.

@clerk/nextjs is provided "as is" without any warranty. Use at your own risk.

For more information and to report security issues, please refer to our security documentation.

License

This project is licensed under the MIT license.

See LICENSE for more information.