> ## Documentation Index
> Fetch the complete documentation index at: https://docs.timeback.com/llms.txt
> Use this file to discover all available pages before exploring further.

# SolidStart

> Integrate Timeback with SolidStart

## Overview

The Timeback SDK provides a SolidStart adapter using middleware.

## Installation

<CodeGroup>
  ```bash npm theme={null}
  npm install @timeback/sdk
  ```

  ```bash pnpm theme={null}
  pnpm add @timeback/sdk
  ```

  ```bash yarn theme={null}
  yarn add @timeback/sdk
  ```

  ```bash bun theme={null}
  bun add @timeback/sdk
  ```
</CodeGroup>

## Server Setup

Create a server-only Timeback instance. The session helpers below belong to your application: persist the callback user in your authenticated session, and return that session user (including `email`) or `null` from `getUser`. These helpers are not SDK exports. Keep API and SSO secrets on the server:

```typescript src/lib/timeback.ts theme={null}
import { createTimeback } from '@timeback/sdk'

export const timeback = await createTimeback({
    env: 'staging',
    api: {
        clientId: process.env.TIMEBACK_API_CLIENT_ID!,
        clientSecret: process.env.TIMEBACK_API_CLIENT_SECRET!,
    },
    identity: {
        mode: 'sso',
        clientId: process.env.AWS_COGNITO_CLIENT_ID!,
        clientSecret: process.env.AWS_COGNITO_CLIENT_SECRET!,
        redirectUri: 'http://localhost:3000/api/timeback/identity/callback',
        onCallbackSuccess: async ({ user, state, redirect }) => {
            await setSession({ id: user.id, email: user.email })
            return redirect(state?.returnTo ?? '/')
        },
        onCallbackError: ({ error, redirect }) => {
            return redirect('/?error=sso_failed')
        },
        getUser: () => getSession(),
    },
})
```

## Middleware

Create middleware to handle Timeback routes:

```typescript src/middleware.ts theme={null}
import { createMiddleware } from '@solidjs/start/middleware'
import { solidStartHandler } from '@timeback/sdk/solid-start'
import { timeback } from '~/lib/timeback'

export default createMiddleware({
    onRequest: [
        async event => {
            const response = await solidStartHandler({
                timeback,
                event,
            })
            if (response) return response
        },
    ],
})
```

## Alternative: Route-Based Handler

For more control, you can use a route-based approach instead of middleware:

```typescript src/routes/api/timeback/[...path].ts theme={null}
import { toSolidStartHandler } from '@timeback/sdk/solid-start'
import { timeback } from '~/lib/timeback'

const handlers = toSolidStartHandler(timeback)

export const GET = handlers.GET
export const POST = handlers.POST
```

## Client Provider

Wrap your app with the TimebackProvider:

```tsx src/app.tsx theme={null}
import { TimebackProvider } from '@timeback/sdk/solid'
import { Router } from '@solidjs/router'
import { FileRoutes } from '@solidjs/start/router'
import { Suspense } from 'solid-js'

export default function App() {
    return (
        <TimebackProvider>
            <Router root={props => <Suspense>{props.children}</Suspense>}>
                <FileRoutes />
            </Router>
        </TimebackProvider>
    )
}
```

## Usage

<Tip>
  For the full client-side API, see the [Solid client
  adapter](/beta/build-on-timeback/sdk/client/solid).
</Tip>

Use primitives in components:

```tsx src/components/UserStatus.tsx theme={null}
import { SignInButton, createTimebackVerification } from '@timeback/sdk/solid'
import { Show } from 'solid-js'

export function UserStatus() {
    const verification = createTimebackVerification()

    return (
        <Show when={verification.state.status === 'verified'} fallback={<SignInButton size="lg">Sign In</SignInButton>}>
            <div>Welcome!</div>
        </Show>
    )
}
```

## Next Steps

<CardGroup cols={2}>
  <Card title="Solid Client" href="/beta/build-on-timeback/sdk/client/solid">
    Client-side Solid integration
  </Card>

  <Card title="Identity" href="/beta/build-on-timeback/sdk/identity">
    Authentication options
  </Card>

  <Card title="Custom Activities" href="/beta/build-on-timeback/sdk/activity-tracking/intro">
    Track learning sessions
  </Card>
</CardGroup>
