/ Authentication / Send Confirmation Email

This is tutorial on how you can send confirmation email after registration via Devise confirmable setting.

1. Setting up Gmail

For demonstration and testing purpose we are gonna use Gmail smtp to help sending out emails from our Rails app. So if you don’t have a Gmail account, just create one for yourself. It’s dead easy. After you have created one, go to this setting page here. And search for Allow less secure apps (normally at the bottom of the page). When you found it, turn it on.

 

Allow less secure app Gmail

 

2. Set up SMTP setting

Go to app/config/application.rb and add this:

 

# GMAIL SMPTP
ActionMailer::Base.smtp_settings = {
  :address              => 'smtp.gmail.com',
  :domain               => 'mail.google.com',
  :port                 => 587,
  :user_name            => "YOUR GMAIL USERNAME",
  :password             => "YOUR GMAIL PASSWORD",
  :authentication       => 'login',
  :enable_starttls_auto => true
}

 

4. Add :confirmable Module

On your app/models/user.rb, change devise setting to this:

devise :database_authenticatable, :registerable, :omniauthable,
:recoverable, :rememberable, :trackable, :validatable, :confirmable

 

5. Add user.skip_confirmation! for Omniauth

If the user use Omniauth social media login, we do not want the app to send confirmation email, thus before saving the user we must add user.skip_confirmation!.

To edit Omniauth setting, go to omniauth_controller.rb and add this before saving every user from omniauth provider:

if auth.provider == "facebook"
        
        	user.provider = auth.provider
        user.uid = auth.uid
          user.oauth_token = auth.credentials.token
        	
        	user.first_name = auth.extra.raw_info.first_name
        	user.last_name = auth.extra.raw_info.last_name
          user.email = auth.extra.raw_info.email

          # Facebook's token doesn't last forever
        user.oauth_expires_at = Time.at(auth.credentials.expires_at)

        user.skip_confirmation!
        user.save
.......

 

 

 

 

No video for this post.

No issues for this post.

by ryzalyusoff