Add authentication and sending registration confirmation links via mails in rails application

Steps to use authlogic…..send confirmation email on registration and reset password..

1.gem install authlogic. environment.rb file write..
config.gem “authlogic” the model in which you wanna authenticate (probably user or member model) write

also in the migration file add these 3 lines..

t.string :crypted_password
t.string :password_salt
t.string :persistence_token

4.paste the foll code in application controller.

helper_method :current_member

def current_member_session
return @current_member_session if defined?(@current_member_session)
@current_member_session = MemberSession.find

def current_member
return @current_member if defined?(@current_member)
@current_member = current_member_session && current_member_session.member

5.paste the foll code in the application layout…

<% if current_member %>

Welcome Mr.<%= current_member.username %>

<%= link_to “Edit Profile “,edit_member_profile(:current) %>|<%= link_to “Logout”,logout_path %>

<% else %>

<%= link_to “Register “,new_member_profile(:current) %>|<%= link_to “Login”,login_path %>

<% end %>

the code must be at a place where you would like login /logout etc to appear.
PS.Use nifty_scaffold …and nifty_layout.

6.Create a session using
script/generate session user_session

the do..
script/generate nifty_scaffold user_session –skip-model username:string password:string new destroy.

7.Now in the routes do

map.login “login” ,:controller=>’member_sessions’,:action=>’new’
map.logout “logout” , :controller=>’member_sessions’,:action=>’destroy’ the user_sessions controller must look like this..

class MemberSessionsController ‘members’,:action=>’index’
render :action => ‘new’

def destroy
@member_session = MemberSession.find
flash[:notice] = “Successfully logged out.”
redirect_to :controller=>’members’,:action=>’index’

PS.I have used members instead of users..

9.use password_field instead of text_field in view.

10.Start the app and see the magic…


now to send email….

do all the above steps.. the user model add a field called active which is of type boolean. the user model file add…
def active?

def activate!

3.Create a mailer…the command..
script/generate mailer mymailer..

4.add a method to be used to send emails..

def mail(recipient)
from “”
subject “Hi #{recipient}”
body (:user=>recipient)

5.In the enviornments.rb file write this after the end..

require “smtp_tls”

ActionMailer::Base.raise_delivery_errors = false
ActionMailer::Base.delivery_method = :smtp
ActionMailer::Base.smtp_settings = {
:address => “”,
:port => 587,
:domain => ‘’,
:user_name => “”,
:password => “***********”,
:authentication => :plain
This is particularly for sending mails via ur gmail account..

download the plugin or contact me if you cant get it..
Put the plugin in vendors/plugin..
the name of plugin is action_mailer..

6.the user controller can be modified in this way…

def create
@member =[:member])
flash[:notice] = “Successfully Registered.”
flash[:notice] = “A verification email has been sent to the email id provided by you.”
redirect_to @member
redirect_to :controller=>’members’,:action=>’index’
render :action => ‘new’

rest all methods remain the same..

here we have used deliver method of actionmailer class.. create a controller to handle confirmation..
script/generate controller activations

class ActivationsController ‘member_sessions’,:action=>’new’

end in views/mymailer put the file mail.rhtml as…


u can write any text below or above it..

9.finally in the routes.rb file add…
map.connect “:members/:mymethod/:id”,:controller=>’activations’,:action=>’mymethod’,:id=>’persistence_token’

10.done now connect to the internet and see the magic..

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s