<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: themagickoala</title>
    <description>The latest articles on DEV Community by themagickoala (@themagickoala1).</description>
    <link>https://dev.to/themagickoala1</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F196449%2F4c80ed64-78d9-4856-b29f-04eb282bb0d3.jpg</url>
      <title>DEV Community: themagickoala</title>
      <link>https://dev.to/themagickoala1</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/themagickoala1"/>
    <language>en</language>
    <item>
      <title>Reconfirmable: integrating Devise into a non-standard registration system</title>
      <dc:creator>themagickoala</dc:creator>
      <pubDate>Tue, 16 Jul 2019 19:42:30 +0000</pubDate>
      <link>https://dev.to/themagickoala1/reconfirmable-integrating-devise-into-a-non-standard-registration-system-1563</link>
      <guid>https://dev.to/themagickoala1/reconfirmable-integrating-devise-into-a-non-standard-registration-system-1563</guid>
      <description>&lt;p&gt;Devise makes it easy to implement a full user authentication system in Ruby on Rails while writing as little code as necessary. The modules it provides are easy to integrate and follow best practices for securing your application. But sometimes your application requires a little out of the box thinking. So what do you do when your user journeys aren't what Devise is expecting?&lt;/p&gt;

&lt;h2&gt;
  
  
  What problem are you even solving here?
&lt;/h2&gt;

&lt;p&gt;Consider an application where you want to confirm a user's email address before letting them even get into your application. The standard flow for the devise confirmable module is to do it the other way round. So you're going to have to hand-crank this functionality. But you also really want to use confirmable's reconfirmable feature when users change their email address, which means enabling confirmable as well. Now you've got two competing confirmation systems and you've got your knickers in a twist! This post will lay out how to bypass confirmable for registration while still using reconfirmable for changing emails.&lt;/p&gt;

&lt;h2&gt;
  
  
  Getting started
&lt;/h2&gt;

&lt;p&gt;Setting up a Ruby on Rails project is a solved problem, as is integrating Devise, so we'll try not to spend too much time on it. The &lt;a href="https://github.com/plataformatec/devise"&gt;Devise Github repository&lt;/a&gt; has a good tutorial for this, and the rest of the post will assume a working implementation of Devise without confirmable enabled.&lt;/p&gt;

&lt;h2&gt;
  
  
  Configuring confirmable
&lt;/h2&gt;

&lt;p&gt;This section is going to take heavy inspiration from the Devise wiki on Github &lt;a href="https://github.com/plataformatec/devise/wiki/How-To:-Add-:confirmable-to-Users"&gt;here&lt;/a&gt;. Assuming your user model is at &lt;code&gt;models/user.rb&lt;/code&gt;, all you need to do is add &lt;code&gt;:confirmable&lt;/code&gt;:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;devise :registerable, :confirmable
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Enabling confirmable will require adding four columns to your users table, so create a migration script:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;rails g migration add_confirmable_to_devise
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In the resulting migration file &lt;code&gt;db/migrate/YYYYMMDDxxx_add_confirmable_to_devise.rb&lt;/code&gt;, add the following code:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;class AddConfirmableToDevise &amp;lt; ActiveRecord::Migration
  # Note: You can't use change, as User.update_all will fail in the down migration
  def up
    add_column :users, :confirmation_token, :string
    add_column :users, :confirmed_at, :datetime
    add_column :users, :confirmation_sent_at, :datetime
    add_column :users, :unconfirmed_email, :string
    add_index :users, :confirmation_token, unique: true

    # User.reset_column_information # Need for some types of updates, but not for update_all.
    # To avoid a short time window between running the migration and updating all existing
    # users as confirmed, do the following
    User.update_all confirmed_at: DateTime.now
    # All existing user accounts should be able to log in after this.
  end

  def down
    remove_columns :users, :confirmation_token, :confirmed_at, :confirmation_sent_at, :unconfirmed_email
  end
end
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The first step here is to add the necessary columns:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;add_column :users, :confirmation_token, :string
add_column :users, :confirmed_at, :datetime
add_column :users, :confirmation_sent_at, :datetime
add_column :users, :unconfirmed_email, :string
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The &lt;code&gt;confirmation_token&lt;/code&gt; column is your confirmation key - this will need to be sent to the user when setting a new email address to verify that they have access to the inbox. The &lt;code&gt;confirmation_sent_at&lt;/code&gt; column gets populated on generation of the token, and the &lt;code&gt;confirmed_at&lt;/code&gt; column gets set when confirmation is completed successfully. Having only skimmed the surface of the sort of magic Devise is capable of, I can only assume these two columns can be compared in order to prevent multiple confirmations being attempted.&lt;/p&gt;

&lt;p&gt;The fourth column, &lt;code&gt;unconfirmed_email&lt;/code&gt;, is added for use with the reconfirmable flow. The temporary email address is stored here, and persisted to the email column when confirmation is complete. Sounds simple enough right?&lt;/p&gt;

&lt;p&gt;Well we're not quite done - confirmable introduces additional functionality to the user model, and users have to confirm their accounts to use the application. But what about existing users? They aren't confirmed, and they don't have a token with which to confirm. To get around this, we can just run &lt;code&gt;User.update_all confirmed_at: DateTime.now&lt;/code&gt; and just like that, all our existing users are confirmed.&lt;/p&gt;

&lt;p&gt;To finish off, run &lt;code&gt;rake db:migrate&lt;/code&gt; and then make sure reconfirmable is enabled in the Devise config by adding/setting the following line in &lt;code&gt;config/initializers/devise.rb&lt;/code&gt;:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;config.reconfirmable = false
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Tweaking reconfirmable
&lt;/h2&gt;

&lt;p&gt;By default, the &lt;code&gt;confirmation_token&lt;/code&gt; generated uses &lt;code&gt;SecureRandom.urlsafe_base64(15)&lt;/code&gt; to create the random token. This can be changed in the User model by overriding &lt;code&gt;generate_confirmation_token&lt;/code&gt;. Bear in mind that this method also sets the &lt;code&gt;confirmation_sent_at&lt;/code&gt; property, so I recommend calling super and then setting the &lt;code&gt;confirmation_token&lt;/code&gt; property to your new preferred value. We used this to set a 6-digit passcode to be manually entered into a continuous client-side flow, though you can just as easily use the more standard email-with-link-to-unique-page using the token as a url parameter to identify the user.&lt;/p&gt;

&lt;h2&gt;
  
  
  Bypassing confirmable for registration
&lt;/h2&gt;

&lt;p&gt;Here we get to the problem we're trying to solve. We're already confirming a user's email address as part of the registration journey, so we don't need to confirm it post-registration. But by default, confirmable is going to send its own confirmation email to the user. So how do we get around it?&lt;/p&gt;

&lt;p&gt;Fortunately, we can override a method provided by Devise to allow you to customise the mail mapping of the registration email. This is &lt;code&gt;send_on_create_confirmation_instructions&lt;/code&gt;. By default, all it does is call &lt;code&gt;send_confirmation_instructions&lt;/code&gt;, allowing the user to perform additional configuration before or after sending the email. So the first step is to do nothing at all:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;def send_on_create_confirmation_instructions
end
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This will prevent the confirmation email from being sent but the user still won't be able to log in as they aren't confirmed. So let's expand a little:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;def send_on_create_confirmation_instructions
  self.confirm
end
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;That should do it. We know the user is confirmed from our own custom flow, so we can just tell Devise as much, and voila! The user can log in just fine, and didn't receive any superfluous confirmation emails.&lt;/p&gt;

&lt;h2&gt;
  
  
  Summary
&lt;/h2&gt;

&lt;p&gt;Devise provides a lot of additional functionality to the user model, allowing us to focus on small customisations to support our application's individual requirements.&lt;/p&gt;

&lt;p&gt;This post is a work in progress, and I hope to be able to improve it as I continue to integrate our application with the server-side code being written by our stellar back-end team, and with feedback from you. Please leave a comment if you can help to improve this guide, whether with constructive criticism or additional sources.&lt;/p&gt;

&lt;h2&gt;
  
  
  Sources
&lt;/h2&gt;

&lt;ol&gt;
&lt;li&gt;How to add :confirmable to users - Devise Github Wiki (&lt;a href="https://github.com/plataformatec/devise/wiki/How-To:-Add-:confirmable-to-Users"&gt;https://github.com/plataformatec/devise/wiki/How-To:-Add-:confirmable-to-Users&lt;/a&gt;);&lt;/li&gt;
&lt;li&gt;Devise::Models::Confirmable - Devise Rubydocs (&lt;a href="https://www.rubydoc.info/github/plataformatec/devise/master/Devise/Models/Confirmable"&gt;https://www.rubydoc.info/github/plataformatec/devise/master/Devise/Models/Confirmable&lt;/a&gt;);&lt;/li&gt;
&lt;li&gt;Getting started with Rails and Devise - Devise Github repository (&lt;a href="https://github.com/plataformatec/devise"&gt;https://github.com/plataformatec/devise&lt;/a&gt;);&lt;/li&gt;
&lt;li&gt;Credit to &lt;a href="https://github.com/verenion"&gt;verenion&lt;/a&gt; for development of the server-side code and architecture of the application.&lt;/li&gt;
&lt;/ol&gt;

</description>
      <category>rails</category>
      <category>ruby</category>
      <category>devise</category>
      <category>security</category>
    </item>
  </channel>
</rss>
