err:unkey:authentication:portal_session_not_foundExample
What Happened?
This error is returned byPOST /v2/portal.exchangeCode and any portal-authenticated endpoint when the supplied credential cannot be resolved to a usable session. There are four common reasons:
- Expired code: The exchange code carried by the portal URL is valid for 15 minutes. After that it can no longer be redeemed.
- Already-redeemed code: Codes are single-use. Once the portal redeems one, the same code cannot be redeemed again.
- Expired access token: After exchange, the access token is valid for 24 hours. Once it expires, requests using it return this error.
- Revoked session: A session that has been explicitly revoked returns this error even before its natural expiry.
How To Fix
Create a fresh session from your backend and redirect the user again:url. The portal will redeem the new code for a 24-hour access token.
Re-authenticating always mints a new session rather than extending the existing one, so this is the correct response to every case above.
If you passed a returnUrl when creating the session, expired sessions will automatically redirect there with ?reason=session_expired. Use that hook to re-mint a session and bounce the user back into the portal seamlessly.
Common Mistakes
- Reusing a code: Codes are single-use. Generate a new session for every redirect.
- Storing codes: Don’t persist exchange codes. They are short-lived credentials meant to be consumed immediately, and they are not recoverable from Unkey once issued.
- Confusing
idwith the code:portal.createSessionreturns a non-secretidalongside theurl. Theididentifies the session for your own records; it is not a credential and cannot be exchanged. - Long-running tabs: Users who keep the portal open beyond 24 hours need a fresh session.
Related Errors
- err:unkey:authentication:portal_token_missing - When no portal access token is supplied at all
- err:unkey:data:portal_not_found - When the portal referenced by
portaldoes not exist - err:unkey:authentication:missing - When no authentication credentials are provided to a non-portal endpoint