このサイトを参考にsns認証を実装していたのですがグーグル認証をすると
↳ app/models/user.rb:21:in `from_omniauth' (1.0ms) rollback transaction ↳ app/models/user.rb:21:in `from_omniauth' Redirected to http://localhost:3000/users/sign_up Completed 302 Found in 2419ms (ActiveRecord: 3.8ms | Allocations: 4613)
rollbackしてしまいます。モデルを書き換えたりコントローラーを書き換えたりしましたが治りませんでした。繰り返しになりますが何かわかる方は回答、コメントお願いします。
userrb
1def self.from_omniauth(auth) 2 where(provider: auth.provider, uid: auth.uid).first_or_create do |user| 3 user.email = auth.info.email 4 user.password = Devise.friendly_token[0,20] 5 end 6 end
OmniauthCallbacksController
1 2class Man::OmniauthCallbacksController < Devise::OmniauthCallbacksController 3 # callback for facebook 4 def facebook 5 callback_for(:facebook) 6 end 7 8 # callback for twitter 9 def twitter 10 callback_for(:twitter) 11 end 12 13 # callback for google 14 def google_oauth2 15 callback_for(:google) 16 end 17 18 # common callback method 19 def callback_for(provider) 20 @user = User.from_omniauth(request.env["omniauth.auth"]) 21 if @user.persisted? 22 sign_in_and_redirect @user, event: :authentication #this will throw if @user is not activated 23 set_flash_message(:notice, :success, kind: "#{provider}".capitalize) if is_navigational_format? 24 else 25 session["devise.#{provider}_data"] = request.env["omniauth.auth"].except("extra") 26 redirect_to new_man_registration_url 27 end 28 end 29 30 def failure 31 redirect_to root_path 32 end 33 34end
いちおうグーグルアカウントの選択画面は正しく出るのでAppIDなどの移し間違えではないと思います。
deviserb
1 config.omniauth :facebook, ENV['FACEBOOK_KEY'], ENV['FACEBOOK_SECRET'], scope: 'email', info_fields: 'email', callback_url: "#{ENV['HOST']}/users/auth/facebook/callback" 2 config.omniauth :twitter, ENV['TWITTER_API_KEY'], ENV['TWITTER_API_SECRET'], scope: 'email', oauth_callback: "#{ENV['HOST']}/users/auth/twitter/callback" 3 config.omniauth :google_oauth2,ENV['GOOGLE_CLIENT_ID'],ENV['GOOGLE_CLIENT_SECRET'], skip_jwt: true
あなたの回答
tips
プレビュー