Skip to content

Blog

Keycloak "Restart login cookie not found": how to fix it

Why Keycloak shows "Restart login cookie not found": blocked or dropped cookies, iframes, hostname changes, old tabs and bookmarks, and how to fix each.

Author
The RealmDeploy team
Published

"Restart login cookie not found" is an error page users see in the middle of signing in. They typed a password or clicked a button on the Keycloak login page, and instead of moving on, Keycloak says it cannot find the state of their login.

To understand it, you need two cookies. While a login is in progress, Keycloak keeps an authentication session and points the browser to it with the AUTH_SESSION_ID cookie. It also sets a backup cookie, KC_RESTART, which holds enough to start the same login again. When the authentication session is gone, Keycloak tries the backup. This error means the backup is missing too.

So the message is almost always about cookies the browser did not store or did not send. Sometimes it is about a login page that was opened long ago in a browser that has since forgotten its session cookies. This guide covers Keycloak 26.x; the strings quoted are from the Keycloak 26.8.2 source.

What you see

The full text on the error page is:

Restart login cookie not found. It may have expired; it may have been deleted or cookies are disabled in your browser. If cookies are disabled then enable them. Click Back to Application to login again.

Two related messages help tell the cases apart:

MessageWhat it means
Your login attempt timed out. Login will start from the beginning.The authentication session expired, but KC_RESTART was there, so Keycloak restarted the login. This is the good outcome.
Restart login cookie not found. ...The authentication session was gone and KC_RESTART was missing.
An error occurred, please login again through your application.KC_RESTART was there but could not be used, for example because it belongs to a different client.

In the realm's events, the missing-cookie case is an error event, for the login form a LOGIN_ERROR, with error set to cookie_not_found. A restart after a timeout is logged with error set to expired_code and a restart_after_timeout detail.

If Keycloak serves a request it does not consider secure, the server log has this WARN line once:

Non-secure context detected; cookies are not secured, and will not be available in cross-origin POST requests.

Causes, most common first

  1. The login runs inside an iframe on another site. Browsers that block third-party cookies drop AUTH_SESSION_ID and KC_RESTART. The error appears only in the embedded login, and only in some browsers.
  2. The hostname changes between steps. The login page was served from one host and the form posts to another, for example keycloak.internal:8080 and auth.example.com. Cookies set for one host are not sent to the other. The form's action address shows it.
  3. Keycloak thinks the request is plain HTTP. Behind a TLS-terminating proxy without the right proxy headers, Keycloak marks its cookies SameSite=Lax and not Secure. Cross-site POSTs, such as a SAML POST binding or an identity provider posting back, then arrive without them. The WARN line above confirms it.
  4. The browser forgot its session cookies. Both cookies live for the browser session only. A user who leaves a login tab open, closes the browser and restores the tab later, or opens a login link copied from another browser, has neither cookie.
  5. A bookmarked or shared login page. Some users bookmark the page they see when they sign in. That address points to a single login attempt and stops working once it is over.
  6. The back button and several tabs. Going back to a finished login step or signing in from two tabs at once can leave a tab pointing at state that no longer exists.
  7. A proxy that drops cookies. A proxy or web application firewall that removes Set-Cookie headers, rewrites the cookie path or strips cookies from requests.

Diagnose it step by step

  1. Turn on saved events. In the admin console, open Realm settings, then the Events tab, and under User events settings switch on Save events. Then open Events in the left menu and filter by cookie_not_found.
  2. Reproduce in a clean private window with the browser's developer tools open on the Network tab, with Preserve log on.
  3. Follow the cookies. On the first response from the login page, check that AUTH_SESSION_ID and KC_RESTART are set. Look at their Domain, Path, Secure and SameSite attributes. On the form POST that fails, check that the request sends them back.
  4. Compare hosts. The host of the login page and the host in the form's action must be the same. If they differ, the hostname setting is wrong.
  5. Check the scheme Keycloak sees. Open the realm's discovery document and look at issuer:
curl -s https://auth.example.com/realms/myrealm/.well-known/openid-configuration | python3 -m json.tool | grep issuer

If it says http:// while users browse with https://, Keycloak is not reading the proxy's headers.

  1. Search the server log for "Non-secure context detected".
  2. Test outside the iframe. Open the same login URL in a top-level tab. If it works there, the iframe is the cause.

Fixes

Logins inside iframes

Move the login out of the iframe. Use a full-page redirect to Keycloak, or a popup window, and come back to the app with the code. Embedded logins depend on third-party cookies, and browsers keep restricting them.

If an app uses a silent check in a hidden iframe (for example check-sso in keycloak-js), it is not the login itself. Let the app fall back to a redirect when the silent check fails, instead of trying to log in inside the iframe.

One public hostname

Keycloak must build every address with the public hostname users see. Set it once:

bin/kc.sh start --hostname=https://auth.example.com --proxy-headers=xforwarded --http-enabled=true

Use --proxy-headers=forwarded instead if your proxy sends the standard Forwarded header. Do not let users reach Keycloak on its internal address.

The proxy must pass the scheme and keep cookies

Your proxy must set the forwarding headers and must not touch cookies. For nginx:

location / {
    proxy_pass http://keycloak:8080;
    proxy_set_header Host $host;
    proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
    proxy_set_header X-Forwarded-Proto $scheme;
    proxy_set_header X-Forwarded-Host $host;
    proxy_set_header X-Forwarded-Port $server_port;
    proxy_buffer_size 128k;
    proxy_buffers 4 256k;
}

The larger buffers keep nginx from failing when Keycloak sends large headers, such as several long cookies at once. Do not use proxy_cookie_path or proxy_cookie_domain for Keycloak.

Old tabs, bookmarks and the back button

These cannot be fixed on the server, but you can make them painless:

  • Give users a stable address to bookmark, such as your app's sign-in page, and tell them not to bookmark the Keycloak page.
  • Set the client's Home URL. The "Back to Application" link on the error page appears only when the client has one.
  • Keep Login timeout on the Tokens tab of Realm settings long enough for real users. Keycloak's own help text recommends a relatively long value. As long as KC_RESTART is present, a timed-out login restarts instead of failing.

Two tabs with two clients

KC_RESTART restarts the login only for the client it was created for. If users start a login for one app and finish it in a tab for another app, they get "An error occurred, please login again through your application." Starting again from the app is the fix.

On RealmDeploy

We set the hostname and the proxy headers for you. Your Keycloak runs with KC_HOSTNAME set to https:// and your own hostname, TLS ends at our gateway, and Keycloak reads X-Forwarded-* headers that the gateway sets itself after dropping any sent by clients. Keycloak therefore sees requests as HTTPS and sets its cookies as secure. If you see the "Non-secure context detected" warning or a wrong issuer on your tenant, write to us: that would be ours to fix.

Iframes, bookmarks, Login timeout and the client's Home URL are on your side. User sessions are kept in the database, and on Premium and Enterprise every Keycloak replica reads the same state, so several replicas are not a cause of this error.

Write to us from the dashboard or through the contact form. Include the time, the browser and the cookie_not_found event. See Contact and support.

Tools that help