# Single sign-on

The visitor signs in at the app's identity provider (Okta, Entra ID, Google Workspace and the like), which the builder set up in Softr. There is no server function to write. A link starts it, and the visitor comes back signed in:

```tsx
import { useLocation } from '@tanstack/react-router'
import { ssoSignInHref, toReturnPath, useAuthSettings } from '@softr/sdk'

const options = useAuthSettings()
const location = useLocation()

{options.sso ? (
  <Button nativeButton={false} render={<a href={ssoSignInHref(toReturnPath(location).href)} />}>
    Continue with SSO
  </Button>
) : null}
```

A plain `<a href>`, never `<Link>` or `navigate`: the visitor leaves the app for the identity provider. They land on the path passed in, which only works for a path on this app. Without one, they land where `auth.guard.alreadyAuthenticated` in `softr.json` points, as after any other sign-in.

Sign-in starts in the app, always. A sign-in started at the identity provider, from an app tile on its dashboard, is not supported: it arrives without the app's record of having started and ends in `expired`.

When single sign-on needs more from the visitor, or fails, they come back to the page `auth.routes.sso.path` in `softr.json` names, `/sso-authentication` when it is not set. That page must exist. It reads two search params, and the login page's redirect param (`auth.routes.login.redirectParam`) with the path the visitor wanted:

| Param | Means | Show |
| --- | --- | --- |
| `authenticator` | `'verify'`, `'setup'` or `'setupOptional'`: the app asks for an authenticator app as well | the second-factor step, as for `authenticatorRequired` and its `step`. Finish with the same calls, then `toReturnPath(location)` |
| `error` | why it did not finish, one of the codes below | a message, a "Try again" link to `ssoSignInHref(toReturnPath(location).href)`, and a link to the login page that keeps the redirect param, so either way the visitor ends where they wanted |

| `error` | Means |
| --- | --- |
| `host` | Single sign-on works only on the published app, not in the preview. Say so. Never link to a URL from the search params: anyone can put one there |
| `idp` | The identity provider refused or the visitor cancelled there |
| `not-enabled` | Single sign-on is off or not set up completely |
| `sign-up-disabled` | No account for this visitor, and the app does not let new people sign up |
| `restricted` | The account is deactivated, or the app has no room for another user |
| `expired` | It took too long, another tab started a newer sign-in, or the sign-in started at the identity provider rather than in the app |
| `failed` | Anything else |

With neither param, redirect to `toReturnPath(location)`.

An app whose only way in is single sign-on still has a login page, with the button on it.

## Three login pages

Three ways to put it on the login page. Build the one the builder asks for:

1. **A button**, as above, next to the other methods. The default.
2. **Email first.** Ask for the email, then call `auth.discoverSso({ email })` from a server function. It answers a `Result` of `{ sso: boolean }`, decided by the email's domain alone, and fails only with `tooManyAttempts`. On `sso: true` send the visitor to `ssoSignInHref(...)` with `window.location.assign`, otherwise carry on with the password or code step. Never hardcode the domains: the builder changes them in Softr, and the app would not follow.
3. **Straight to the identity provider** when single sign-on is the only method: send the visitor to `ssoSignInHref(...)` as the login page loads. Skip it when the login page has a `signedOut` search param, or the visitor is signed straight back in after signing out, since the identity provider still knows them. The sign-out server function adds it: `throw redirect({ to: '/login', search: { signedOut: true } })`, and so does the login link on the single sign-on page, or an error there sends the visitor to the identity provider and back to the same error. Keep the button on the page for that case.
