{"_id":"56183be0a410c90d00c61408","parentDoc":null,"project":"53fe6dc5addab8973c1af267","version":{"_id":"53fe6dc5addab8973c1af26a","__v":19,"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"},"__v":15,"user":"53fe70beaddab8973c1af273","category":{"_id":"564fb3a59b4fab1700187518","version":"53fe6dc5addab8973c1af26a","project":"53fe6dc5addab8973c1af267","__v":0,"pages":[],"sync":{"url":"","isSync":false},"reference":false,"createdAt":"2015-11-20T23:58:29.588Z","from_sync":false,"order":1,"slug":"integration-guide","title":"Integration Guide"},"updates":[],"next":{"pages":[],"description":""},"createdAt":"2015-10-09T22:12:48.652Z","link_external":false,"link_url":"","githubsync":"","sync_unique":"","hidden":false,"api":{"settings":"","results":{"codes":[]},"auth":"required","params":[],"url":""},"isReference":false,"order":1,"body":"[block:callout]\n{\n  \"type\": \"warning\",\n  \"title\": \"Tutorial Requirements\",\n  \"body\": \"In order to finish this tutorial, you'll need to have already handled Clef's authentication handshake and registered new users in your database.\\n\\nIf you haven't, check out our [register new users docs](/v1.0/docs/register-new-users).\"\n}\n[/block]\nSo, you've handled the authentication handshake and written code to register new users who come through Clef — now what? \n\nNow, you'll handle looking up returning users in your database and granting them access to the parts of your site that need authentication.\n\n# Looking up returning users\n\nIn our last tutorial, we walked through how to register new users who come through Clef. If you followed along, you'll notice that we actually already handled looking up existing users to make sure we didn't register a user twice. Take another look at [the code there](http://docs.getclef.com/v1.0/docs/register-new-users#section-registering-a-new-user), and you'll be all set for the next section.\n\n# Logging users in\n\nOnce you've queried for an existing user or created a new one, you can mark them in a browser session and give them access to the members-only area of your site. \n\nIf you have an existing way to authenticate users on your site, you probably already have code to do this. If you don't, this is typically done by setting the user's ID in a signed cookie or server-side session. Then, when a user accesses a members-only area, you can grab the ID from the signed cookie or session and ensure that they are a valid user.\n[block:code]\n{\n  \"codes\": [\n    {\n      \"code\": \":::at:::app.route('/clef/handshake')\\ndef redirect_url_endpoint():\\n  # ... \\n  # In your redirect URL endpoint, you've already done Clef's handshake \\n  # and looked up or created a user:\\n  user = get_or_create_from_clef(clef_user_information)\\n\\n  # Now, just set the user's ID in a signed browser session\\n  session['user_id'] = user.id\\n\\treturn redirect(url_for('members_area'))\\n\\n@app.route('/members')\\ndef members_area():\\n  user = get_user_by_id(user_id = session.get('user_id'))\\n  if user:\\n    return render_template('members.html', user=user)\\n  else:\\n    abort(403)\",\n      \"language\": \"python\"\n    },\n    {\n      \"code\": \"app.get('/clef/handshake', function(req, res) {\\n\\t// ... \\n  // In your redirect URL endpoint, you've already done Clef's handshake \\n  // and looked up or created a user:\\n  getOrCreateUserFromClef(userInformation, function(user) {\\n    // Now, just set the user's ID in a signed browser session\\n  \\treq.session.userID = user.id;\\n    res.redirect('/members');\\n  });\\n});\\n\\napp.get('/members', function(req, res) {\\n\\tgetUserByID(req.session.userID, function(user) {\\n  \\tif (user) {\\n     \\tres.render('members', {user: user});\\n    } else {\\n     \\tres.status(403).send('Forbidden'); \\n    }\\n  });\\n});\",\n      \"language\": \"javascript\",\n      \"name\": \"Node\"\n    },\n    {\n      \"code\": \"In your redirect URL:\\n\\n<?php\\n\\tsession_start()\\n  session_regenerate_id(true);\\n\\n\\t// In your redirect URL endpoint, you've already done Clef's handshake \\n  // and looked up or created a user:\\n\\t$user = get_or_create_user_from_clef($user_information);\\n\\n\\t// Now, just set the user's ID in a session. If you're using a web framework \\n  // like Laravel or Lumen, you should used their signed cookie module.\\n\\t$_SESSION['user_id'] = $user->id;\\n\\n\\theader(\\\"Location: members_area.php\\\");\\n\\tdie();\\n?>\\n\\nIn members_area.php:\\n\\n<?php\\n\\tsession_start();\\n\\n  if(!isset($_SESSION[\\\"user_id\\\"])) {\\n    header(\\\"Location: index.php\\\");\\n    die();\\n  }\\n  $user = get_user_by_id($_SESSION[\\\"user_id\\\"]);\\n  if (!$user) header(\\\"Location: index.php\\\");\\n\\n\\t// Now show user information!\\n?>\",\n      \"language\": \"php\"\n    }\n  ]\n}\n[/block]\n# Next up\n\nAwesome! You've logged in users who come to your site through Clef.\n\nNext, you'll need to handle connecting users who already have an account on your site and now want to log in with Clef. Check out our guide on [connecting existing users](doc:connect-existing-users).\n[block:html]\n{\n  \"html\": \"<div></div>\\n<a class=\\\"clef-button blue\\\" href=\\\"/v1.0/docs/connect-existing-users\\\">See \\\"Connect existing users\\\"</a>\\n<style></style>\"\n}\n[/block]","excerpt":"In this tutorial, you'll learn how to log users in who come from Clef. If you need help after reading this, chat live with our developers in Clef's [community room](http://community.getclef.com).","slug":"log-your-users-in","type":"basic","title":"Log your users in"}

Log your users in

In this tutorial, you'll learn how to log users in who come from Clef. If you need help after reading this, chat live with our developers in Clef's [community room](http://community.getclef.com).

[block:callout] { "type": "warning", "title": "Tutorial Requirements", "body": "In order to finish this tutorial, you'll need to have already handled Clef's authentication handshake and registered new users in your database.\n\nIf you haven't, check out our [register new users docs](/v1.0/docs/register-new-users)." } [/block] So, you've handled the authentication handshake and written code to register new users who come through Clef — now what? Now, you'll handle looking up returning users in your database and granting them access to the parts of your site that need authentication. # Looking up returning users In our last tutorial, we walked through how to register new users who come through Clef. If you followed along, you'll notice that we actually already handled looking up existing users to make sure we didn't register a user twice. Take another look at [the code there](http://docs.getclef.com/v1.0/docs/register-new-users#section-registering-a-new-user), and you'll be all set for the next section. # Logging users in Once you've queried for an existing user or created a new one, you can mark them in a browser session and give them access to the members-only area of your site. If you have an existing way to authenticate users on your site, you probably already have code to do this. If you don't, this is typically done by setting the user's ID in a signed cookie or server-side session. Then, when a user accesses a members-only area, you can grab the ID from the signed cookie or session and ensure that they are a valid user. [block:code] { "codes": [ { "code": "@app.route('/clef/handshake')\ndef redirect_url_endpoint():\n # ... \n # In your redirect URL endpoint, you've already done Clef's handshake \n # and looked up or created a user:\n user = get_or_create_from_clef(clef_user_information)\n\n # Now, just set the user's ID in a signed browser session\n session['user_id'] = user.id\n\treturn redirect(url_for('members_area'))\n\n@app.route('/members')\ndef members_area():\n user = get_user_by_id(user_id = session.get('user_id'))\n if user:\n return render_template('members.html', user=user)\n else:\n abort(403)", "language": "python" }, { "code": "app.get('/clef/handshake', function(req, res) {\n\t// ... \n // In your redirect URL endpoint, you've already done Clef's handshake \n // and looked up or created a user:\n getOrCreateUserFromClef(userInformation, function(user) {\n // Now, just set the user's ID in a signed browser session\n \treq.session.userID = user.id;\n res.redirect('/members');\n });\n});\n\napp.get('/members', function(req, res) {\n\tgetUserByID(req.session.userID, function(user) {\n \tif (user) {\n \tres.render('members', {user: user});\n } else {\n \tres.status(403).send('Forbidden'); \n }\n });\n});", "language": "javascript", "name": "Node" }, { "code": "In your redirect URL:\n\n<?php\n\tsession_start()\n session_regenerate_id(true);\n\n\t// In your redirect URL endpoint, you've already done Clef's handshake \n // and looked up or created a user:\n\t$user = get_or_create_user_from_clef($user_information);\n\n\t// Now, just set the user's ID in a session. If you're using a web framework \n // like Laravel or Lumen, you should used their signed cookie module.\n\t$_SESSION['user_id'] = $user->id;\n\n\theader(\"Location: members_area.php\");\n\tdie();\n?>\n\nIn members_area.php:\n\n<?php\n\tsession_start();\n\n if(!isset($_SESSION[\"user_id\"])) {\n header(\"Location: index.php\");\n die();\n }\n $user = get_user_by_id($_SESSION[\"user_id\"]);\n if (!$user) header(\"Location: index.php\");\n\n\t// Now show user information!\n?>", "language": "php" } ] } [/block] # Next up Awesome! You've logged in users who come to your site through Clef. Next, you'll need to handle connecting users who already have an account on your site and now want to log in with Clef. Check out our guide on [connecting existing users](doc:connect-existing-users). [block:html] { "html": "<div></div>\n<a class=\"clef-button blue\" href=\"/v1.0/docs/connect-existing-users\">See \"Connect existing users\"</a>\n<style></style>" } [/block]