Ruby on rails omniauth-twitter and devise -
Ruby on rails omniauth-twitter and devise -
i seek implement oauth-twitter gem allow users sign-up/sign-in twitter, thing when users redirected after accepting app on twitter, redirect him sign page.
here config:
app/controllers/users/omniauth_callbacks_controller.rb
class users::omniauthcallbackscontroller < devise::omniauthcallbackscontroller def twitter # need implement method below in model (e.g. app/models/user.rb) @user = user.from_omniauth(request.env["omniauth.auth"]) if @user.persisted? sign_in_and_redirect @user, :event => :authentication #this throw if @user not activated set_flash_message(:notice, :success, :kind => "twitter") if is_navigational_format? else session["devise.twitter.data"] = request.env["omniauth.auth"].except("extra") redirect_to new_user_registration_url end end end
then app/models/users.rb
class user < activerecord::base # include default devise modules. others available are: # :confirmable, :lockable, :timeoutable , :omniauthable devise :database_authenticatable, :registerable, :recoverable, :rememberable, :trackable, :validatable devise :omniauthable, :omniauth_providers => [:twitter] def self.from_omniauth(auth) where(auth.slice(:provider, :uid)).first_or_create |user| user.email = auth.info.email user.password = devise.friendly_token[0,20] user.name = auth.info.name # assuming user model has name end end def self.new_with_session(params, session) super.tap |user| if info = session["devise.twitter_data"] && session["devise.twitter_data"]["extra"]["raw_info"] user.email = data["email"] if user.email.blank? end end end end
my gemfile
source 'https://rubygems.org' ruby '2.1.1' gem 'rails', '4.0.0' gem "sass-rails", "~> 4.0.0" gem 'uglifier' gem 'coffee-rails' gem 'jquery-rails' gem 'jbuilder', '~> 1.2' gem 'bootstrap-sass' gem 'devise' gem "font-awesome-rails", "~> 4.0.1.0" gem 'masonry-rails', '~> 0.2.0' gem "zeroclipboard-rails", "~> 0.0.10" gem "will_paginate", '~> 3.0' gem 'intercom-rails', '~> 0.2.24' gem 'magnific-popup-rails' gem "rake" gem 'omniauth-twitter' grouping :development, :test gem 'sqlite3' end grouping :production gem 'pg' gem 'rails_12factor' end grouping :doc # bundle exec rake doc:rails generates api under doc/api. gem 'sdoc', require: false end # utilize activemodel has_secure_password # gem 'bcrypt-ruby', '~> 3.0.0' # utilize unicorn app server # gem 'unicorn' # utilize capistrano deployment # gem 'capistrano', group: :development # utilize debugger # gem 'debugger', group: [:development, :test]
in config/initializers/devise.rb
i added the:
require 'omniauth-twitter' config.omniauth :twitter, "app_id", "app_key", :strategy_class => omniauth::strategies::twitter
but when users sign via link:
<%= link_to "sign in twitter", user_omniauth_authorize_path(:twitter) %>
and take app on twitter redirected sign page, , it's not filling anything, know email adress can't retrieve via twitter, name ? should fill out name?
thanks pointing wrong
inside user model's new_with_session(params, session) method, didn't retrieve info data homecoming twitter, assignment there.
ruby-on-rails twitter oauth devise
Comments
Post a Comment