{"__v":11,"_id":"53fe80ebaddab8973c1af2be","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"},"comments":[],"is_link":false,"parentDoc":null,"project":"53fe6dc5addab8973c1af267","tags":[],"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":["53fe831daddab8973c1af2cd","54aa91323b56130b0056c8d9"],"next":{"pages":[],"description":""},"createdAt":"2014-08-28T01:07:55.173Z","link_external":false,"link_url":"","githubsync":"","sync_unique":"","hidden":false,"api":{"results":{"codes":[]},"settings":"","try":true,"auth":"never","params":[],"url":""},"isReference":false,"order":1,"body":"Handling the logout webhook is very similar to the OAuth handshake [we just implemented for login](/v1.0/docs/authenticating-users).\n\nThe basic idea is simple: the Clef servers hit your logout webhook with a logout token, which you then exchange for a user ID. With that user ID in hand, you can invalidate the user session through a database logout.\n\nCheck out the example code to the right to see the basic process, or clone one of our sample applications to get started.\n[block:code]\n{\n  \"codes\": [\n    {\n      \"code\": \"require 'httparty'\\nrequire 'json'\\n\\nAPP_ID = '6f8fb6e642924a5e9e7deacf35292abf'\\nAPP_SECRET = '27788a6c0934331258651af709813ba2'\\n\\ntoken = params[:logout_token]\\n\\ndata = {\\n    body: {\\n        logout_token: token,\\n        app_id: APP_ID,\\n        app_secret: APP_SECRET\\n    }\\n}\\n\\nurl = \\\"https://clef.io/api/v1/logout\\\"\\n\\nresponse = HTTParty.post(url, data)\\n\\nif response['success']\\n \\t  clef_id = response['clef_id']\\nelse\\n    p response['error']\\nend\",\n      \"language\": \"ruby\"\n    },\n    {\n      \"code\": \"from flask import Flask, request\\nimport requests\\nimport json\\n\\napp = Flask(__name__)\\n\\nAPP_ID = '8c9253dca23777745c9102e0be99ea70'\\nAPP_SECRET = 'a9a356f16c77bdcddf15f0a1c407dd3a'\\n\\n:::at:::app.route(\\\"/logout\\\", methods=[\\\"POST\\\"])\\ndef logout():\\n    logout_token = request.form.get(\\n        \\\"logout_token\\\",\\n        None\\n    )\\n\\n    url = \\\"https://clef.io/api/v1/logout\\\"\\n\\n    data = dict(\\n        logout_token=logout_token,\\n        app_id=APP_ID,\\n        app_secret=APP_SECRET\\n    )\\n\\n    response = requests.post(url, data=data)\\n\\n    if response.status == 200:\\n        json_response = json.loads(response.text)\\n        # {\\n        #   clef_id: 172632085,\\n        #   success: true\\n        # }\\n        clef_id = json_response['clef_id']\\n    else:\\n        print json_response['error']\\n\\nif __name__ == \\\"__main__\\\":\\n    app.run()\\n                  \",\n      \"language\": \"python\"\n    },\n    {\n      \"code\": \"<?php\\n\\nif (isset($_POST['logout_token'])) {\\n    $app_id = '8c9253dca23777745c9102e0be99ea70';\\n    $app_secret = 'a9a356f16c77bdcddf15f0a1c407dd3a';\\n\\n    $url = \\\"https://clef.io/api/v1/logout\\\";\\n\\n    $postdata = http_build_query(\\n        array(\\n            'logout_token' => $_POST['logout_token'],\\n            'app_id' => $app_id,\\n            'app_secret' => $app_secret\\n        )\\n    );\\n\\n    $opts = array('http' =>\\n        array(\\n            'method'  => 'POST',\\n            'header'  => 'Content-type: application/x-www-form-urlencoded',\\n            'content' => $postdata\\n        )\\n    );\\n\\n    $context  = stream_context_create($opts);\\n    $response = file_get_contents($url, false, $context);\\n    $response = json_decode($response, true);\\n\\n    if($response && $response['success']) {\\n        // {\\n        //   clef_id: 172632085,\\n        //   success: true\\n        // }\\n\\n        $clef_id = $response['clef_id'];\\n    }\\n    else {\\n        echo $response['error'];\\n    }\\n}\\n\",\n      \"language\": \"php\"\n    }\n  ]\n}\n[/block]\n# Next up\nOnce you've exchange the logout token for a user ID, you're ready to [log the user out](/v1.0/docs/database-logout).\n[block:html]\n{\n  \"html\": \"<div></div>\\n<a class=\\\"clef-button blue\\\" href=\\\"/v1.0/docs/database-logout\\\">Go to the next section</a>\\n<style></style>\"\n}\n[/block]","excerpt":"","slug":"handling-the-logout-webhook","type":"basic","title":"Handling the webhook"}

Handling the webhook


Handling the logout webhook is very similar to the OAuth handshake [we just implemented for login](/v1.0/docs/authenticating-users). The basic idea is simple: the Clef servers hit your logout webhook with a logout token, which you then exchange for a user ID. With that user ID in hand, you can invalidate the user session through a database logout. Check out the example code to the right to see the basic process, or clone one of our sample applications to get started. [block:code] { "codes": [ { "code": "require 'httparty'\nrequire 'json'\n\nAPP_ID = '6f8fb6e642924a5e9e7deacf35292abf'\nAPP_SECRET = '27788a6c0934331258651af709813ba2'\n\ntoken = params[:logout_token]\n\ndata = {\n body: {\n logout_token: token,\n app_id: APP_ID,\n app_secret: APP_SECRET\n }\n}\n\nurl = \"https://clef.io/api/v1/logout\"\n\nresponse = HTTParty.post(url, data)\n\nif response['success']\n \t clef_id = response['clef_id']\nelse\n p response['error']\nend", "language": "ruby" }, { "code": "from flask import Flask, request\nimport requests\nimport json\n\napp = Flask(__name__)\n\nAPP_ID = '8c9253dca23777745c9102e0be99ea70'\nAPP_SECRET = 'a9a356f16c77bdcddf15f0a1c407dd3a'\n\n@app.route(\"/logout\", methods=[\"POST\"])\ndef logout():\n logout_token = request.form.get(\n \"logout_token\",\n None\n )\n\n url = \"https://clef.io/api/v1/logout\"\n\n data = dict(\n logout_token=logout_token,\n app_id=APP_ID,\n app_secret=APP_SECRET\n )\n\n response = requests.post(url, data=data)\n\n if response.status == 200:\n json_response = json.loads(response.text)\n # {\n # clef_id: 172632085,\n # success: true\n # }\n clef_id = json_response['clef_id']\n else:\n print json_response['error']\n\nif __name__ == \"__main__\":\n app.run()\n ", "language": "python" }, { "code": "<?php\n\nif (isset($_POST['logout_token'])) {\n $app_id = '8c9253dca23777745c9102e0be99ea70';\n $app_secret = 'a9a356f16c77bdcddf15f0a1c407dd3a';\n\n $url = \"https://clef.io/api/v1/logout\";\n\n $postdata = http_build_query(\n array(\n 'logout_token' => $_POST['logout_token'],\n 'app_id' => $app_id,\n 'app_secret' => $app_secret\n )\n );\n\n $opts = array('http' =>\n array(\n 'method' => 'POST',\n 'header' => 'Content-type: application/x-www-form-urlencoded',\n 'content' => $postdata\n )\n );\n\n $context = stream_context_create($opts);\n $response = file_get_contents($url, false, $context);\n $response = json_decode($response, true);\n\n if($response && $response['success']) {\n // {\n // clef_id: 172632085,\n // success: true\n // }\n\n $clef_id = $response['clef_id'];\n }\n else {\n echo $response['error'];\n }\n}\n", "language": "php" } ] } [/block] # Next up Once you've exchange the logout token for a user ID, you're ready to [log the user out](/v1.0/docs/database-logout). [block:html] { "html": "<div></div>\n<a class=\"clef-button blue\" href=\"/v1.0/docs/database-logout\">Go to the next section</a>\n<style></style>" } [/block]