I try to implement the oauth-twitter gem to allow my users to sign-up/sign-in with twitter, the thing is that when the users is redirected after accepting my app on twitter, it redirect him to the sign up page.
Here is my config:
app/controllers/users/omniauth_callbacks_controller.rb
class Users::OmniauthCallbacksController < Devise::OmniauthCallbacksController
def twitter
# You need to implement the method below in your 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 will throw if @user is 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 my app/models/users.rb
class User < ActiveRecord::Base
# Include default devise modules. Others available are:
# :confirmable, :lockable, :timeoutable and :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 do |user|
user.email = auth.info.email
user.password = Devise.friendly_token[0,20]
user.name = auth.info.name # assuming the user model has a name
end
end
def self.new_with_session(params, session)
super.tap do |user|
if data = 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'
group :development, :test do
gem 'sqlite3'
end
group :production do
gem 'pg'
gem 'rails_12factor'
end
group :doc do
# bundle exec rake doc:rails generates the API under doc/api.
gem 'sdoc', require: false
end
# Use ActiveModel has_secure_password
# gem 'bcrypt-ruby', '~> 3.0.0'
# Use unicorn as the app server
# gem 'unicorn'
# Use Capistrano for deployment
# gem 'capistrano', group: :development
# Use 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 my users sign up via the link:
<%= link_to "Sign in with twitter", user_omniauth_authorize_path(:twitter) %>
and accept my app on twitter they are redirected to the sign up page, and it's not filling anything, I know the email adress can't be retrieve via twitter, but what about the name ? it should fill out the name?
Thanks for pointing what would be wrong
Note that the email can now be retrieved from Twitter. For that, you have to go in your Twitter dev account ( https://developer.twitter.com/en/apps ), select your app and go in the "Permissions" settings. There, you can click on "Edit" and check the box to "Request email address from users".
After that, your omniauth with Twitter should work correctly. That being said, you might want to inform users about the errors they encounter. For that reason, I would add an alert notice inside your twitter
method in case the authentication fails:
def twitter
# You need to implement the method below in your 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 will throw if @user is 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, alert: @user.errors.full_messages.join("\n")
end
end
在您的 User 模型的 new_with_session(params, session) 方法中,您没有从 twitter 返回的数据中检索数据,在那里做一些分配。
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.