# Ask users to sign in again

> Require a recent sign-in, or a fresh one, before an action, and make sure the right user is the one signing in.

Some actions need proof the user is still at the keyboard, such as showing a recovery code or changing a password. The
app asks for it in its sign-in request, with three parameters it can combine. The user signs in again through the
chain they used before, and their session continues: the same session, freshly proved.

| Parameter | What it asks for | What the user sees |
|---|---|---|
| `max_age=300` | A sign-in no older than 300 seconds | Asked to sign in again only if their sign-in is older than that |
| `prompt=login` | A fresh sign-in, whatever its age | Always asked to sign in again |
| `id_token_hint=<ID token>` | That the user signing in is this one | Nothing extra; anyone else signing in is refused |

**Before you start:**

- **Who can do it:** the app's developer. There's nothing for a realm administrator to set.
- **Where:** in the app's own sign-in request.
- **First:** register the app for the browser sign-in, as in
  [Sign users in to a web app](../../sign-in-to-apps/sign-in-web-app/).

## Ask for a fresh sign-in

<Steps>

1. Before the action, the app sends a sign-in request with `max_age`, `prompt=login`, or both, and the current user's
   ID token as `id_token_hint`.

2. The user signs in again if they need to, and the app receives a code as usual.

3. The app checks the new ID token's `auth_time`: it is the time of the fresh sign-in.

</Steps>

## Good to know

- **A fresh sign-in counts only if it happened after the request.** Going back to an old page with an old session
  doesn't count.
- **A session belonging to someone other than the hint's user** is answered `login_required`.
- **A hint the realm didn't sign for this app** is refused as `invalid_request`, never ignored. An expired hint is
  still accepted, because it only names a user.
- **With `prompt=none`, none of this can happen:** the app is told `login_required`.
- **At an API behind the gateway**, a policy can require a sign-in no older than a set age. The gateway then answers
  `401` with `error="insufficient_user_authentication"` and the `max_age` it needs.

## Check it worked

Send `prompt=login` as a signed-in test user: you're asked to sign in again, and the new ID token's `auth_time` is
the moment you did. The user's trail holds a `LOGIN` record for the fresh sign-in, and a `SESSION_UPGRADED` record
when it continued their session. Find them in **Audit → Authentication** in the dashboard.

## Troubleshooting

| Symptom | Cause | Fix |
|---|---|---|
| The user isn't asked to sign in again | `max_age` is longer than the time since their sign-in | Use a shorter `max_age`, or `prompt=login` |
| `login_required` | The request also carried `prompt=none`, or the hint names another user | Drop `prompt=none`; send the current user's ID token |
| `invalid_request` | The hint wasn't signed by the realm for this app | Send the app's own ID token, unchanged |
| `auth_time` didn't change | The app read the old ID token | Read the ID token from this sign-in's code |

## Undo it

Stop sending `max_age` and `prompt=login`. Nothing changes in the realm.

## Related

- [Step-up and re-authentication](../../../applications/single-sign-on/step-up/): `max_age`, `prompt` and `auth_time`.
- [Step up for sensitive actions](../step-up-for-sensitive-actions/): a stronger sign-in rather than a newer one.
- [Check quietly whether a user is signed in](../../sign-in-to-apps/silent-sign-in-check/): `prompt=none`.
