3
3

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

Twitter登録する為に進めていて、躓いた事まとめ

Posted at

#まずドキュメントを見ながら進める事が大前提
OmniAuth: Overview
ユーザーが自分のメールアドレス以外の何かでログインすることを許可するやり方

#uidをDBに保存する為にストロングパラメーターを通さなければならないが、どこに書くのか問題

下の様にuidを入れるとエラーが出た。

app/controllers/application_controller.rb
    def configure_permitted_parameters
      devise_parameter_sanitizer.permit(:sign_up, keys: [:name, :uid])
      devise_parameter_sanitizer.permit(:account_update, keys: [:name])

NameError
undefined local variable or method `uid' for #<Users::OmniauthCallbacksController:0x0000000467c510
Extracted source (around line #13):

そこで下の様にuidを入れるとエラー解決

app/controllers/users/omniauth_callbacks_controller.rb
  def sign_up_params
    params.require(:user).permit(:uid, :provider, :nickname, :image_url)
  end

#ActionDispatch::Cookies::CookieOverflowというエラー
要は、deviseを使っている事情でemailカラムにデータを入れずに登録する事はできない。それが原因で出たエラー。

app/models/user.rb
def self.from_omniauth(auth)
  where(provider: auth.provider, uid: auth.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
    user.image = auth.info.image # assuming the user model has an image
    # If you are using confirmable and the provider(s) you use validate emails, 
    # uncomment the line below to skip the confirmation emails.
    # user.skip_confirmation!
  end
end

上のuser.email = auth.info.emailの部分に問題がある。twitter情報からメールアドレスは抜けないから、空になる。
そこで、解決策としてダミーのメールアドレスを作って、カラムに入れるようにする。下記の通り。

app/model/user.rb
def self.from_omniauth(auth)
    where(provider: auth.provider, uid: auth.uid).first_or_create do |user|
      user.email = User.dummy_email(auth)
      user.password = Devise.friendly_token[4, 30]
      user.uid = auth['uid']
      user.provider = auth['provider']
      user.nickname = auth['info']['name']   # assuming the user model has a name
      user.image_url = auth['info']['image'] # assuming the user model has an image
      # If you are using confirmable and the provider(s) you use validate emails, 
      # uncomment the line below to skip the confirmation emails.
      # user.skip_confirmation!
    end
  end

  private
  #twitterでサインアップする時に、本来サインアップに必要なメールアドレスカラムをダミーで埋める
  def self.dummy_email(auth)
    "#{auth.uid}-#{auth.provider}@example.com"
  end

これでエラー解決して、twitter認証登録完了。

#ドキュメント通りにログアウト作ってエラー
ドキュメント通りに下記を追加する。

config/routes.rb
devise_scope :user do
  delete 'sign_out', :to => 'devise/sessions#destroy', :as => :destroy_user_session
end

エラー内容は

[同じ内容のエラーに関する質問1](# config/routes.rb
devise_scope :user do
delete 'sign_out', :to => 'devise/sessions#destroy', :as => :destroy_user_session
end)
同じ内容のエラーに関する質問2

下記のようにしてもエラー

config/routes.rb
root                                    'static_pages#index'
  get '/users/sign_up',               to: 'users/registrations#new'
  get '/users/show',                  to: 'users#show'
  get '/users/auth/twitter/callback', to: 'omniauth_callbacks#twitter'
  DELETE '/users/sign_out',           to: 'devise/sessions#destroy'

エラー内容は
[dry-types] Int type was renamed to Integer
[dry-types] Form types were renamed to Params
rails aborted!
NoMethodError: undefined method `DELETE' for #<ActionDispatch::Routing::Mapper:0x00000003d3d918

deleteをgetに設定変更するを参考に

config/initializers/devise.rb
config.sign_out_via = :get

のようにdeleteからgetに変更する。そして

config/routes.rb
root                                    'static_pages#index'
  get '/users/sign_up',               to: 'users/registrations#new'
  get '/users/show',                  to: 'users#show'
  get '/users/auth/twitter/callback', to: 'omniauth_callbacks#twitter'
  get '/users/sign_out',              to: 'devise/sessions#destroy'

と書いて解決。

3
3
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
3
3

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?