<?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: Shedrack Sunday</title>
    <description>The latest articles on DEV Community by Shedrack Sunday (@shedracksunday).</description>
    <link>https://dev.to/shedracksunday</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%2F965004%2F739a568c-e63b-4a69-97c4-d9e70376c48f.jpeg</url>
      <title>DEV Community: Shedrack Sunday</title>
      <link>https://dev.to/shedracksunday</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/shedracksunday"/>
    <language>en</language>
    <item>
      <title>Implementing Devise in Your Ruby on Rails Application For Authentication</title>
      <dc:creator>Shedrack Sunday</dc:creator>
      <pubDate>Fri, 04 Aug 2023 14:06:56 +0000</pubDate>
      <link>https://dev.to/shedracksunday/implementing-devise-in-your-ruby-on-rails-application-for-authentication-1d21</link>
      <guid>https://dev.to/shedracksunday/implementing-devise-in-your-ruby-on-rails-application-for-authentication-1d21</guid>
      <description>&lt;p&gt;&lt;strong&gt;INTRODUCTION&lt;/strong&gt;&lt;br&gt;
&lt;a href="https://github.com/heartcombo/devise#getting-started"&gt;Devise&lt;/a&gt; is an effective authentication gem for Ruby on Rails that makes it easier to integrate user authentication into your online applications. Numerous functions are offered, such as session management, third-party sign-in, password reset capability, and others. By providing pre-built modules and functions that are simple to incorporate into your Rails applications, Devise streamlines the process rather than requiring you to construct these features from scratch.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;COMPONENTS OF DEVISE&lt;/strong&gt;&lt;br&gt;
Devise has a rich actionable pact composed of 10 modules:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;a href="http://www.rubydoc.info/github/heartcombo/devise/main/Devise/Models/DatabaseAuthenticatable"&gt;Database Authenticatable&lt;/a&gt;: hashes and stores a password in the database to validate the authenticity of a user while signing in. The authentication can be done both through POST requests or HTTP Basic Authentication.&lt;/li&gt;
&lt;li&gt;
&lt;a href="http://www.rubydoc.info/github/heartcombo/devise/main/Devise/Models/Omniauthable"&gt;Omniauthable&lt;/a&gt;: adds &lt;a href="https://github.com/omniauth/omniauth"&gt;OmniAuth&lt;/a&gt; support.&lt;/li&gt;
&lt;li&gt;
&lt;a href="http://www.rubydoc.info/github/heartcombo/devise/main/Devise/Models/Confirmable"&gt;Confirmable&lt;/a&gt;: sends emails with confirmation instructions and verifies whether an account is already confirmed during sign in.&lt;/li&gt;
&lt;li&gt;
&lt;a href="http://www.rubydoc.info/github/heartcombo/devise/main/Devise/Models/Recoverable"&gt;Recoverable&lt;/a&gt;: resets the user password and sends reset instructions.&lt;/li&gt;
&lt;li&gt;
&lt;a href="http://www.rubydoc.info/github/heartcombo/devise/main/Devise/Models/Registerable"&gt;Registerable&lt;/a&gt;: handles signing up users through a registration process, also allowing them to edit and destroy their account.&lt;/li&gt;
&lt;li&gt;
&lt;a href="http://www.rubydoc.info/github/heartcombo/devise/main/Devise/Models/Rememberable"&gt;Rememberable&lt;/a&gt;: manages generating and clearing a token for remembering the user from a saved cookie.&lt;/li&gt;
&lt;li&gt;
&lt;a href="http://www.rubydoc.info/github/heartcombo/devise/main/Devise/Models/Trackable"&gt;Trackable&lt;/a&gt;: tracks sign in count, timestamps and IP address.&lt;/li&gt;
&lt;li&gt;
&lt;a href="http://www.rubydoc.info/github/heartcombo/devise/main/Devise/Models/Timeoutable"&gt;Timeoutable&lt;/a&gt;: expires sessions that have not been active in a specified period of time.&lt;/li&gt;
&lt;li&gt;
&lt;a href="http://www.rubydoc.info/github/heartcombo/devise/main/Devise/Models/Validatable"&gt;Validatable&lt;/a&gt;: provides validations of email and password. It's optional and can be customized, so you're able to define your own validations.&lt;/li&gt;
&lt;li&gt;
&lt;a href="http://www.rubydoc.info/github/heartcombo/devise/main/Devise/Models/Lockable"&gt;Lockable&lt;/a&gt;: locks an account after a specified number of failed sign-in attempts. Can unlock via email or after a specified time period.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Devise, an exceptionally thorough gem, does the majority of the authentication chores on your behalf. Even if you're thinking about implementing highly specialised features, Devise can still manage a sizable chunk of the effort involved because it maintains all crucial authentication processes.&lt;/p&gt;
&lt;h2&gt;
  
  
  STEPS:
&lt;/h2&gt;

&lt;p&gt;&lt;strong&gt;Making a New Rails Application is the first step.&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;rails new device-sample
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--PLYEWxXP--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/l1srtvcbi0ugs97d9o7z.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--PLYEWxXP--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/l1srtvcbi0ugs97d9o7z.png" alt="Rew rails app" width="452" height="555"&gt;&lt;/a&gt;&lt;br&gt;
A new Rails project with a number of generated files and folders will be created under the directory  device-sample by the rails new command.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Insert devise into the gemfile as the next step&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;gem 'devise'
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--h-ATKeu7--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/2krgollpfoh00ctq5051.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--h-ATKeu7--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/2krgollpfoh00ctq5051.png" alt="gemfile" width="800" height="202"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Run bundle install from your terminal after that.&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;bundle install
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In Ruby on Rails applications, the command "bundle install" is used to install each of the required gems (libraries) listed in the project's Gemfile. This command resolves dependencies, downloads and installs the requested gems, and then verifies the Gemfile for the necessary gems and their versions. It guarantees that your project has all the dependencies needed to function properly.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Next step is to run the command to install devise to the project&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;$ rails generate devise:install
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--ZogIv_Tf--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/4sm01wrbbbl9vgakdazm.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--ZogIv_Tf--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/4sm01wrbbbl9vgakdazm.png" alt="Devise run" width="650" height="542"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Lets go over some of the instructions signalled above:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Default URL&lt;/strong&gt;: Use the &lt;code&gt;config.action_mailer.default_url_options&lt;/code&gt; parameter to define default URL options in your environment files. For instance, update the host and port in config/environments/development.rb to the real host in production.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Root UR&lt;/strong&gt;L: Create a root URL by specifying it in your config/routes.rb file, unless you're developing an API-only service. Use root to: &lt;code&gt;"home#index"&lt;/code&gt; as an example to establish your root URL.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Flash Messages Setup&lt;/strong&gt;: To improve user experience, incorporate flash messages into your application's layout (often located in app/views/layouts/application.html.erb). Include &lt;code&gt;p class="notice"&amp;gt;%= notice%&amp;gt; and p class="alert"&amp;gt;%= alert%&amp;gt;&lt;/code&gt; in your text.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--tluXPIKA--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/zrbdvgzw735ypzicqbi3.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--tluXPIKA--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/zrbdvgzw735ypzicqbi3.png" alt="Prompt message" width="800" height="326"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Next we generate view for our application where the login will be&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;rails generate devise:views
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--DADoERRU--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/lfc6nb2kdg06hmk67dw8.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--DADoERRU--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/lfc6nb2kdg06hmk67dw8.png" alt="Views" width="703" height="434"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Great! We can now generate our model. You can call your devise model whatever you want, I will call it "User". It's a generic name understandable by anyone. Let's run the command now.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;$ rails generate devise User
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--oWI6BFw3--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/a2wiq2tdizhyf7hjc015.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--oWI6BFw3--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/a2wiq2tdizhyf7hjc015.png" alt="User model" width="604" height="192"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;opening the migration file (db/migrate/2........_devise_create_users.rb) to review the default code:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--gzmTzKrz--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/3yfh9uwdsxo6k4fa0a3g.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--gzmTzKrz--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/3yfh9uwdsxo6k4fa0a3g.png" alt="Miraation path" width="800" height="340"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Here, you can uncomment the fields you want to use, for example, if you want to be able to confirm your users sending them an email, you can uncomment the four lines below # Confirmable. The devise gem will do the rest (plus some configuration of your own you will have to investigate, as it's not going to be covered in this tutorial). &lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Next step is adding a route&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--w-KdP1Kf--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/kowwrgc6i8dm16apssxj.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--w-KdP1Kf--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/kowwrgc6i8dm16apssxj.png" alt="Rails route" width="635" height="367"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;After reviewing the Devise-generated files and configuration, you may use the following command to execute the migration you created at the beginning of this step:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;$ rails db:migrate
Or you can also write:

$ rake db:migrate
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--2XcUUw_F--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/6tmtufhl33n07xt5kz2z.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--2XcUUw_F--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/6tmtufhl33n07xt5kz2z.png" alt="Migration" width="631" height="141"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;** Finally lets run the server**&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;$ rails server
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--FLLcvoQl--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/k2f7s9p69yor9shu14uq.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--FLLcvoQl--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/k2f7s9p69yor9shu14uq.png" alt="Sign up" width="363" height="412"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;You've successfully learned how to install devise in your apps, add models using devise, add fields to those models, and modify devise's views. The remaining portions of your web application may now be completed.&lt;/p&gt;

&lt;p&gt;Thank you for reading!&lt;/p&gt;

</description>
      <category>ruby</category>
      <category>rails</category>
      <category>webdev</category>
      <category>softwareengineering</category>
    </item>
  </channel>
</rss>
