{"__v":2,"_id":"56d49a9da4a9211b00c8f118","category":{"__v":6,"_id":"53fe7d89addab8973c1af2b0","pages":["53fe7e17addab8973c1af2bb","53fe80ebaddab8973c1af2be","54011f0ec1c1764f69827203","53fe8159addab8973c1af2c1","53fe81ecaddab8973c1af2c5","54d94b3c6a09052100a6b699","56d49a9da4a9211b00c8f118"],"project":"53fe6dc5addab8973c1af267","version":"53fe6dc5addab8973c1af26a","sync":{"url":"","isSync":false},"reference":false,"createdAt":"2014-08-28T00:53:29.717Z","from_sync":false,"order":6,"slug":"logout-reference","title":"Logout Reference"},"parentDoc":null,"project":"53fe6dc5addab8973c1af267","user":"53fe6d8baddab8973c1af266","version":{"__v":19,"_id":"53fe6dc5addab8973c1af26a","project":"53fe6dc5addab8973c1af267","createdAt":"2014-08-27T23:46:13.941Z","releaseDate":"2014-08-27T23:46:13.941Z","categories":["53fe6dc5addab8973c1af26b","53fe71a2addab8973c1af276","53fe7d89addab8973c1af2b0","53fe7d8daddab8973c1af2b1","53fe836faddab8973c1af2ce","53ff9a4823a37e1d5cebafe1","53ff9e3723a37e1d5cebaff7","53ffaca523a37e1d5cebb039","53ffad2e23a37e1d5cebb03c","5400c7d2ec93b29b61d4f7be","5400f0e1ec93b29b61d4f7dd","54d5636323010a0d001aca81","54d565c1276f8e0d00feab54","54ff40532882a10d00546927","556606d25561af0d008208b7","558c91900b236c2500d37c9a","56180a14f8c9632100ac7599","564fb3a59b4fab1700187518","5702e2d2f2d6f336005e901f"],"is_deprecated":false,"is_hidden":false,"is_beta":false,"is_stable":true,"codename":"","version_clean":"1.0.0","version":"1.0"},"updates":[],"next":{"pages":[],"description":""},"createdAt":"2016-02-29T19:23:09.246Z","link_external":false,"link_url":"","githubsync":"","sync_unique":"","hidden":false,"api":{"results":{"codes":[]},"settings":"","auth":"required","params":[],"url":""},"isReference":false,"order":6,"body":"When a user logs out with Clef, they should be logged out of your site. To do this, you've likely already implemented [Database logout](doc:database-logout). With a basic implementation of database logout; however, the user will only see that they are logged out on the next page refresh, not automatically.  \n\nTo have a seamless logout experience, we recommend using websockets with a service like [Pusher](https://pusher.com) to notify the browser that the user has been logged out and redirect them to the appropriate page.\n\nHere's an example of what this might look like:\n[block:code]\n{\n  \"codes\": [\n    {\n      \"code\": \"<?php\\n// On the backend we have the Pusher object set up\\n$PUSHER_APP_ID = 'YOUR_APP_ID';\\n$PUSHER_APP_KEY = 'YOUR_APP_KEY';\\n$PUSHER_APP_SECRET = 'YOUR_APP_SECRET';\\n$pusher = new Pusher( $PUSHER_APP_KEY, $PUSHER_APP_SECRET, $PUSHER_APP_ID );\\n// then, when a user logs out via Clef, we trigger a logout message\\n// on their user ID push channel\\n$pusher->trigger($user->ID, 'logout', array(success => true));\\n\\n?>\\n\\n<?php\\n// then, in the frontend, we set up Pusher to listen for that message\\n// and redirect the user when it's heard\\nif (isset($user->clef_id)) { ?>\\n<script src=\\\"//js.pusher.com/2.2/pusher.min.js\\\"></script>\\n<script>\\n  var pusher = new Pusher(\\\"<?php echo $PUSHER_APP_ID ?>\\\");\\n  var channel = pusher.subscribe(\\\"<?php echo $user->ID ?>\\\");\\n  channel.bind('logout', function(data) { window.location.reload() });\\n</script>\\n<?php } ?>\\n\\n?>\",\n      \"language\": \"php\"\n    }\n  ]\n}\n[/block]","excerpt":"How to automatically refresh the page and log a user out when they log out with Clef","slug":"instant-logout-with-websockets","type":"basic","title":"Instant logout with websockets"}

Instant logout with websockets

How to automatically refresh the page and log a user out when they log out with Clef

When a user logs out with Clef, they should be logged out of your site. To do this, you've likely already implemented [Database logout](doc:database-logout). With a basic implementation of database logout; however, the user will only see that they are logged out on the next page refresh, not automatically. To have a seamless logout experience, we recommend using websockets with a service like [Pusher](https://pusher.com) to notify the browser that the user has been logged out and redirect them to the appropriate page. Here's an example of what this might look like: [block:code] { "codes": [ { "code": "<?php\n// On the backend we have the Pusher object set up\n$PUSHER_APP_ID = 'YOUR_APP_ID';\n$PUSHER_APP_KEY = 'YOUR_APP_KEY';\n$PUSHER_APP_SECRET = 'YOUR_APP_SECRET';\n$pusher = new Pusher( $PUSHER_APP_KEY, $PUSHER_APP_SECRET, $PUSHER_APP_ID );\n// then, when a user logs out via Clef, we trigger a logout message\n// on their user ID push channel\n$pusher->trigger($user->ID, 'logout', array(success => true));\n\n?>\n\n<?php\n// then, in the frontend, we set up Pusher to listen for that message\n// and redirect the user when it's heard\nif (isset($user->clef_id)) { ?>\n<script src=\"//js.pusher.com/2.2/pusher.min.js\"></script>\n<script>\n var pusher = new Pusher(\"<?php echo $PUSHER_APP_ID ?>\");\n var channel = pusher.subscribe(\"<?php echo $user->ID ?>\");\n channel.bind('logout', function(data) { window.location.reload() });\n</script>\n<?php } ?>\n\n?>", "language": "php" } ] } [/block]