Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Logout and delete data when user logs out of WordPress #150

Merged
merged 26 commits into from
Jan 5, 2023

Conversation

psrpinto
Copy link
Member

@psrpinto psrpinto commented Dec 20, 2022

Fixes #118

Previously, before the move to using hydrogen internally instead of chatterbox, we implemented logging the user out from chatrix when they are not logged in to WordPress. This code was non-functional since the move to hydrogen.

The approach we took was to execute some JavaScript on all non-logged-in page loads. However, this approach prevents chatrix from being used without logging in to WordPress, since the user would be logged out from chatrix on every page load. Instead, the user should be logged out from chatrix only if a WordPress logout has happened.

That's the approach we take in this PR. We set a cookie on WordPress logout, then run some JavaScript if and only if that cookie exists. This allows for using chatrix without logging in to WordPress. Chatrix logout will still happen if the user subsequently logs in and out of WordPress.

Sumary of changes

  • (The two first commits are unrelated to this PR, just fixing little things I noticed)
  • Move logout-related JavaScript code from src/ to frontend/, and add a vite build for this which produces a logout.iife.js bundle. This allows us to use TypeScript instead of vanilla JS.
  • Enqueue logout.iife.js on all non-logged-in page loads
  • Set a chatrix-logout cookie on WordPress logout
  • In logout.ts, check it chatrix-logout cookie exists. If it does exist, logout all chatrix sessions, then delete all data in local storage and indexedDB.

@psrpinto psrpinto self-assigned this Dec 20, 2022
@psrpinto psrpinto marked this pull request as ready for review December 20, 2022 15:32
@psrpinto psrpinto requested review from ashfame and akirk December 20, 2022 15:35
Copy link
Member

@ashfame ashfame left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Works well! 👏

I am curious if you ran into the need to load the logout JS code all the time as opposed to only doing it when the logout cookie is detected, as you wanted to do earlier?

@psrpinto
Copy link
Member Author

psrpinto commented Jan 5, 2023

That's a good point about only loading the logout JS when the cookie is detected, didn't even cross my mind. (Previously I mentioned only loading the logout JS code when needed, but I think that was before we settled on using a cookie.) I will look into this in another PR.

@psrpinto psrpinto merged commit d29fe35 into main Jan 5, 2023
@psrpinto psrpinto deleted the delete-sessions branch January 5, 2023 13:25
@psrpinto psrpinto mentioned this pull request Jan 9, 2023
@psrpinto psrpinto mentioned this pull request Jan 25, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Logout user from matrix when they logout in WordPress
2 participants