###前提・実現したいこと
RailsでいろんなSNSとOAuth連携/ログインする方法を参考に複数SNSの認証をやろうとしているのですが、認証後にバリデーションの関係でエラーが出てしまいます。
原因は、SNSの認証後にメールアドレス、パスワードがなく、deviseのバリデーションに引っかかることなのですが、SNSによる認証を使わない時の登録はメールアドレスやパスワードが必要です。
SNSの認証を使うときのみメールアドレスとパスワードの認証をしないようにしたいのですが、方法がわかりません。
###発生している問題・エラーメッセージ
###該当のソースコード
app/controllers/omniauth_callbacks_controller.rb
ruby
1class OmniauthCallbacksController < Devise::OmniauthCallbacksController 2 def facebook; basic_action; end 3 def github; basic_action; end 4 def google; basic_action; end 5 def hatena; basic_action; end 6 def linkedin; basic_action; end 7 def mixi; basic_action; end 8 def hatena; basic_action; end 9 10 private 11 def basic_action 12 @omniauth = request.env['omniauth.auth'] 13 if @omniauth.present? 14 @profile = SocialProfile.where(provider: @omniauth['provider'], uid: @omniauth['uid']).first 15 unless @profile 16 @profile = SocialProfile.where(provider: @omniauth['provider'], uid: @omniauth['uid']).new 17 @profile.user = current_user || User.create!(name: @omniauth['name']) # ここのユーザー作成時にエラーが出る(作成できない) 18 @profile.save! 19 end 20 if current_user 21 raise "user is not identical" if current_user != @profile.user 22 else 23 sign_in(:user, @profile.user) 24 end 25 @profile.set_values(@omniauth) 26 end 27 render :close, layout: false 28 end
###試したこと
Deviseのカスタマイズ。OAuthのときはemail入力を不必要にして、""でも可にするの記事にあるように、emailのインデックスを消し、nullを許可した場合、
app/models/user.rb
で以下のように書くとSNS経由の登録はできるのですが、常にemailやpasswordが不要になるため困っています。
SNSを使った認証時にのみ、バリデーションを不要にしたいのですが...
ruby
1def email_required? 2 false 3end 4 5def password_required? 6 false 7end
また、Deviseのカスタマイズ。OAuthのときはemail入力を不必要にして、""でも可にするにあるように、下のように書くと別のエラーが出たためやめました。
ruby
1def email_required? 2 (authentications.empty? || !email.blank?) && super 3end
###補足情報(言語/FW/ツール等のバージョンなど)
あまり関係ないかもしれませんが、バージョン情報をのせておきます。
- Rails 5.0.0.rc1
- Devise 4.1.1
回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2016/06/10 06:58