Account activation and password reset using Devise.

Nikhil Bhavsar
3 min readMar 17, 2016

--

Hi guys you are already known to the my simplsms app on which I added some authentications using the devise gem. Now I have updated some new features into the app using devise gem.

I have added the confirmable and recoverable modules in the new updated app. Which is usable for the confirming the account first before signing in to the app. It just because for the confirming the user is actually exists and there should no any misuse of our app using any fake account. Also added the password reset thing to the app.

This account activation and password reset features can be use by simply adding the confirmable and recoverable modules in the our devise model. The message for the account activation and password reset token send using the sending a mail on the users email address.

So for doing this we need to add SMTP server information to our app. And have provide the details of our email address from which we will send these tokens on the users email address.

So lets configure the smtp with account confirmation and password resetting:

First we will set up the development mailer for use on your local machine. In“config/environments/development.rb” you should already have included

config.action_mailer.default_url_options = { :host => ‘localhost:3000’ }

when you installed devise.

Next you should turn on the option to raise an exception if there is an error when sending an email. You can do this by including

config.action_mailer.raise_delivery_errors = true

in the same file. Next we will add the email delivery method. You should leave the values as they are because I will show you how to set your email information later using local environment variables. Using local environment variables will allow you to hide your email information on open source software sites like Github. In the same file add

config.action_mailer.delivery_method = :smtp

config.action_mailer.smtp_settings = {
address: “smtp.gmail.com”,
port: 587,
domain: ENV[“GMAIL_DOMAIN”],
authentication: “plain”,
enable_starttls_auto: true,
user_name: ENV[“GMAIL_USERNAME”],
password: ENV[“GMAIL_PASSWORD”]
}

Make sure that you leave the values as they are in order to allow you to set the local variables later on.

Now add the environmental variables:

just go to console by rails c command and type:

ENV[“GMAIL_DOMAIN”] = “gmail.com” or “your_domain.com”

ENV[“GMAIL_USERNAME”] = “your_username@domain.com”

ENV[“GMAIL_PASSWORD”] = “password”

now restart rails server and see on the local server the app working and able to send mail with the account activation and password reset tokens to the users email address.

Then now do the same for production environment for deploy it on server we need to do the same changes “config/environments/production.rb” file and add this lines:

config.action_mailer.default_url_options = { :host => ‘your_servers_address’ }

after running app if there showing any action view::template::error then you should add this line in to file:

Rails.application.routes.default_url_options[:host] = ‘your_server_address’

Next you will have to add in these lines. Once again make sure that you leave the variables as they are.

config.action_mailer.delivery_method = :smtp
config.action_mailer.perform_deliveries = true
config.action_mailer.raise_delivery_errors = false
config.action_mailer.default :charset => “utf-8”

config.action_mailer.smtp_settings = {
address: “smtp.gmail.com”,
port: 587,
domain: ENV[“GMAIL_DOMAIN”],
authentication: “plain”,
enable_starttls_auto: true,
user_name: ENV[“GMAIL_USERNAME”],
password: ENV[“GMAIL_PASSWORD”]
}

now we completed all the things so we need to deploy our app on our server by running command on terminal:

$ cap production deploy

and then run the migrations on server and don’t forget to add the env. variables over the server too.

Hope you liked the blog, Thank you for reading.

--

--