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

Posted on June 28, 2010

0


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

1.gem install authlogic.

2.in environment.rb file write..
config.gem “authlogic”

3.in the model in which you wanna authenticate (probably user or member model) write
acts_as_authentic.

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

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

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

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’

8.now the user_sessions controller must look like this..

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

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

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..

1.in the user model add a field called active which is of type boolean.
2.in the user model file add…
def active?
active
end

def activate!
self.active=true
save
end

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

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

def mail(recipient)
from “example@gmail.com”
recipients recipient.email
subject “Hi #{recipient}”
body (:user=>recipient)
end

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 => “smtp.gmail.com”,
:port => 587,
:domain => ‘gmail.com’,
:user_name => “abc@gmail.com”,
: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.new(params[:member])
@member.active=false
if @member.save
flash[:notice] = “Successfully Registered.”
flash[:notice] = “A verification email has been sent to the email id provided by you.”
Mymailer.deliver_mail(@member)
redirect_to @member
redirect_to :controller=>’members’,:action=>’index’
else
render :action => ‘new’
end
end

rest all methods remain the same..

here we have used deliver method of actionmailer class..

7.now create a controller to handle confirmation..
script/generate controller activations

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

end

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

http://localhost:3000/activations/mymethod/

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..

Advertisements
Posted in: Rails app