#まずドキュメントを見ながら進める事が大前提
OmniAuth: Overview
ユーザーが自分のメールアドレス以外の何かでログインすることを許可するやり方
#uidをDBに保存する為にストロングパラメーターを通さなければならないが、どこに書くのか問題
下の様にuidを入れるとエラーが出た。
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を入れるとエラー解決
def sign_up_params
params.require(:user).permit(:uid, :provider, :nickname, :image_url)
end
#ActionDispatch::Cookies::CookieOverflowというエラー
要は、deviseを使っている事情でemailカラムにデータを入れずに登録する事はできない。それが原因で出たエラー。
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情報からメールアドレスは抜けないから、空になる。
そこで、解決策としてダミーのメールアドレスを作って、カラムに入れるようにする。下記の通り。
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認証登録完了。
#ドキュメント通りにログアウト作ってエラー
ドキュメント通りに下記を追加する。
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
下記のようにしてもエラー
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
config.sign_out_via = :get
のようにdeleteからgetに変更する。そして
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'
と書いて解決。