From b934bb3a5fa6a8069b1b0a0031461e2b90120348 Mon Sep 17 00:00:00 2001 From: Sharon Kennedy Date: Wed, 26 Feb 2020 00:12:05 -0500 Subject: [PATCH] exit chat properly and clear local storage --- public/index.html | 2 +- src/components/chatbox.jsx | 39 ++++++++++++++++++++++++++++++-------- 2 files changed, 32 insertions(+), 9 deletions(-) diff --git a/public/index.html b/public/index.html index 1d1cf09..563254f 100644 --- a/public/index.html +++ b/public/index.html @@ -4,7 +4,7 @@ Embeddable Chatbox Demo - +

Bookmarklet (drag it to your bookmarks bar)

diff --git a/src/components/chatbox.jsx b/src/components/chatbox.jsx index 86a7e32..212dcf4 100644 --- a/src/components/chatbox.jsx +++ b/src/components/chatbox.jsx @@ -39,6 +39,8 @@ const initialState = { ready: true, accessToken: null, userId: null, + password: null, + localStorage: null, messages: [ { id: 'intro-msg-id', @@ -103,18 +105,35 @@ class ChatBox extends React.Component { handleExitChat = () => { if (this.state.client) { - this.leaveRoom() - .then(() => { - this.setState(initialState) - }) - .catch(err => console.log("Error leaving room", err)) + this.exitChat() } else { this.setState(initialState) } } - leaveRoom = () => { + exitChat = () => { + if (!this.state.client) return null; return this.state.client.leave(this.state.roomId) + .then(() => { + const auth = { + type: 'm.login.password', + // TODO: Remove `user` once servers support proper UIA + // See https://github.com/vector-im/riot-web/issues/10312 + user: this.state.userId, + identifier: { + type: "m.id.user", + user: this.state.userId, + }, + password: this.state.password, + }; + this.state.client.deactivateAccount(auth, true) + }) + .then(() => this.state.client.stopClient()) + .then(() => this.state.client.clearStores()) + .then(() => { + this.state.localStorage.clear() + this.setState(initialState) + }) } initializeChat = () => { @@ -150,7 +169,9 @@ class ChatBox extends React.Component { this.setState({ accessToken: data.access_token, userId: data.user_id, - username: username + username: username, + password: password, + localStorage: localStorage, }) // create new client with full options @@ -334,11 +355,13 @@ class ChatBox extends React.Component { componentDidMount() { document.addEventListener("keydown", this.handleEscape, false); + window.addEventListener('beforeunload', this.exitChat) } componentWillUnmount() { document.removeEventListener("keydown", this.handleEscape, false); - this.leaveRoom(); + window.removeEventListener('beforeunload', this.exitChat) + this.exitChat(); } handleInputChange = e => {