📄 tanstack/start/latest/docs/framework/react/examples/start-supabase-basic

File: start-supabase-basic.md | Updated: 11/15/2025

Source: https://tanstack.com/start/latest/docs/framework/react/examples/start-supabase-basic



TanStack

Start v0v0

Search...

+ K

Auto

Log In

TanStack StartRC

Docs Examples GitHub Contributors

TanStack Router

Docs Examples GitHub Contributors

TanStack Query

Docs Examples GitHub Contributors

TanStack Table

Docs Examples Github Contributors

TanStack Formnew

Docs Examples Github Contributors

TanStack DBbeta

Docs Github Contributors

TanStack Virtual

Docs Examples Github Contributors

TanStack Paceralpha

Docs Examples Github Contributors

TanStack Storealpha

Docs Examples Github Contributors

TanStack Devtoolsalpha

Docs Github Contributors

More Libraries

Maintainers Partners Support Learn StatsBETA Discord Merch Blog GitHub Ethos Brand Guide

Documentation

Framework

React logo

React

Version

Latest

Search...

+ K

Menu

Getting Started

Guides

Examples

Tutorials

Framework

React logo

React

Version

Latest

Menu

Getting Started

Guides

Examples

Tutorials

React Example: Start Supabase Basic

Deploy to Cloudflare Deploy with Netlify Github CodeSandbox

=============================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================

Code ExplorerCode

Interactive SandboxSandbox

  • .vscode

  • src

    • components

    • hooks

    • routes

      • _authed

      • __root.tsx file icon__root.tsx

      • _authed.tsx file icon_authed.tsx

      • index.tsx file iconindex.tsx

      • login.tsx file iconlogin.tsx

      • logout.tsx file iconlogout.tsx

      • signup.tsx file iconsignup.tsx

    • styles

    • utils

    • routeTree.gen.ts file iconrouteTree.gen.ts

    • router.tsx file iconrouter.tsx

  • .env file icon.env

  • .gitignore file icon.gitignore

  • .prettierignore file icon.prettierignore

  • package.json file iconpackage.json

  • postcss.config.mjs file iconpostcss.config.mjs

  • tsconfig.json file icontsconfig.json

  • vite.config.ts file iconvite.config.ts

tsx

/// <reference types="vite/client" />
import {
  HeadContent,
  Link,
  Outlet,
  Scripts,
  createRootRoute,
} from '@tanstack/react-router'
import { TanStackRouterDevtools } from '@tanstack/react-router-devtools'
import { createServerFn } from '@tanstack/react-start'
import * as React from 'react'
import { DefaultCatchBoundary } from '../components/DefaultCatchBoundary'
import { NotFound } from '../components/NotFound'
import appCss from '../styles/app.css?url'
import { seo } from '../utils/seo'
import { getSupabaseServerClient } from '../utils/supabase'

const fetchUser = createServerFn({ method: 'GET' }).handler(async () => {
  const supabase = getSupabaseServerClient()
  const { data, error: _error } = await supabase.auth.getUser()

  if (!data.user?.email) {
    return null
  }

  return {
    email: data.user.email,
  }
})

export const Route = createRootRoute({
  beforeLoad: async () => {
    const user = await fetchUser()

    return {
      user,
    }
  },
  head: () => ({
    meta: [\
      {\
        charSet: 'utf-8',\
      },\
      {\
        name: 'viewport',\
        content: 'width=device-width, initial-scale=1',\
      },\
      ...seo({\
        title:\
          'TanStack Start | Type-Safe, Client-First, Full-Stack React Framework',\
        description: `TanStack Start is a type-safe, client-first, full-stack React framework. `,\
      }),\
    ],
    links: [\
      { rel: 'stylesheet', href: appCss },\
      {\
        rel: 'apple-touch-icon',\
        sizes: '180x180',\
        href: '/apple-touch-icon.png',\
      },\
      {\
        rel: 'icon',\
        type: 'image/png',\
        sizes: '32x32',\
        href: '/favicon-32x32.png',\
      },\
      {\
        rel: 'icon',\
        type: 'image/png',\
        sizes: '16x16',\
        href: '/favicon-16x16.png',\
      },\
      { rel: 'manifest', href: '/site.webmanifest', color: '#fffff' },\
      { rel: 'icon', href: '/favicon.ico' },\
    ],
  }),
  errorComponent: (props) => {
    return (
      <RootDocument>
        <DefaultCatchBoundary {...props} />
      </RootDocument>
    )
  },
  notFoundComponent: () => <NotFound />,
  component: RootComponent,
})

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

function RootDocument({ children }: { children: React.ReactNode }) {
  const { user } = Route.useRouteContext()

  return (
    <html>
      <head>
        <HeadContent />
      </head>
      <body>
        <div className="p-2 flex gap-2 text-lg">
          <Link
            to="/"
            activeProps={{
              className: 'font-bold',
            }}
            activeOptions={{ exact: true }}
          >
            Home
          </Link>{' '}
          <Link
            to="/posts"
            activeProps={{
              className: 'font-bold',
            }}
          >
            Posts
          </Link>
          <div className="ml-auto">
            {user ? (
              <>
                <span className="mr-2">{user.email}</span>
                <Link to="/logout">Logout</Link>
              </>
            ) : (
              <Link to="/login">Login</Link>
            )}
          </div>
        </div>
        <hr />
        {children}
        <TanStackRouterDevtools position="bottom-right" />
        <Scripts />
      </body>
    </html>
  )
}


/// <reference types="vite/client" />
import {
  HeadContent,
  Link,
  Outlet,
  Scripts,
  createRootRoute,
} from '@tanstack/react-router'
import { TanStackRouterDevtools } from '@tanstack/react-router-devtools'
import { createServerFn } from '@tanstack/react-start'
import * as React from 'react'
import { DefaultCatchBoundary } from '../components/DefaultCatchBoundary'
import { NotFound } from '../components/NotFound'
import appCss from '../styles/app.css?url'
import { seo } from '../utils/seo'
import { getSupabaseServerClient } from '../utils/supabase'

const fetchUser = createServerFn({ method: 'GET' }).handler(async () => {
  const supabase = getSupabaseServerClient()
  const { data, error: _error } = await supabase.auth.getUser()

  if (!data.user?.email) {
    return null
  }

  return {
    email: data.user.email,
  }
})

export const Route = createRootRoute({
  beforeLoad: async () => {
    const user = await fetchUser()

    return {
      user,
    }
  },
  head: () => ({
    meta: [\
      {\
        charSet: 'utf-8',\
      },\
      {\
        name: 'viewport',\
        content: 'width=device-width, initial-scale=1',\
      },\
      ...seo({\
        title:\
          'TanStack Start | Type-Safe, Client-First, Full-Stack React Framework',\
        description: `TanStack Start is a type-safe, client-first, full-stack React framework. `,\
      }),\
    ],
    links: [\
      { rel: 'stylesheet', href: appCss },\
      {\
        rel: 'apple-touch-icon',\
        sizes: '180x180',\
        href: '/apple-touch-icon.png',\
      },\
      {\
        rel: 'icon',\
        type: 'image/png',\
        sizes: '32x32',\
        href: '/favicon-32x32.png',\
      },\
      {\
        rel: 'icon',\
        type: 'image/png',\
        sizes: '16x16',\
        href: '/favicon-16x16.png',\
      },\
      { rel: 'manifest', href: '/site.webmanifest', color: '#fffff' },\
      { rel: 'icon', href: '/favicon.ico' },\
    ],
  }),
  errorComponent: (props) => {
    return (
      <RootDocument>
        <DefaultCatchBoundary {...props} />
      </RootDocument>
    )
  },
  notFoundComponent: () => <NotFound />,
  component: RootComponent,
})

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

function RootDocument({ children }: { children: React.ReactNode }) {
  const { user } = Route.useRouteContext()

  return (
    <html>
      <head>
        <HeadContent />
      </head>
      <body>
        <div className="p-2 flex gap-2 text-lg">
          <Link
            to="/"
            activeProps={{
              className: 'font-bold',
            }}
            activeOptions={{ exact: true }}
          >
            Home
          </Link>{' '}
          <Link
            to="/posts"
            activeProps={{
              className: 'font-bold',
            }}
          >
            Posts
          </Link>
          <div className="ml-auto">
            {user ? (
              <>
                <span className="mr-2">{user.email}</span>
                <Link to="/logout">Logout</Link>
              </>
            ) : (
              <Link to="/login">Login</Link>
            )}
          </div>
        </div>
        <hr />
        {children}
        <TanStackRouterDevtools position="bottom-right" />
        <Scripts />
      </body>
    </html>
  )
}

Basic + DIY Auth

Trellaux + Convex

Partners Become a Partner

Code RabbitCode Rabbit CloudflareCloudflare AG GridAG Grid NetlifyNetlify NeonNeon WorkOSWorkOS ClerkClerk ConvexConvex ElectricElectric SentrySentry PrismaPrisma StrapiStrapi UnkeyUnkey

scarf analytics