<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0" xmlns:cc="http://cyber.law.harvard.edu/rss/creativeCommonsRssModule.html">
    <channel>
        <title><![CDATA[Stories by Christopher Mark on Medium]]></title>
        <description><![CDATA[Stories by Christopher Mark on Medium]]></description>
        <link>https://medium.com/@chdmark?source=rss-ce18d6369cbf------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/0*FefWFaA_Tq4H2g3l.jpg</url>
            <title>Stories by Christopher Mark on Medium</title>
            <link>https://medium.com/@chdmark?source=rss-ce18d6369cbf------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Sun, 29 Nov 2026 22:48:44 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@chdmark/feed" rel="self" type="application/rss+xml"/>
        <webMaster><![CDATA[yourfriends@medium.com]]></webMaster>
        <atom:link href="http://medium.superfeedr.com/" rel="hub"/>
        <item>
            <title><![CDATA[Facebook OAuth with Rails]]></title>
            <link>https://medium.com/@chdmark/facebook-oauth-with-rails-c1113238df57?source=rss-ce18d6369cbf------2</link>
            <guid isPermaLink="false">https://medium.com/p/c1113238df57</guid>
            <category><![CDATA[omniauth]]></category>
            <category><![CDATA[facebook-oauth]]></category>
            <category><![CDATA[rails]]></category>
            <dc:creator><![CDATA[Christopher Mark]]></dc:creator>
            <pubDate>Sun, 10 Jan 2016 02:33:38 GMT</pubDate>
            <atom:updated>2016-01-12T02:22:36.667Z</atom:updated>
            <content:encoded><![CDATA[<p>By: <a href="https://medium.com/u/f7eb39694803">Eunice Choi</a> &amp; <a href="https://medium.com/u/ce18d6369cbf">Christopher Mark</a></p><p>When adding Facebook Login to our most recent project, <a href="https://accountabuddy.herokuapp.com/">Accountabuddy</a>, we noticed that there lacks a step-by-step and complete tutorial on using the OmniAuth gem. Here we show you how you can implement Facebook login to your Rails app. (note to mention: we did not use Devise).</p><h4><strong>1. Setting Up your App on Facebook</strong></h4><p>First, you will need to register your app through the <a href="http://developers.facebook.com/">Facebook for Developers</a>. If logged in, you will see the option to <strong>Add a New App</strong> under <strong>My Apps.</strong></p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*gDrfqE3daiOm8b85qWqgqg.jpeg" /></figure><p>This will open a box to select the type of platform for your app. After selection, you should see the following page to type in your app name:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*PbHK8jKIanMnlQ8MnA6hgA.png" /></figure><p>After you create your app id, you will be taken a new page with a field to input your site URL. For now, enter <a href="http://localhost:3000/"><strong>http://localhost:3000/</strong></a></p><figure><img alt="" src="https://cdn-images-1.medium.com/max/968/1*TPcd-Ouunask704DfV4q2g.png" /></figure><p>You now have access to your dashboard<strong> </strong>where you can access your <strong>App ID </strong>and <strong>App Secret</strong>, along with other information.</p><h4>2. Storing your Keys via Figaro</h4><p>(Refer to Andrei Istratii’s <a href="http://guides.railsgirls.md/day-3-part-2-login-with-facebook/">article </a>for reference)</p><p>Now that you have your secret IDs, you will want to store them in a private file somewhere in your Rails app. We went with the <a href="https://github.com/laserlemon/figaro"><strong>Figaro gem</strong></a>, which helps manage secret API keys while being Heroku-friendly.</p><p>Add a newline to your Gemfile:</p><pre>gem &#39;figaro&#39;</pre><p>Once added, run the bundle install in your command line:</p><pre>$ bundle install</pre><p>Also, run ‘figaro install’ in your command line:</p><pre>$ figaro install</pre><p>This creates an <strong>application.yml</strong> file in your <strong>config</strong> folder and will add it to the <strong>.gitignore </strong>file so that secrets aren’t uploaded in a public repo.</p><p>Now, open <strong>config/application.yml</strong> and type in:</p><pre>FACEBOOK_APP_ID: &quot;123123123&quot;</pre><pre>FACEBOOK_APP_SECRET: &quot;abcde1a2bcdef3abc4a56789abcdef&quot;</pre><p>In your <strong>config/secrets.yml, </strong>you should see something like this:</p><pre>development:<br> secret_key_base: a8d5a5706cf53...<br> facebook_app_id: &lt;%= ENV[“FACEBOOK_APP_ID”] %&gt;<br> facebook_app_secret: &lt;%= ENV[“FACEBOOK_APP_SECRET”] %&gt;</pre><pre>test:<br> secret_key_base: 432cb5d950462...<br> facebook_app_id: &lt;%= ENV[“FACEBOOK_APP_ID”] %&gt;<br> facebook_app_secret: &lt;%= ENV[“FACEBOOK_APP_SECRET”] %&gt;</pre><pre>production:<br> secret_key_base: &lt;%= ENV[“SECRET_KEY_BASE”] %&gt;<br> facebook_app_id: &lt;%= ENV[“FACEBOOK_APP_ID”] %&gt;<br> facebook_app_secret: &lt;%= ENV[“FACEBOOK_APP_SECRET”] %&gt;</pre><p>Congrats! You have successfully hidden your keys.</p><h4>3. Implementing OmniAuth Facebook Gem</h4><p>There is one more gem that you will need to install.</p><p>Add ‘omniauth-facebook’ to your Gemfile:</p><pre>gem &#39;omniauth-facebook&#39; </pre><p>and bundle install in your command line:</p><pre>$ bundle install</pre><h4>4. Updating the User Table</h4><p>In order to apply Facebook OAuth for your users, you will need to update your User migration table.</p><p>In your command line, type in:</p><pre>$ rails generate migration AddColumnsToUsers provider:string, uid:string</pre><p>This will add <strong>provider </strong>and <strong>uid </strong>columns to the user table. The provider specifies the type OAuth while the uid specifies the unique ID of the user. The new migration file should look like this:</p><pre>class AddColumnsToUsers &lt; ActiveRecord::Migration<br> def change<br>   add_column :users, :provider, :string<br>   add_column :users, :uid, :string<br> end<br>end</pre><p>Now, migrate.</p><pre>$ rake db:migrate</pre><h4>5. Configuring OmniAuth</h4><p>When you installed the omniauth-facebook gem, it created the following file: <strong>config/initializers/omniauth.rb</strong></p><p>Update the <strong>omniauth.rb</strong>, so that it looks like this</p><p>OmniAuth.config.logger = Rails.logger</p><pre>Rails.application.config.middleware.use OmniAuth::Builder do<br> provider :facebook, Rails.application.secrets.facebook_app_id,<br> Rails.application.secrets.facebook_app_secret, scope: ‘public_profile’, info_fields: ‘id,name,link’<br>end</pre><p>You can edit the scope and info_fields depending on the level of access you have in your application. Explore <a href="https://developers.facebook.com/docs/facebook-login/permissions">https://developers.facebook.com/docs/facebook-login/permissions</a> to view different permission options. Be aware that you might have to request permissions for certain levels.</p><h4>6. Updating the User Model</h4><p>You will now update the User model so that your app can save a new user to the database or login if that user already exists. The following method will check for this:</p><pre>def self.create_with_omniauth(auth)</pre><pre> user = User.where(<br>  uid: auth[‘uid’], <br>  provider: auth[‘provider’]<br> ).first_or_create(<br>  email: “#{auth[‘uid’]}@#{auth[‘provider’]}.com”,<br>  password: auth[‘uid’],<br>  name: auth[&#39;info&#39;][&#39;name&#39;]<br> )</pre><pre> if user.persisted?<br>   user<br> else<br>   user.save!<br>   user<br> end</pre><pre>end</pre><p>Let’s break down what is happening here.</p><pre>user = User.where(uid: auth[‘uid’], provider: auth[‘provider’])</pre><p>The ActiveRecord method <strong>where</strong> will search for a user with the <strong>uid </strong>and <strong>provider</strong>. The method <strong>first_or_create </strong>will set the existing user to a variable called user or create a new user if the user does not exist. (Credits to <a href="https://medium.com/u/c70d1096f48f">Dylan Drop</a> for this suggestion). You can update your user attributes to fit your User table. For now, we just save the email, password, and name.</p><pre>if user.persisted?<br>   user<br> else<br>   user.save!<br>   user<br> end</pre><p>The second half of the method is a simple control flow</p><ul><li>If the user persisted, return user</li><li>Else, save new user in the database and return user</li></ul><h4>7. Configuring your routes.rb</h4><p>Let’s update your <strong>routes.rb </strong>so that when a user signs in via Facebook, your application knows to direct to the <strong>session#create</strong> route.</p><p>Add the following get routes to your <strong>config/routes.rb</strong></p><pre>Rails.application.routes.draw do<br>  get ‘auth/facebook/callback’, to: ‘sessions#create’<br>  get ‘auth/failure’, to: redirect(‘/’)<br>end</pre><h4>8. Updating your Sessions Controller</h4><p>One last thing!</p><p>You will need to add logic to your create route in your sessions controller. Now that you’ve implemented Facebook OAuth, you want your users to be able to login via Facebook <strong>as well as</strong> your application’s own login system.</p><p>We have created a simple create route for you. In your sessions controller, add the following:</p><pre>def create<br> if request.env[‘omniauth.auth’]<br>   user = User.create_with_omniauth(request.env[‘omniauth.auth’])<br>   session[:user_id] = user.id<br>   redirect_to edit_user_path(user.id)<br> else<br>   user = User.find_by_email(params[:email])<br>   user &amp;&amp; user.authenticate(params[:password])<br>   session[:user_id] = user.id<br>   redirect_to user_path(user.id)<br>  end<br> end<br> </pre><p>and…that’s it!</p><iframe src="https://cdn.embedly.com/widgets/media.html?src=https%3A%2F%2Fgiphy.com%2Fembed%2FAwLhdQZ229gRi%2Ftwitter%2Fiframe&amp;src_secure=1&amp;url=http%3A%2F%2Fgiphy.com%2Fgifs%2Fvintage-80s-1980s-AwLhdQZ229gRi&amp;image=https%3A%2F%2Fmedia.giphy.com%2Fmedia%2FAwLhdQZ229gRi%2Fgiphy.gif&amp;key=d04bfffea46d4aeda930ec88cc64b87c&amp;type=text%2Fhtml&amp;schema=giphy" width="435" height="326" frameborder="0" scrolling="no"><a href="https://medium.com/media/34f1506bf7b974b5dcefcf6f3d4ace9d/href">https://medium.com/media/34f1506bf7b974b5dcefcf6f3d4ace9d/href</a></iframe><p>Yay! You have successfully implemented Facebook login to your Rails application. We hope you found this tutorial very useful. Please feel free to share your thoughts below!</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=c1113238df57" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>