- 📖 About the Project
- 💻 Getting Started
- 👥 Authors
- 🔭 Future Features
- 🤝 Contributing
- ⭐️ Show your support
- 🙏 Acknowledgements
- 📝 License
This project is all about authentication using devise and using action mailer to send outbond emails from your Rails application eg, forgot password, email confirmation
Key features of the application.
- Devise installation
- Creating a devise User
- Adding action mailer in development.rb file
- Generating app name and password
- Encrypting email and password in Rails application credential
- Accessing the email and password in the mailer configuration
To get a local copy up and running, follow these steps.
To run this project you need:
- Code Editor.
- Ruby On Rails.
Clone this repository to your desired folder:
I am using Ruby -v 3.3.4 and Rails -v 7.2.1
cd my-folder
gh repo clone https://github.com/fatmahussein/rails-auth-skeleton.git
In your gmail account -> manage account -> security -> search 'app password', create an app name, and copy the generated password, you will use it.
Add gem 'devise', '~> 4.9.2'
bundle install
Run the following code:
$ rails generate devise:install
, follow instructions provided
$ rails generate devise User
uncomment
t.string :confirmation_token`
t.datetime :confirmed_at
t.datetime :confirmation_sent_at
t.string :unconfirmed_email
in the devise_create_user migration file
Add
:confirmable
to the User model
$ rails db:migrate
In your development.rb file, add the following settings:
config.action_mailer.delivery_method = :smtp
config.action_mailer.smtp_settings = {
address: 'smtp.gmail.com',
port: 587,
domain: 'example.com',
user_name: 'your email',
password: 'your password',
authentication: 'plain',
enable_starttls: true,
open_timeout: 5,
read_timeout: 5 }
Please change this to be true in your file
config.action_mailer.raise_delivery_errors = true
Your Rails application should now be able to send outbound emails.
Run
$env:EDITOR="code --wait"; rails credentials:edit
in your code editor, an editor will pop up.
Add
email:
username: 'your email'
password: 'your password'
to the editor and save.
We will use
Rails.application.credentials.email[:username]
to access the encrypted data.
Your action mailer config should now look like this:
config.action_mailer.delivery_method = :smtp
config.action_mailer.smtp_settings = {
address: 'smtp.gmail.com',
port: 587,
domain: 'example.com',
user_name: Rails.application.credentials.email[:username],
password: Rails.application.credentials.email[:password],
authentication: 'plain',
enable_starttls: true,
open_timeout: 5,
read_timeout: 5 }
Your data is now encrypted and rails is sending emails successfully :)
Finally, in case it's still not working,
run
Rails.application.credentials.email[:username]
in your rails console to confirm if it's displaying Nil
or your email address.
All the best.
👤 Fatuma Hussein
- GitHub: @githubhandle
- Skeleton to kickstart any RoR project
Contributions, issues, and feature requests are welcome! Feel free to check the Issues.
If you like this project, show your support by giving the project a ⭐️.
I would like to thank Microverse.
This project is MIT licensed.