# Sign-in Form

> Sign in, accept an invitation and manage sessions.

Page: https://uicontext.com/docs/components/sign-in-form

Group: Settings.

## When to use

Sign in, accept an invitation and manage sessions. Use [Account Menu](https://uicontext.com/docs/components/account-menu.md) instead when the person is already signed in.

## Usage

```ts
import { AuthScreen, arrivalOf } from "./ui/uicontext/components/Session.ts"

export const SignInPage = <M>(h: Html.H<M>, link: { readonly join?: { readonly id: string; readonly code: string } }, claimed: boolean) =>
	AuthScreen(h, {
		props: {
			arrival: arrivalOf(link),
			claimed,
			methods: ["password", "link"],
		},
	})
```

## Examples

Each example is a complete file. Its imports point where the CLI installs the source.

### Password, email link or passkey

A wrong password says what to check; the fourth try in a row is rate limited. An emailed link signs in without one.

```ts title="src/demo.ts" source="www/src/examples/sign-in-form/demo.ts"
import { Component } from "effect/ui"
import { Html } from "effect/ui/html"
import * as stylex from "@stylexjs/stylex"
import { Schema } from "effect"
import { AuthScreen } from "./ui/uicontext/components/Session.ts"
import { sx } from "./ui/uicontext/components/Sx.ts"
import { color, corner } from "./ui/uicontext/components/tokens.stylex.ts"
import { PretendAuthServer } from "./server.ts"

const styles = stylex.create({
	frame: { inlineSize: "min(100%, 26rem)", blockSize: 560, overflow: "hidden", borderRadius: corner.card, borderWidth: 1, borderStyle: "solid", borderColor: color.line },
})

const SignInNoDeps = Html.component({
	name: "SignIn",
	model: Schema.Struct({}),
	message: Schema.Never,
}).make({
	init: () => ({}),
	update: ($) => $.model,
	view: (h) =>
		h.div.attrs(sx(styles.frame)).children(
			AuthScreen(h, {
				props: {
					arrival: { _tag: "SignIn" },
					claimed: true,
					methods: ["password", "link", "passkey"],
					copy: { signIn: { title: "Sign in to Northwind", lede: "Use your work email." }, emailPlaceholder: "you@northwind.example" },
				},
			}),
		),
})

// the pretend server stands in for your auth routes
export const SignIn = SignInNoDeps.pipe(Component.provide(PretendAuthServer))
```

### Joining from an invitation

The invitation is read first: the email it was sent to fills the form.

```ts title="src/invitation.ts" source="www/src/examples/sign-in-form/invitation.ts"
import { Component } from "effect/ui"
import { Html } from "effect/ui/html"
import * as stylex from "@stylexjs/stylex"
import { Schema } from "effect"
import { AuthScreen, arrivalOf } from "./ui/uicontext/components/Session.ts"
import { sx } from "./ui/uicontext/components/Sx.ts"
import { color, corner } from "./ui/uicontext/components/tokens.stylex.ts"
import { PretendAuthServer } from "./server.ts"

const styles = stylex.create({
	frame: { inlineSize: "min(100%, 26rem)", blockSize: 520, overflow: "hidden", borderRadius: corner.card, borderWidth: 1, borderStyle: "solid", borderColor: color.line },
})

const InvitationNoDeps = Html.component({
	name: "JoinFromInvitation",
	model: Schema.Struct({}),
	message: Schema.Never,
}).make({
	init: () => ({}),
	update: ($) => $.model,
	view: (h) =>
		h.div.attrs(sx(styles.frame)).children(
			AuthScreen(h, {
				props: {
					arrival: arrivalOf({ join: { id: "inv-42", code: "k3y" } }),
					claimed: true,
					copy: { join: { title: "Join Northwind", lede: "Ana invited you. Make your account to get in." } },
				},
			}),
		),
})

export const JoinFromInvitation = InvitationNoDeps.pipe(Component.provide(PretendAuthServer))
```

### Sample data

The examples above import these files.

```ts title="src/server.ts" source="www/src/examples/sign-in-form/server.ts"
import { Effect, Layer } from "effect"
import { HttpClient, HttpClientResponse } from "effect/http"

// A pretend auth server for the examples. "maya@northwind.example" with "correct horse" signs in;
// a wrong password is refused, and the fourth try in a row is rate limited for five minutes.
let tries = 0

const answer = (url: URL, body: unknown): { status: number; body: unknown; headers?: Record<string, string> } => {
	const fields = (body ?? {}) as { email?: string; password?: string }
	switch (url.pathname) {
		case "/api/auth/invitation":
			return { status: 200, body: { valid: true, email: "bo@northwind.example", role: "member" } }
		case "/api/auth/sign-in": {
			if (fields.email === "maya@northwind.example" && fields.password === "correct horse") return { status: 200, body: {} }
			tries += 1
			return tries > 3
				? { status: 429, body: { error: "Too many attempts." }, headers: { "retry-after": "300" } }
				: { status: 401, body: { error: "That email and password don’t match. Check both, or email yourself a sign-in link." } }
		}
		case "/api/auth/sign-in-link":
			return { status: 202, body: {} }
		case "/api/auth/passkey":
			return { status: 404, body: { error: "No passkey is registered here." } }
		default:
			return { status: 409, body: { error: "An account with this email already exists. Sign in instead." } }
	}
}

export const PretendAuthServer = Layer.succeed(
	HttpClient.HttpClient,
	HttpClient.make((request, url) =>
		Effect.gen(function* () {
			const text = request.body._tag === "Uint8Array" ? new TextDecoder().decode(request.body.body) : ""
			const reply = answer(url, text === "" ? {} : JSON.parse(text))
			yield* Effect.sleep("400 millis")
			return HttpClientResponse.fromWeb(
				request,
				new Response(JSON.stringify(reply.body), { status: reply.status, headers: { "content-type": "application/json", ...reply.headers } }),
			)
		}),
	),
)
```

## API reference

**Props**

| Name | Type | Description |
| --- | --- | --- |
| `arrival` (required) | `Arrival` | — |
| `claimed` (required) | `boolean` | — |
| `copy` | `AuthCopy` | — |
| `tone` | `"accent" \| "neutral"` | The submit button: the accent (default), or solid neutral for apps that keep color for status |
| `paths` | `AuthPathOverrides` | This screen's session routes, over the ones a component above provides with `authPathsLayer` |
| `methods` | `SignInMethod[]` | How people may sign in, the first shown first; a password only by default |

## Installation

```sh
pnpm dlx shadcn@latest add checkfu/uicontext/sign-in-form
```

With npm: `npx shadcn@latest add checkfu/uicontext/sign-in-form`. With yarn: `yarn dlx shadcn@latest add checkfu/uicontext/sign-in-form`. With bun: `bunx --bun shadcn@latest add checkfu/uicontext/sign-in-form`.

It writes these files:

- `src/ui/uicontext/components/Button.ts`
- `src/ui/uicontext/components/FormControls.ts`
- `src/ui/uicontext/components/IconStroke.ts`
- `src/ui/uicontext/components/Session.ts`
- `src/ui/uicontext/components/ShowText.ts`
- `src/ui/uicontext/components/Sx.ts`
- `src/ui/uicontext/components/button.stylex.ts`
- `src/ui/uicontext/components/domain/Actor.ts`
- `src/ui/uicontext/components/domain/Format.ts`
- `src/ui/uicontext/components/tokens.stylex.ts`
- `src/ui/uicontext/components/variants.ts`

And installs the npm packages `@stylexjs/stylex`, `effect`.

## Source

- [src/Session.ts](https://github.com/checkfu/uicontext/blob/main/src/Session.ts)

## Related

- [Settings](https://uicontext.com/docs/components/settings.md) (`settings`): A settings page with sections, fields and a save bar.
- [Env Editor](https://uicontext.com/docs/components/env-editor.md) (`env-editor`): Names and values such as API keys and URLs, one row each: secrets hidden, a pasted .env file split into rows, mistakes marked.
- [Setting List](https://uicontext.com/docs/components/setting-list.md) (`setting-list`): Settings as rows, each with its control.
- [Inherited Settings](https://uicontext.com/docs/components/inherited-settings.md) (`inherited-settings`): Settings that a workspace sets and a team or project can override.
- [Members](https://uicontext.com/docs/components/members.md) (`members`): Workspace members, their roles and pending invitations.
- [Connections](https://uicontext.com/docs/components/connections.md) (`connections`): Connect outside services, and see each connection's status.
