Skip to content

Commit

Permalink
Account activation.
Browse files Browse the repository at this point in the history
When a user creates an account, they are sent an activation email.
This email contains link which, when clicked, activates the user and
logs them in.
  • Loading branch information
Rob Nagle committed Apr 11, 2010
1 parent 5fe741a commit 3cd54c7
Show file tree
Hide file tree
Showing 11 changed files with 84 additions and 7 deletions.
28 changes: 24 additions & 4 deletions app/controllers/users_controller.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
class UsersController < ApplicationController
layout "application"
before_filter :authenticate_user, :except => [:new, :create]
before_filter :authenticate_user, :except => [:new, :create, :activate]

def index
@users = User.all
Expand Down Expand Up @@ -38,15 +38,35 @@ def new
def create
@user = User.new(params[:user])

if @user.save
flash[:notice] = 'Registration successful.'
# Saving without session maintenance to skip
# auto-login which can't happen here because
# the User has not yet been activated
if @user.save_without_session_maintenance
@user.deliver_activation_instructions
flash[:notice] = 'Registration successful. Please check your email to activate your account.'
redirect_to shares_path
else
flash[:error] = 'Registration failed. Please try again.'
redirect_to root_path
end
end



def activate
@user = User.find_using_perishable_token(params[:token], 0) # Token does not expire.
if !@user
redirect_to root_url
else
if !@user.active?
@user.activated = true
@user.save
UserSession.create(@user) # Log the user in.
flash[:notice] = 'Account activated successfully.'
end

redirect_to shares_path
end
end

def edit
@user = current_user
Expand Down
11 changes: 11 additions & 0 deletions app/models/activation_mailer.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
class ActivationMailer < ActionMailer::Base

def activation_instructions(user, sent_at = Time.now)
from "A-List Account Activation <[email protected]>"
reply_to "[email protected]"
recipients user.email
subject "Activate Your A-List Account"
body :account_activation_url => activate_url(user.perishable_token)
end

end
6 changes: 6 additions & 0 deletions app/models/user.rb
Original file line number Diff line number Diff line change
Expand Up @@ -28,4 +28,10 @@ def unread_shares
def shares_from(submitter)
shares.select {|s| s.link.submitter == submitter }
end

def deliver_activation_instructions
reset_perishable_token!
ActivationMailer.deliver_activation_instructions(self)
end

end
5 changes: 5 additions & 0 deletions app/views/activation_mailer/activation_instructions.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
Thank you for creating an A-List account! Click the URL below to activate your account.

<%= @account_activation_url %>

If the above URL does not work try copying and pasting it into your browser. If you continue to have problem, please feel free to contact us.
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<p>Thank you for creating an A-List account! Click the URL below to activate your account.</p>

<p><%= link_to "Activate", @account_activation_url %></p>

<p>If the above URL does not work try copying and pasting it into your browser. If you continue to have problem, please feel free to contact us.</p>
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
Thank you for creating an A-List account! Click the URL below to activate your account.

<%= link_to "Activate", @account_activation_url %>

If the above URL does not work try copying and pasting it into your browser. If you continue to have problem, please feel free to contact us.
2 changes: 1 addition & 1 deletion app/views/user_sessions/new.html.erb
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<h1>Log In</h1>

<% form_for @user_session do |f| %>
<%= f.error_messages %>
<%= f.error_messages :header_message => "", :message => "Problem logging in:" %>
<p>
<%= f.label :email %><br />
<%= f.text_field :email %>
Expand Down
5 changes: 3 additions & 2 deletions config/routes.rb
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
ActionController::Routing::Routes.draw do |map|
map.log_in "log_in", :controller => "user_sessions", :action => "new"
map.log_out "log_out", :controller => "user_sessions", :action => "destroy"
map.log_in "/log_in", :controller => "user_sessions", :action => "new"
map.log_out "/log_out", :controller => "user_sessions", :action => "destroy"

map.connect "/users/friends", :controller => "users", :action => "friends"
map.activate "/users/activate/:token", :controller => "users", :action => "activate"

map.resources :shares
map.resources :links
Expand Down
9 changes: 9 additions & 0 deletions public/stylesheets/application.css
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,15 @@ a:hover {
color: #000;
}

#errorExplanation {
color: #CC0000;
}


#errorExplanation ul {
list-style: disc;
}

#main {
background: #FCFCFC;
max-width: 1000px;
Expand Down
3 changes: 3 additions & 0 deletions test/fixtures/activation_mailer/activation_instructions
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
ActivationMailer#activation_instructions

Find me in app/views/activation_mailer/activation_instructions.erb
12 changes: 12 additions & 0 deletions test/unit/activation_mailer_test.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
require 'test_helper'

class ActivationMailerTest < ActionMailer::TestCase
test "activation_instructions" do
@expected.subject = 'ActivationMailer#activation_instructions'
@expected.body = read_fixture('activation_instructions')
@expected.date = Time.now

assert_equal @expected.encoded, ActivationMailer.create_activation_instructions(@expected.date).encoded
end

end

0 comments on commit 3cd54c7

Please sign in to comment.