Advertisement
Guest User

Untitled

a guest
Jul 22nd, 2016
78
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.63 KB | None | 0 0
  1. Devise.setup do |config|
  2. config.omniauth :facebook, ENV["FB_ID"], ENV["FB_SECRET"], scope: 'email', info_fields: 'email, first_name,last_name', image_size: 'large'
  3. end
  4.  
  5. Rails.application.routes.draw do
  6. devise_for :users, controllers: { omniauth_callbacks: 'users/omniauth_callbacks' }
  7. end
  8.  
  9. # app/models/user.rb
  10.  
  11. class User < ActiveRecord::Base
  12. devise :omniauthable, omniauth_providers: [:facebook]
  13. end
  14.  
  15. $ rails g migration AddOmniauthToUsers
  16. provider uid picture first_name last_name token token_expiry:datetime
  17. $ rake db:migrate
  18.  
  19. # app/models/user.rb
  20. class User < ActiveRecord::Base
  21. def self.find_for_facebook_oauth(auth)
  22. where(provider: auth.provider, uid: auth.uid).first_or_create do |user|
  23. user.provider = auth.provider
  24. user.uid = auth.uid
  25. user.email = auth.info.email
  26. user.password = Devise.friendly_token[0,20] # Fake password for validation
  27. user.first_name = auth.info.first_name
  28. user.last_name = auth.info.last_name
  29. user.picture = auth.info.image
  30. user.token = auth.credentials.token
  31. user.token_expiry = Time.at(auth.credentials.expires_at)
  32. end
  33. end
  34. end
  35.  
  36. # app/controllers/users/omniauth_callbacks_controller.rb
  37.  
  38. class Users::OmniauthCallbacksController < Devise::OmniauthCallbacksController
  39. def facebook
  40. user = User.find_for_facebook_oauth(request.env['omniauth.auth'])
  41.  
  42. if user.persisted?
  43. sign_in_and_redirect user, event: :authentication
  44. set_flash_message(:notice, :success, kind: 'Facebook') if is_navigational_format?
  45. else
  46. session['devise.facebook_data'] = request.env['omniauth.auth']
  47. redirect_to new_user_registration_url
  48. end
  49. end
  50. end
  51.  
  52. /app/app/controllers/Users/omniauth_callbacks_controller.rb:1:in `<top (required)>': uninitialized constant Users (NameError)
  53. from /app/vendor/bundle/ruby/2.3.0/gems/railties-4.2.6/lib/rails/engine.rb:472:in `block (2 levels) in eager_load!'
  54. from /app/vendor/bundle/ruby/2.3.0/gems/railties-4.2.6/lib/rails/engine.rb:471:in `each'
  55. from /app/vendor/bundle/ruby/2.3.0/gems/railties-4.2.6/lib/rails/engine.rb:471:in `block in eager_load!'
  56. from /app/vendor/bundle/ruby/2.3.0/gems/railties-4.2.6/lib/rails/engine.rb:469:in `each'
  57. from /app/vendor/bundle/ruby/2.3.0/gems/railties-4.2.6/lib/rails/engine.rb:469:in `eager_load!'
  58. from /app/vendor/bundle/ruby/2.3.0/gems/railties-4.2.6/lib/rails/engine.rb:346:in `eager_load!'
  59. from /app/vendor/bundle/ruby/2.3.0/gems/railties-4.2.6/lib/rails/application/finisher.rb:56:in `each'
  60. from /app/vendor/bundle/ruby/2.3.0/gems/railties-4.2.6/lib/rails/application/finisher.rb:56:in `block in <module:Finisher>'
  61. from /app/vendor/bundle/ruby/2.3.0/gems/railties-4.2.6/lib/rails/initializable.rb:30:in `instance_exec'
  62. from /app/vendor/bundle/ruby/2.3.0/gems/railties-4.2.6/lib/rails/initializable.rb:30:in `run'
  63. from /app/vendor/bundle/ruby/2.3.0/gems/railties-4.2.6/lib/rails/initializable.rb:55:in `block in run_initializers'
  64. from /app/vendor/ruby-2.3.1/lib/ruby/2.3.0/tsort.rb:228:in `block in tsort_each'
  65. from /app/vendor/ruby-2.3.1/lib/ruby/2.3.0/tsort.rb:350:in `block (2 levels) in each_strongly_connected_component'
  66. from /app/vendor/ruby-2.3.1/lib/ruby/2.3.0/tsort.rb:431:in `each_strongly_connected_component_from'
  67. from /app/vendor/ruby-2.3.1/lib/ruby/2.3.0/tsort.rb:349:in `block in each_strongly_connected_component'
  68. from /app/vendor/ruby-2.3.1/lib/ruby/2.3.0/tsort.rb:347:in `each'
  69. from /app/vendor/ruby-2.3.1/lib/ruby/2.3.0/tsort.rb:347:in `call'
  70. from /app/vendor/ruby-2.3.1/lib/ruby/2.3.0/tsort.rb:347:in `each_strongly_connected_component'
  71. from /app/vendor/ruby-2.3.1/lib/ruby/2.3.0/tsort.rb:226:in `tsort_each'
  72. from /app/vendor/ruby-2.3.1/lib/ruby/2.3.0/tsort.rb:205:in `tsort_each'
  73. from /app/vendor/bundle/ruby/2.3.0/gems/railties-4.2.6/lib/rails/initializable.rb:54:in `run_initializers'
  74. from /app/vendor/bundle/ruby/2.3.0/gems/railties-4.2.6/lib/rails/application.rb:352:in `initialize!'
  75. from /app/config/environment.rb:5:in `<top (required)>'
  76. from /app/vendor/bundle/ruby/2.3.0/gems/railties-4.2.6/lib/rails/application.rb:328:in `require_environment!'
  77. from /app/vendor/bundle/ruby/2.3.0/gems/railties-4.2.6/lib/rails/commands/commands_tasks.rb:142:in `require_application_and_environment!'
  78. from /app/vendor/bundle/ruby/2.3.0/gems/railties-4.2.6/lib/rails/commands/commands_tasks.rb:67:in `console'
  79. from /app/vendor/bundle/ruby/2.3.0/gems/railties-4.2.6/lib/rails/commands/commands_tasks.rb:39:in `run_command!'
  80. from /app/vendor/bundle/ruby/2.3.0/gems/railties-4.2.6/lib/rails/commands.rb:17:in `<top (required)>'
  81. from /app/bin/rails:9:in `require'
  82. from /app/bin/rails:9:in `<main>'
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement