mirror of
https://github.com/langgenius/dify-docs.git
synced 2026-07-22 04:15:24 -04:00
[DOCS]: iframe component session isolation issue #240
Reference in New Issue
Block a user
Delete Branch "%!s()"
Deleting a branch is permanent. Although the deleted branch may continue to exist for a short time before it actually gets removed, it CANNOT be undone in most cases. Continue?
Originally created by @wjt123454321 on GitHub (Sep 10, 2025).
Type of Documentation Change
Error/Typo fix
Documentation Page URL or Path
No response
Current Content
Excuse me, how to implement user and session management? The iframe uses localStorage to store the session ID internally, which causes the frontend application to still display the session of the previous user even after switching login accounts.
Suggested Changes
give iframe options to control session
Reason for Change
No response
Code of Conduct
@dosubot[bot] commented on GitHub (Sep 10, 2025):
Hi @wjt123454321! I'm Dosu and I’m helping the dify-docs team.
Dify's iframe embed manages session state using localStorage, keyed by both appId and userId. To isolate sessions for different users, you should pass a unique, properly encoded userId as a URL parameter (
sys.user_id) when generating the iframe. This userId must be GZIP-compressed, Base64-encoded, and URI-encoded for Dify to recognize it and associate the correct session and history [docs reference].However, the iframe embed does not currently offer built-in options to reset or clear session state when switching users. Session isolation relies on updating the userId in the iframe URL and ensuring localStorage is cleared or updated accordingly. The script-based embed method allows more granular session control, but the iframe method lacks these options [code reference].
For documentation, it would be helpful to clarify these limitations and recommend that developers explicitly reset the iframe and clear localStorage when switching users to avoid session leakage. There are no recent changes adding session isolation options for iframes, so this is a current architectural limitation.
To reply, just mention @dosu.
How did I do? Good | Irrelevant | Incorrect | Verbose | Hallucination | Report 🐛 | Other

@wjt123454321 commented on GitHub (Sep 10, 2025):
Thank you for your answer!
But when I use this method, there are 401 errors.
const iframeUrl = computed(() => { const baseUrl = '' const encodedUserId = encode(userId.value) return encodedSid ?${baseUrl}?sys.user_id=${encodedUserId}: baseUrl })And frontend port is diffefent from dify port, how can I delete the localstorage by js?
@dosu
@dosubot[bot] commented on GitHub (Sep 10, 2025):
The 401 errors are likely due to incorrect encoding of
sys.user_idor CORS/auth issues. Dify expects the user ID to be GZIP-compressed, then Base64-encoded, then URI-encoded before adding it to the iframe URL—using justencode()won't work. Here’s a working example using pako:Dify will decode this as URI-decode → Base64-decode → GZIP-decompress [source].
For cross-origin localStorage: browsers block direct access from your frontend to the iframe’s localStorage if they’re on different ports/domains. You cannot clear it via JS from the parent. The only workaround is to reload the iframe with a new
sys.user_id(which resets the session for that user), or usepostMessageif both sides implement a message handler [source].For 401 errors, double-check:
WEB_API_CORS_ALLOW_ORIGINS, etc.) allow your frontend’s origin.If you switch users, reload the iframe with the new encoded user ID to isolate sessions. There’s no built-in way to clear the iframe’s localStorage from outside if cross-origin.
To reply, just mention @dosu.
How did I do? Good | Irrelevant | Incorrect | Verbose | Hallucination | Report 🐛 | Other
