# Second factors and recovery

> TOTP, pending sign-ins, recovery codes, and undoing a recovery sign-in that the person didn't make.

From `docs/guide.md` in tensile-tools/tensile-auth at commit `e66760d` (v0.4.1-7-ge66760d). The HTML page is https://auth.tensile.tools/docs/guides/second-factors.

## Second factors and pending sign-ins

When the account has TOTP and the first proof isn't a passkey, `SignIn` doesn't start a session yet: it sets a pending cookie and returns `res.Needs == [totp]`. Ask for the code, then finish:

```go
proof, err := twoFactor.Verify(ctx, res.First, code) // twoFactor is your *totp.TOTP; // res.First from SignIn, kept in your page state
p, err := authhttp.CompleteSignIn(w, r, svc, proof)
```

A reused code answers `code_reused` with the seconds until the next one, and doesn't count toward the 100-failure lockout. Enrolling TOTP returns a proof from `Confirm`, so finishing enrolment can also step the session up.

## Recovery

`recovery.Codes.Generate` returns 10 codes to show once. To sign in with one, check it without spending it, then sign in as usual:

```go
proof, err := codes.Check(ctx, typed)
res, err := authhttp.SignIn(w, r, svc, proof, auth.StartOptions{})
```

The code is spent only when the session starts, so leaving at the TOTP prompt keeps it. The session is in recovery mode: it can add a passkey and make new recovery codes, and every other sensitive operation answers `stronger_factor_required` with the fix "Add a passkey". For an hour (`auth.WithRecoveryHold`), the whole account can't create tokens, approve apps, or claim agents.

Show `svc.RecoverySignIns(ctx, user)` on the person's other devices with a "That wasn't me" button that calls `svc.UndoRecovery(ctx, principal, session)`. It revokes everything created since the recovery, removes new factors, deletes the remaining codes, and lifts the hold.
