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

# Auth

> useAuth on the frontend and context.user in workflows — reading the signed-in user, signing in, and signing out.

The `zitejs/auth` module gives your frontend the signed-in user and sign-in/out actions. In backend
[workflows](/framework/workflows), the same user is available as `context.user`. See
[Authentication](/concepts/authentication) for the concepts (access modes and user sync).

## useAuth (frontend)

```tsx theme={null}
import { useAuth } from 'zitejs/auth';

function Account() {
  const { user, isLoading, loginWithRedirect, logout } = useAuth();

  if (isLoading) return null;
  if (!user) return <button onClick={() => loginWithRedirect()}>Sign in</button>;

  return (
    <div>
      Signed in as {user.email}
      <button onClick={() => logout()}>Sign out</button>
    </div>
  );
}
```

`useAuth()` returns:

| Field               | Type                | Notes                                            |
| ------------------- | ------------------- | ------------------------------------------------ |
| `user`              | `User \| undefined` | The signed-in user; `undefined` while unresolved |
| `isLoading`         | `boolean`           | `true` until auth resolves                       |
| `loginWithRedirect` | `(opts?) => void`   | Redirect to the sign-in page                     |
| `logout`            | `(opts?) => void`   | Sign out                                         |

Both functions take an optional `opts` object:

| Function            | Option        | Type                  | Notes                                |
| ------------------- | ------------- | --------------------- | ------------------------------------ |
| `loginWithRedirect` | `redirectUrl` | `string`              | Where to land after signing in       |
| `loginWithRedirect` | `initialView` | `'login' \| 'signup'` | Open the sign-in or the sign-up view |
| `logout`            | `returnTo`    | `string`              | Where to land after signing out      |

The `User` type is `{ id, email, firstName?, lastName?, … }`. With [user sync](/concepts/authentication#user-sync)
enabled, your users-table fields are merged in and typed, so `user.plan` or `user.team` are available.

<Note>
  In an **internal** app, users are signed in automatically — `loginWithRedirect` and `logout` are not
  used (they throw if called). Use them in **external** apps.
</Note>

## context.user (backend)

Set `authenticated: true` on a workflow and read `context.user`:

```typescript theme={null}
import { createEndpoint } from 'zitejs/backend';
import { zite } from 'zitejs/db';

export default createEndpoint({
  authenticated: true,
  execute: async ({ context }) => {
    // scope the query to the signed-in user
    return zite.Orders.findAll({ filter: { customerEmail: context.user.email } });
  },
});
```

The same synced fields available on the frontend `User` are available on `context.user`, and can be
referenced in row-level [permission](/concepts/permissions) filters via `userField`.
