{"action":"created","comment":{"url":"https://api.github.com/repos/auth0/docs/pulls/comments/71360460","id":71360460,"diff_hunk":"@@ -0,0 +1,224 @@\n+---\n+title: Custom Login\n+description: Learn how to login using the Authentication API SDK instead of omniauth\n+---\n+\n+## Ruby On Rails - Custom Login\n+\n+<%= include('../../_includes/_package', {\n+  pkgRepo: 'omniauth-auth0',\n+  pkgBranch: 'master',\n+  pkgPath: 'examples/ruby-on-rails-webapp',\n+  pkgFilePath: null,\n+  pkgType: 'server'\n+}) %>\n+\n+[Login](/quickstart/webapp/rails/01-login) explains how to login using a widget called Lock and a gem called OmniAuth (and a specific Auth0 strategy for OmniAuth). You can build an application with Auth0 using your custom design without including neither Lock nor OmniAuth. In this section, you'll learn how to create a custom login form using the Auth0 Ruby SDK.\n+\n+### 1. Add dependencies\n+\n+Add the following dependencies to your `Gemfile` and run `bundle install`\n+\n+${snippet(meta.snippets.dependencies)}\n+\n+### 2. Create a login form in the home view\n+**NOTE**: `home` is not a mandatory name for the view / controller.\n+\n+```ruby\n+<div id=\"login-container\">\n+  ${ '<%= form_tag do %>' }\n+      ${ '<%= text_field_tag :user, \"\", class:\"form-control\" %>' }\n+      <br>\n+      ${ '<%= password_field_tag :password, \"\", class:\"form-control\"%>' }</p>\n+      <br>\n+      ${ '<%= submit_tag(\"Login\", class: \"btn btn-success btn-lg\") %>' }\n+      ${ '<%= submit_tag(\"Sign up\", name: \"signup\", class:\"btn btn-success btn-lg\") %>' }\n+  ${' <% end %> '}\n+  ${ '<%= link_to \"Login with Google\" , controller: :auth0, action: :google_authorize %>' }\n+</div>\n+```\n+\n+In order for this form to post its contents to the auth action of the home controller, you need to make this a valid route. To do so, add the following to your `config/routes.rb` file:\n+```ruby\n+post \"/\" => \"auth0#callback\"\n+```\n+\n+### 3. Create an auth action in the home controller\n+\n+Add the following actions in the `auth0` controller:\n+\n+```ruby\n+def callback\n+  begin\n+      if params[:signup]\n+        signup\n+      end\n+      session[:token_id] = login\n+    end\n+    redirect_to '/dashboard'\n+  rescue Auth0::Unauthorized\n+    redirect_to '/', notice: 'Invalid email or password'\n+  rescue => ex\n+    redirect_to '/', notice: ex.message\n+  end\n+end\n+\n+def failure\n+  @error_msg = request.params['message']\n+end\n+```\n+The `callback` controller action calls the `signup` or `login` actions, depending on the contents of the `params` hash. Upon successful execution of either, it redirects to `/dashboard`. In case of an exception, it redirects to `/` and flashes the exception message.\n+\n+Next, add the definitions for the `login` and `signup` actions under `private`:\n+\n+```ruby\n+def login\n+  token = client.login(\n+    params[:user],\n+    params[:password],\n+    authParams: {\n+     scope: 'openid name email'\n+    },\n+    connection:'Username-Password-Authentication'\n+  )\n+\n+  session[:token_id] = token\n+end\n+\n+def signup\n+  token = client.signup(\n+    params[:user],\n+    params[:password]\n+  )\n+end\n+```\n+\n+### 4. Add an SDK client instance in the Application Controller\n+In order to call the API methods using the Auth0 Ruby SDK, you need to initialize a client instance. Add the following code in your Application Controller (`controllers/application_controller.rb`):\n+\n+```ruby\n+def client\n+  creds = { client_id: ENV['AUTH0_CLIENT_ID'],\n+    client_secret: ENV['AUTH0_CLIENT_SECRET'],\n+    api_version: 1,\n+    domain: ENV['AUTH0_DOMAIN'] }\n+\n+  @client = Auth0Client.new(creds)\n+end\n+```\n+\n+### 5. Checking if the user is authenticated\n+In [Login](/quickstart/webapp/rails/01-login) you used a parent controller with a `before_action` to prevent users from executing actions in controllers that inherited from it. You will use the same strategy now, but the code will change slightly.\n+\n+Create a `SecuredController` class with the code below:\n+\n+```ruby\n+class SecuredController < ApplicationController\n+  before_action :logged_in?\n+\n+  private\n+\n+  def logged_in?\n+    unless session[:token_id].present?\n+      redirect_to '/'\n+    end\n+  end\n+end\n+```\n+\n+Then, update the `DashboardController` so that it inherits from `SecuredController`:\n+\n+```ruby\n+class DashboardController < SecuredController\n+```\n+\n+This way, if a user attempts to access the routes governed by the `DashboardController` actions, it will be redirected to `/`.\n+\n+### 6. Login with Google\n+\n+For your users to login using a Social Connection without using neither Lock nor OmniAuth, follow these steps:\n+\n+**NOTE**: Social connections support only browser based (passive) authentication. The providers don't allow entering the user/password in applications that don't belong to them, so the user gets redirected to the provider (e.g. Google sign in page). Three steps take place in this process: initiation, authentication, and getting the access token. You'll trigger the Initiation, Auth0 will take care of the Authentication, and then you'll obtain the access and id token for the user (*to learn more about this process go to: [Authentication API](/api/authentication#!%23get--authorize_social) and [OAuth Server Side](/protocols#oauth-server-side) documents*).\n+\n+For this particular example, you'll configure Google as your social provider. The API methods called by the API also support Facebook, Twitter and Weibo.\n+\n+**Prerequisite**: configure your Google Social Connection by following [this tutorial](/connections/social/google).\n+\n+To trigger initiation, you'll need to generate a redirect to the social provider. To do so, create the `google_authorize method` in the `Auth0Controller`:\n+\n+```ruby\n+def google_authorize\n+    redirect_to client.authorization_url(\n+      ENV['AUTH0_CALLBACK_URL'],\n+      {\n+        connection: 'google-oauth2',\n+        scope:'openid'\n+      }\n+    ).to_s\n+  end\n+```\n+\n+Next, add the actual link to the controller action in the home view:\n+\n+```ruby\n+${ '<%= link_to \"Login with Google\" , controller: :auth0, action: :google_authorize %>' }\n+```\n+And the corresponding route in the `routes` file:\n+\n+```ruby\n+get 'auth0/google_authorize' => 'auth0#google_authorize'\n+```\n+\n+The authentication will be handled by Auth0, the common visible part of this process is that the user is redirected to the identity provider site. When this process finishes, a request will be made to the callback URL, which will be handled by the application. You'll need to extract the access_code in this request and post a request to the Auth0 token endpoint using the SDK to get the access and id tokens.\n+\n+Add the following method to the `Auth0Controller`:\n+\n+```ruby\n+def google_login\n+  client.obtain_user_tokens(params['code'], ENV['AUTH0_CALLBACK_URL'] , 'google-oauth2', 'openid')['id_token']\n+end\n+```\n+\n+And call this method from the controller `callback` action","path":"articles/server-platforms/rails/02-custom-login.md","position":182,"original_position":182,"commit_id":"c7a3d63cb25b7983b02eff8505df98a8cc06a9e7","original_commit_id":"c7a3d63cb25b7983b02eff8505df98a8cc06a9e7","user":{"login":"mpaktiti","id":11715799,"avatar_url":"https://avatars.githubusercontent.com/u/11715799?v=3","gravatar_id":"","url":"https://api.github.com/users/mpaktiti","html_url":"https://github.com/mpaktiti","followers_url":"https://api.github.com/users/mpaktiti/followers","following_url":"https://api.github.com/users/mpaktiti/following{/other_user}","gists_url":"https://api.github.com/users/mpaktiti/gists{/gist_id}","starred_url":"https://api.github.com/users/mpaktiti/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/mpaktiti/subscriptions","organizations_url":"https://api.github.com/users/mpaktiti/orgs","repos_url":"https://api.github.com/users/mpaktiti/repos","events_url":"https://api.github.com/users/mpaktiti/events{/privacy}","received_events_url":"https://api.github.com/users/mpaktiti/received_events","type":"User","site_admin":false},"body":"Missing a fullstop.","created_at":"2016-07-19T15:21:10Z","updated_at":"2016-07-19T15:21:10Z","html_url":"https://github.com/auth0/docs/pull/1764#discussion_r71360460","pull_request_url":"https://api.github.com/repos/auth0/docs/pulls/1764","_links":{"self":{"href":"https://api.github.com/repos/auth0/docs/pulls/comments/71360460"},"html":{"href":"https://github.com/auth0/docs/pull/1764#discussion_r71360460"},"pull_request":{"href":"https://api.github.com/repos/auth0/docs/pulls/1764"}}},"pull_request":{"url":"https://api.github.com/repos/auth0/docs/pulls/1764","id":77021192,"html_url":"https://github.com/auth0/docs/pull/1764","diff_url":"https://github.com/auth0/docs/pull/1764.diff","patch_url":"https://github.com/auth0/docs/pull/1764.patch","issue_url":"https://api.github.com/repos/auth0/docs/issues/1764","number":1764,"state":"open","locked":false,"title":"Rails Quickstart - Sections 1 through 3","user":{"login":"fleko","id":1147022,"avatar_url":"https://avatars.githubusercontent.com/u/1147022?v=3","gravatar_id":"","url":"https://api.github.com/users/fleko","html_url":"https://github.com/fleko","followers_url":"https://api.github.com/users/fleko/followers","following_url":"https://api.github.com/users/fleko/following{/other_user}","gists_url":"https://api.github.com/users/fleko/gists{/gist_id}","starred_url":"https://api.github.com/users/fleko/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/fleko/subscriptions","organizations_url":"https://api.github.com/users/fleko/orgs","repos_url":"https://api.github.com/users/fleko/repos","events_url":"https://api.github.com/users/fleko/events{/privacy}","received_events_url":"https://api.github.com/users/fleko/received_events","type":"User","site_admin":false},"body":"Updates to **Rails Quickstart** doc\r\n- Updated section 01 - Login. Moved *Accessing User Information* to own section. \r\n- Added section 02 - Custom Login. Login using custom form, and social login using Ruby SDK\r\n- Added section 03 - User Profile. Access Normalized and Full User Profile using OmniAuth\r\n\r\n<!---\r\nNotes:\r\n- Your PR should conform to our [Contributing Guidelines](https://github.com/auth0/docs#contributing-guidelines)\r\n- If applicable, added details to the [update feed](https://github.com/auth0/docs/tree/master/updates)\r\n- Make sure your PR gets deployed to a Heroku [Review App](https://github.com/auth0/docs#review-apps) without errors.\r\n- Please include a short description\r\n- If your PR is a work in progress title it [DO NOT MERGE]\r\n--->\r\n","created_at":"2016-07-11T20:52:42Z","updated_at":"2016-07-19T15:21:10Z","closed_at":null,"merged_at":null,"merge_commit_sha":"de1e7a2be93176c130481a511ea1f2dd130f8421","assignee":null,"assignees":[],"milestone":null,"commits_url":"https://api.github.com/repos/auth0/docs/pulls/1764/commits","review_comments_url":"https://api.github.com/repos/auth0/docs/pulls/1764/comments","review_comment_url":"https://api.github.com/repos/auth0/docs/pulls/comments{/number}","comments_url":"https://api.github.com/repos/auth0/docs/issues/1764/comments","statuses_url":"https://api.github.com/repos/auth0/docs/statuses/c7a3d63cb25b7983b02eff8505df98a8cc06a9e7","head":{"label":"ignaciojonas:master","ref":"master","sha":"c7a3d63cb25b7983b02eff8505df98a8cc06a9e7","user":{"login":"ignaciojonas","id":874682,"avatar_url":"https://avatars.githubusercontent.com/u/874682?v=3","gravatar_id":"","url":"https://api.github.com/users/ignaciojonas","html_url":"https://github.com/ignaciojonas","followers_url":"https://api.github.com/users/ignaciojonas/followers","following_url":"https://api.github.com/users/ignaciojonas/following{/other_user}","gists_url":"https://api.github.com/users/ignaciojonas/gists{/gist_id}","starred_url":"https://api.github.com/users/ignaciojonas/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/ignaciojonas/subscriptions","organizations_url":"https://api.github.com/users/ignaciojonas/orgs","repos_url":"https://api.github.com/users/ignaciojonas/repos","events_url":"https://api.github.com/users/ignaciojonas/events{/privacy}","received_events_url":"https://api.github.com/users/ignaciojonas/received_events","type":"User","site_admin":false},"repo":{"id":62226592,"name":"docs","full_name":"ignaciojonas/docs","owner":{"login":"ignaciojonas","id":874682,"avatar_url":"https://avatars.githubusercontent.com/u/874682?v=3","gravatar_id":"","url":"https://api.github.com/users/ignaciojonas","html_url":"https://github.com/ignaciojonas","followers_url":"https://api.github.com/users/ignaciojonas/followers","following_url":"https://api.github.com/users/ignaciojonas/following{/other_user}","gists_url":"https://api.github.com/users/ignaciojonas/gists{/gist_id}","starred_url":"https://api.github.com/users/ignaciojonas/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/ignaciojonas/subscriptions","organizations_url":"https://api.github.com/users/ignaciojonas/orgs","repos_url":"https://api.github.com/users/ignaciojonas/repos","events_url":"https://api.github.com/users/ignaciojonas/events{/privacy}","received_events_url":"https://api.github.com/users/ignaciojonas/received_events","type":"User","site_admin":false},"private":false,"html_url":"https://github.com/ignaciojonas/docs","description":"Auth0 documentation","fork":true,"url":"https://api.github.com/repos/ignaciojonas/docs","forks_url":"https://api.github.com/repos/ignaciojonas/docs/forks","keys_url":"https://api.github.com/repos/ignaciojonas/docs/keys{/key_id}","collaborators_url":"https://api.github.com/repos/ignaciojonas/docs/collaborators{/collaborator}","teams_url":"https://api.github.com/repos/ignaciojonas/docs/teams","hooks_url":"https://api.github.com/repos/ignaciojonas/docs/hooks","issue_events_url":"https://api.github.com/repos/ignaciojonas/docs/issues/events{/number}","events_url":"https://api.github.com/repos/ignaciojonas/docs/events","assignees_url":"https://api.github.com/repos/ignaciojonas/docs/assignees{/user}","branches_url":"https://api.github.com/repos/ignaciojonas/docs/branches{/branch}","tags_url":"https://api.github.com/repos/ignaciojonas/docs/tags","blobs_url":"https://api.github.com/repos/ignaciojonas/docs/git/blobs{/sha}","git_tags_url":"https://api.github.com/repos/ignaciojonas/docs/git/tags{/sha}","git_refs_url":"https://api.github.com/repos/ignaciojonas/docs/git/refs{/sha}","trees_url":"https://api.github.com/repos/ignaciojonas/docs/git/trees{/sha}","statuses_url":"https://api.github.com/repos/ignaciojonas/docs/statuses/{sha}","languages_url":"https://api.github.com/repos/ignaciojonas/docs/languages","stargazers_url":"https://api.github.com/repos/ignaciojonas/docs/stargazers","contributors_url":"https://api.github.com/repos/ignaciojonas/docs/contributors","subscribers_url":"https://api.github.com/repos/ignaciojonas/docs/subscribers","subscription_url":"https://api.github.com/repos/ignaciojonas/docs/subscription","commits_url":"https://api.github.com/repos/ignaciojonas/docs/commits{/sha}","git_commits_url":"https://api.github.com/repos/ignaciojonas/docs/git/commits{/sha}","comments_url":"https://api.github.com/repos/ignaciojonas/docs/comments{/number}","issue_comment_url":"https://api.github.com/repos/ignaciojonas/docs/issues/comments{/number}","contents_url":"https://api.github.com/repos/ignaciojonas/docs/contents/{+path}","compare_url":"https://api.github.com/repos/ignaciojonas/docs/compare/{base}...{head}","merges_url":"https://api.github.com/repos/ignaciojonas/docs/merges","archive_url":"https://api.github.com/repos/ignaciojonas/docs/{archive_format}{/ref}","downloads_url":"https://api.github.com/repos/ignaciojonas/docs/downloads","issues_url":"https://api.github.com/repos/ignaciojonas/docs/issues{/number}","pulls_url":"https://api.github.com/repos/ignaciojonas/docs/pulls{/number}","milestones_url":"https://api.github.com/repos/ignaciojonas/docs/milestones{/number}","notifications_url":"https://api.github.com/repos/ignaciojonas/docs/notifications{?since,all,participating}","labels_url":"https://api.github.com/repos/ignaciojonas/docs/labels{/name}","releases_url":"https://api.github.com/repos/ignaciojonas/docs/releases{/id}","deployments_url":"https://api.github.com/repos/ignaciojonas/docs/deployments","created_at":"2016-06-29T13:09:05Z","updated_at":"2016-07-14T19:15:58Z","pushed_at":"2016-07-19T14:53:43Z","git_url":"git://github.com/ignaciojonas/docs.git","ssh_url":"git@github.com:ignaciojonas/docs.git","clone_url":"https://github.com/ignaciojonas/docs.git","svn_url":"https://github.com/ignaciojonas/docs","homepage":null,"size":240775,"stargazers_count":0,"watchers_count":0,"language":"GCC Machine Description","has_issues":false,"has_downloads":true,"has_wiki":false,"has_pages":false,"forks_count":0,"mirror_url":null,"open_issues_count":1,"forks":0,"open_issues":1,"watchers":0,"default_branch":"master"}},"base":{"label":"auth0:master","ref":"master","sha":"2f18afc7b0e206544747f7bb0a4d8095fdf3a3b5","user":{"login":"auth0","id":2824157,"avatar_url":"https://avatars.githubusercontent.com/u/2824157?v=3","gravatar_id":"","url":"https://api.github.com/users/auth0","html_url":"https://github.com/auth0","followers_url":"https://api.github.com/users/auth0/followers","following_url":"https://api.github.com/users/auth0/following{/other_user}","gists_url":"https://api.github.com/users/auth0/gists{/gist_id}","starred_url":"https://api.github.com/users/auth0/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/auth0/subscriptions","organizations_url":"https://api.github.com/users/auth0/orgs","repos_url":"https://api.github.com/users/auth0/repos","events_url":"https://api.github.com/users/auth0/events{/privacy}","received_events_url":"https://api.github.com/users/auth0/received_events","type":"Organization","site_admin":false},"repo":{"id":33872272,"name":"docs","full_name":"auth0/docs","owner":{"login":"auth0","id":2824157,"avatar_url":"https://avatars.githubusercontent.com/u/2824157?v=3","gravatar_id":"","url":"https://api.github.com/users/auth0","html_url":"https://github.com/auth0","followers_url":"https://api.github.com/users/auth0/followers","following_url":"https://api.github.com/users/auth0/following{/other_user}","gists_url":"https://api.github.com/users/auth0/gists{/gist_id}","starred_url":"https://api.github.com/users/auth0/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/auth0/subscriptions","organizations_url":"https://api.github.com/users/auth0/orgs","repos_url":"https://api.github.com/users/auth0/repos","events_url":"https://api.github.com/users/auth0/events{/privacy}","received_events_url":"https://api.github.com/users/auth0/received_events","type":"Organization","site_admin":false},"private":false,"html_url":"https://github.com/auth0/docs","description":"Auth0 documentation","fork":false,"url":"https://api.github.com/repos/auth0/docs","forks_url":"https://api.github.com/repos/auth0/docs/forks","keys_url":"https://api.github.com/repos/auth0/docs/keys{/key_id}","collaborators_url":"https://api.github.com/repos/auth0/docs/collaborators{/collaborator}","teams_url":"https://api.github.com/repos/auth0/docs/teams","hooks_url":"https://api.github.com/repos/auth0/docs/hooks","issue_events_url":"https://api.github.com/repos/auth0/docs/issues/events{/number}","events_url":"https://api.github.com/repos/auth0/docs/events","assignees_url":"https://api.github.com/repos/auth0/docs/assignees{/user}","branches_url":"https://api.github.com/repos/auth0/docs/branches{/branch}","tags_url":"https://api.github.com/repos/auth0/docs/tags","blobs_url":"https://api.github.com/repos/auth0/docs/git/blobs{/sha}","git_tags_url":"https://api.github.com/repos/auth0/docs/git/tags{/sha}","git_refs_url":"https://api.github.com/repos/auth0/docs/git/refs{/sha}","trees_url":"https://api.github.com/repos/auth0/docs/git/trees{/sha}","statuses_url":"https://api.github.com/repos/auth0/docs/statuses/{sha}","languages_url":"https://api.github.com/repos/auth0/docs/languages","stargazers_url":"https://api.github.com/repos/auth0/docs/stargazers","contributors_url":"https://api.github.com/repos/auth0/docs/contributors","subscribers_url":"https://api.github.com/repos/auth0/docs/subscribers","subscription_url":"https://api.github.com/repos/auth0/docs/subscription","commits_url":"https://api.github.com/repos/auth0/docs/commits{/sha}","git_commits_url":"https://api.github.com/repos/auth0/docs/git/commits{/sha}","comments_url":"https://api.github.com/repos/auth0/docs/comments{/number}","issue_comment_url":"https://api.github.com/repos/auth0/docs/issues/comments{/number}","contents_url":"https://api.github.com/repos/auth0/docs/contents/{+path}","compare_url":"https://api.github.com/repos/auth0/docs/compare/{base}...{head}","merges_url":"https://api.github.com/repos/auth0/docs/merges","archive_url":"https://api.github.com/repos/auth0/docs/{archive_format}{/ref}","downloads_url":"https://api.github.com/repos/auth0/docs/downloads","issues_url":"https://api.github.com/repos/auth0/docs/issues{/number}","pulls_url":"https://api.github.com/repos/auth0/docs/pulls{/number}","milestones_url":"https://api.github.com/repos/auth0/docs/milestones{/number}","notifications_url":"https://api.github.com/repos/auth0/docs/notifications{?since,all,participating}","labels_url":"https://api.github.com/repos/auth0/docs/labels{/name}","releases_url":"https://api.github.com/repos/auth0/docs/releases{/id}","deployments_url":"https://api.github.com/repos/auth0/docs/deployments","created_at":"2015-04-13T14:09:26Z","updated_at":"2016-07-14T20:33:03Z","pushed_at":"2016-07-19T14:58:27Z","git_url":"git://github.com/auth0/docs.git","ssh_url":"git@github.com:auth0/docs.git","clone_url":"https://github.com/auth0/docs.git","svn_url":"https://github.com/auth0/docs","homepage":null,"size":249034,"stargazers_count":39,"watchers_count":39,"language":"GCC Machine Description","has_issues":true,"has_downloads":true,"has_wiki":false,"has_pages":false,"forks_count":280,"mirror_url":null,"open_issues_count":200,"forks":280,"open_issues":200,"watchers":39,"default_branch":"master"}},"_links":{"self":{"href":"https://api.github.com/repos/auth0/docs/pulls/1764"},"html":{"href":"https://github.com/auth0/docs/pull/1764"},"issue":{"href":"https://api.github.com/repos/auth0/docs/issues/1764"},"comments":{"href":"https://api.github.com/repos/auth0/docs/issues/1764/comments"},"review_comments":{"href":"https://api.github.com/repos/auth0/docs/pulls/1764/comments"},"review_comment":{"href":"https://api.github.com/repos/auth0/docs/pulls/comments{/number}"},"commits":{"href":"https://api.github.com/repos/auth0/docs/pulls/1764/commits"},"statuses":{"href":"https://api.github.com/repos/auth0/docs/statuses/c7a3d63cb25b7983b02eff8505df98a8cc06a9e7"}}}}
{"actor":{"display_login":"mpaktiti"}}