Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 13 additions & 2 deletions frontend/logout.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,12 @@ window.addEventListener('DOMContentLoaded', () => {
}

console.log("Logging out all chatrix sessions");
logoutAndDeleteData().catch(error => console.log(error));
logoutAndDeleteData().then(() => {
// Logout has been done and data has been deleted.
// We can now expire the logout cookie.
const now = (new Date).toUTCString();
document.cookie = `${logoutCookieName}=false; expires=${now};path=/;`;
}).catch(error => console.error(error));
});

async function logoutAndDeleteData() {
Expand Down Expand Up @@ -53,10 +58,16 @@ async function logoutSession(session) {
headers: {
'Authorization': 'Bearer ' + session.accessToken,
},
}).then((response) => {
// Fetch considers 403 and 404 to be a success but to us, it's a failure.
if (!response.ok) {
throw response;
}
return response;
});

promise.catch(error => {
console.log(`Failed to logout chatrix session. deviceId: ${session.deviceId}`, error);
console.error(`Failed to logout chatrix session. deviceId: ${session.deviceId}`);
});

return promise;
Expand Down
4 changes: 3 additions & 1 deletion src/Sessions/logout.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,16 @@

namespace Automattic\Chatrix\Sessions;

use const Automattic\Chatrix\LOGOUT_COOKIE_NAME;

function init_logout() {
// Set a cookie when user logs out.
add_action(
'wp_logout',
function () {
// Expire in 10 minutes.
$expiration = time() + ( 10 * 60 );
setcookie( 'chatrix-logout', 'true', $expiration, COOKIEPATH, COOKIE_DOMAIN );
setcookie( LOGOUT_COOKIE_NAME, 'true', $expiration, COOKIEPATH, COOKIE_DOMAIN );
}
);
}
2 changes: 2 additions & 0 deletions src/plugin.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
const SCRIPT_HANDLE_APP = 'chatrix-app';
const SCRIPT_HANDLE_LOGOUT = 'chatrix-logout';
const CONFIG_VARIABLE = 'ChatrixConfig';
const LOGOUT_COOKIE_NAME = 'chatrix-logout';

function main() {
init_logout();
Expand Down Expand Up @@ -51,6 +52,7 @@ function () {
automattic_chatrix_version(),
false
);

wp_enqueue_script( SCRIPT_HANDLE_LOGOUT );
}
}
Expand Down