recipes/omniauth.rb in rails3_devise_wizard-0.2.8 vs recipes/omniauth.rb in rails3_devise_wizard-0.3.1
- old
+ new
@@ -7,88 +7,85 @@
recipes.delete('omniauth')
end
if config['omniauth']
after_bundler do
-
- create_file 'config/initializers/omniauth.rb', <<-RUBY
+
+ # Don't use single-quote-style-heredoc: we want interpolation.
+ create_file 'config/initializers/omniauth.rb' do <<-RUBY
Rails.application.config.middleware.use OmniAuth::Builder do
- provider :provider, 'KEY', 'SECRET'
+ provider :#{config['provider']}, 'KEY', 'SECRET'
end
RUBY
- end
+ end
- append_file '.gitignore' do <<-TXT
-\n
-# keep OmniAuth service provider secrets out of the Git repo
-config/initializers/omniauth.rb
-TXT
- end
+ route "match '/auth/failure' => 'sessions#failure'"
+ route "match '/signout' => 'sessions#destroy', :as => :signout"
+ route "match '/signin' => 'sessions#new', :as => :signin"
+ route "match '/auth/:provider/callback' => 'sessions#create'"
+ route "resources :users, :only => [ :show, :edit, :update ]"
- inject_into_file 'config/routes.rb', :before => 'end' do
- "resources :users, :only => [ :show, :edit, :update ]\n"
- end
- inject_into_file 'config/routes.rb', :before => 'end' do
- "match '/auth/:provider/callback' => 'sessions#create'\n"
- end
- inject_into_file 'config/routes.rb', :before => 'end' do
- "match '/signout' => 'sessions#destroy', :as => :signout\n"
- end
- inject_into_file 'config/routes.rb', :before => 'end' do
- "match '/signin' => 'sessions#new', :as => :signin\n"
- end
- inject_into_file 'config/routes.rb', :before => 'end' do
- "match '/auth/failure' => 'sessions#failure'\n"
- end
-
- inject_into_file 'app/models/user.rb', :before => 'end' do <<-RUBY
+ inject_into_file 'app/models/user.rb', :before => 'end' do <<-RUBY
+
def self.create_with_omniauth(auth)
- create! do |user|
- user.provider = auth['provider']
- user.uid = auth['uid']
- user.name = auth['user_info']['name'] if auth['user_info']['name'] # Twitter, Google, Yahoo, GitHub
- user.email = auth['user_info']['email'] if auth['user_info']['email'] # Google, Yahoo, GitHub
- user.name = auth['extra']['user_hash']['name'] if auth['extra']['user_hash']['name'] # Facebook
- user.email = auth['extra']['user_hash']['email'] if auth['extra']['user_hash']['email'] # Facebook
+ begin
+ create! do |user|
+ user.provider = auth['provider']
+ user.uid = auth['uid']
+ if auth['user_info']
+ user.name = auth['user_info']['name'] if auth['user_info']['name'] # Twitter, Google, Yahoo, GitHub
+ user.email = auth['user_info']['email'] if auth['user_info']['email'] # Google, Yahoo, GitHub
+ end
+ if auth['extra']['user_hash']
+ user.name = auth['extra']['user_hash']['name'] if auth['extra']['user_hash']['name'] # Facebook
+ user.email = auth['extra']['user_hash']['email'] if auth['extra']['user_hash']['email'] # Facebook
+ end
+ end
+ rescue Exception
+ raise Exception, "cannot create user record"
end
end
+
RUBY
- end
+ end
- create_file 'app/controllers/sessions_controller.rb', <<-RUBY
+ # We have to use single-quote-style-heredoc to avoid interpolation.
+ create_file 'app/controllers/sessions_controller.rb', do <<-'RUBY'
class SessionsController < ApplicationController
-
- def new
- redirect_to '/auth/provider}'
- end
-
+
def create
auth = request.env["omniauth.auth"]
user = User.where(:provider => auth['provider'],
:uid => auth['uid']).first || User.create_with_omniauth(auth)
session[:user_id] = user.id
- if !user.email
- redirect_to edit_user_path(user), :alert => "Please enter your email address."
- else
- redirect_to root_url, :notice => 'Signed in!'
- end
+ redirect_to root_url, :notice => 'Signed in!'
end
-
+
def destroy
session[:user_id] = nil
redirect_to root_url, :notice => 'Signed out!'
end
-
+
def failure
redirect_to root_url, :alert => "Authentication error: #{params[:message].humanize}"
end
-
+
end
RUBY
- end
+ end
- inject_into_file 'app/controllers/application_controller.rb', :before => 'end' do <<-RUBY
+ # Don't use single-quote-style-heredoc: we want interpolation.
+ inject_into_class 'app/controllers/sessions_controller.rb', 'SessionsController' do <<-RUBY
+
+ def new
+ redirect_to '/auth/#{config['provider']}'
+ end
+
+RUBY
+ end
+
+ inject_into_file 'app/controllers/application_controller.rb', :before => 'end' do <<-RUBY
helper_method :current_user
helper_method :user_signed_in?
helper_method :correct_user?
private
@@ -114,11 +111,12 @@
def authenticate_user!
if !current_user
redirect_to root_url, :alert => 'You need to sign in for access to this page.'
end
end
+
RUBY
- end
+ end
end
end
__END__