> ## 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.

# Nuxt

> Integrate Timeback with Nuxt 3

## Overview

The Timeback SDK provides a Nuxt adapter using server 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 server/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(),
    },
})
```

## Server Middleware

Create middleware to handle Timeback routes:

```typescript server/middleware/timeback.ts theme={null}
import { nuxtHandler } from '@timeback/sdk/nuxt'

import { timeback } from '../lib/timeback'

export default defineEventHandler(async event => {
    const response = await nuxtHandler({
        timeback,
        event,
    })
    if (response) return response
})
```

## Alternative: Route-Based Handler

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

```typescript server/api/timeback/[...path].ts theme={null}
import { toNuxtHandler } from '@timeback/sdk/nuxt'
import { timeback } from '../../lib/timeback'

const handlers = toNuxtHandler(timeback)
export default defineEventHandler(event => {
  const method = event.node.req.method ?? 'GET'
  const handler = handlers[method as keyof typeof handlers]
  return handler
    ? handler(event)
    : new Response('Method not allowed', { status: 405 })
})
```

## Client Provider

Wrap your app with the TimebackProvider:

```vue app.vue theme={null}
<script setup>
import { TimebackProvider } from '@timeback/sdk/vue'
</script>

<template>
  <TimebackProvider>
    <NuxtPage />
  </TimebackProvider>
</template>
```

## Usage

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

Use composables in components:

```vue components/UserStatus.vue theme={null}
<script setup>
import { SignInButton, useTimebackVerification } from '@timeback/sdk/vue'

const { state } = useTimebackVerification()
</script>

<template>
  <SignInButton v-if="state.status !== 'verified'" size="lg">Sign In</SignInButton>
  <div v-else>Welcome!</div>
</template>
```

## Next Steps

<CardGroup cols={2}>
  <Card title="Vue Client" href="/beta/build-on-timeback/sdk/client/vue">
    Client-side Vue 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>
