Techie August 2022
Introduction
There are several ways to authenticate users on a Rails app and the easiest approach is to use existing tools such as the Devise gem, since it’s a mature solution that has thousands of hours of code review, design, testing, and time in production.
During the authentication process, your app needs to commmunicate to the users via email because features such as password reset and account confirmation are contingent on the email service. Rails has an inbuilt component called Action Mailer that allows your app to send emails with just a few tweaks.
This section reveals the simple configurations needed to allow Devise to send account confirmation request and password reset link emails to app users.
Prerequisites
NB: You may skirt these requirement versions if you are willing to experiment.
- Ruby 3.2.0
- Rails 7.0.3
- Gmail account
Part A: Creating a Rails 7 Project
Create a new rails project called devise_email by following this document: Creating A Rails 7 App: With esbuild, bootstrap and jquery.
Part B: Installing Devise
1 . Add devise gem in the Gemfile file.
2 . Bundle install
3 . Setup devise
4 . Generate user model
5 . Add confirmable and recoverable to the users table
in db/migrations/[…]_devise_create_users.rb.
6 . Add devise :confirmable in models/user.rb file.
7 . Run migrations
8 . Add :turbo_stream as a navigational format in
config/initializers/devise.rb. Rails 7 throws this error if you dont have it:
undefined method `user_url’ for #<Devise::RegistrationsController:0x0000000000cf08>
9 . Redirect user to login page if they are not signed in.
Add this code to controllers/application_controller.rb before any other actions.
10 . Add sign in and log out
links to the navbar in app/views/layouts/application.html.erb
Part C: Setting Up Gmail App Passwords
ActionMailer will use your gmail account to send mail, but you can not use your normal gmail password with external applications. Gmail has a nifty feature called App passwords that lets you sign in to your Google Account from apps on devices that don’t support 2-Step Verification. Click here to set up App passwords. On the page, select ‘Mail’ under the ‘select app’ tab, then enter a custom name under the ‘select device’ tab.
Click on the ‘Generate’ button to generate the password. Just like your normal password, this app password grants complete access to your Google Account. Copy 16-character password shown, you will need it for the next step.
Part D: Setting Up Environment Variables For the Email Credentials
1 . In your rails project, create a ruby hidden file in /config directory that will contain the ENV variables. Prepend a dot . in the file name to make it hidden.
e.g .my_secret.rb, which will contain this:
NB: press Ctrl + Hto view hidden files on linux.
2 . You want the rails app to load .my_secret.rb file as soon as the server fires up. To do that, you will use the File and load methods inside the /config/environment.rb file in the rails project. Make sure to place this code just above the line: Rails.application.initialize!
3 . Ensure the .my_secret.rb file is not submitted to github whenever you commit your project to a github repository. To do that, open the .gitignore file and add the path to the file as shown below:
With the these steps you are done setting up the environment variables for your database. Now if you open the rails console and enter this ENV[‘EMAIL_USER_ID’], it should output the content of that variable:
Part E: Configure Devise to work with ActionMailer
1 . Add your email address in config/initializers/devise.rb
2 . Configure ActionMailer for the development
environment in config/environments/development.rb.
Testing the app
To run the app, cd into the root of the project and issue this command:
Now navigate to localhost:3000. Create an account by clicking on the sign up link. This should send you an email containing the confirmation instructions. You will also get reset password emails whenever you reset your password by clicking on forgot your password link.
Additional Information
- Redirecting users
1 . Redirecting After The User Signs Up (Confirmation Pending)
If you want to redirect the user to a specific url after signing up, override the after_inactive_sign_up_path_for in the registrations_controller.
i). Create a new registrations_controller.rb in app/controllers directory.
ii). Override Devise default behaviour in the routes in config/routes.rb
2 . Redirecting From The Confirmation Email
You may want to redirect the user to a specific url after they clicked the link in the confirmation email. To do that, just override the after_confirmation_path_for in the confirmations_controller.
i). Create a new confirmations_controller.rb in app/controllers directory.
ii). Override Devise default behaviour in the routes in config/routes.rb
Thanks for reading, see you in the next one!