railsでdeviseを使ってfacebookとGoogle認証をしようとしていますが次のようなエラーが出てしまいます。
##試したこと
①gem"devise"とOAuth関連のgem
Gemfile
1gem 'devise' 2gem 'omniauth' 3gem 'omniauth-google-oauth2' 4gem 'omniauth-facebook' 5gem 'dotenv-rails'
②FacebookとGoogleのKeyとSecretを取得
facebookに有効なOAuthリダイレクトURI
https://localhost:3000/users/auth/facebook/callback
Googleに有効なOAuthリダイレクトURI
http://localhost:3000/users/auth/google_oauth2/callback
③IDとSECRETの設定
*のところは、各SNSのIDとSECRETをいれてあります。
env
1FACEBOOK_CLIENT_ID='************' 2FACEBOOK_CLIENT_SECRET='******************' 3GOOGLE_CLIENT_ID='*************************************************' 4GOOGLE_CLIENT_SECRET='*****************'
devise
1config.omniauth :facebook, ENV['FACEBOOK_CLIENT_ID'], ENV['FACEBOOK_CLIENT_SECRET'] 2config.omniauth :google_oauth2, ENV['GOOGLE_CLIENT_ID'], ENV['GOOGLE_CLIENT_SECRET'], skip_jwt: true
④userモデルの作成
Terminal
1rails g devise User
user
1has_many :sns_credentials, dependent: :destroy 2 3devise :database_authenticatable, :registerable, 4 :recoverable, :rememberable, :validatable, 5 :omniauthable, omniauth_providers: %i[facebook google_oauth2]
⑤ SNS_Credentialモデルの作成
Terminal
1rails g model sns_credential
create_sns_credentials
def change create_table :sns_credentials do |t| t.string :provider t.string :uid t.references :user, foreign_key: true t.timestamps end end
sns_credential.rb
class SnsCredential < ApplicationRecord belongs_to :user end
⑥コールバック関数
routes.rb
devise_for :users, controllers: { sessions: 'users/sessions', registrations: "users/registrations", omniauth_callbacks: 'users/omniauth_callbacks' }
omniauth_callbacks_controller.rb
def facebook callback_for(:facebook) end def google_oauth2 callback_for(:google) end def callback_for(provider) @omniauth = request.env['omniauth.auth'] info = User.find_oauth(@omniauth) @user = info[:user] if @user.persisted? sign_in_and_redirect @user, event: :authentication set_flash_message(:notice, :success, kind: "#{provider}".capitalize) if is_navigational_format? else @sns = info[:sns] render template: "devise/registrations/new" end end def failure redirect_to root_path and return end
user.rb
def self.without_sns_data(auth) user = User.where(email: auth.info.email).first if user.present? sns = SnsCredential.create( uid: auth.uid, provider: auth.provider, user_id: user.id ) else user = User.new( nickname: auth.info.name, email: auth.info.email, ) sns = SnsCredential.new( uid: auth.uid, provider: auth.provider ) end return { user: user ,sns: sns} end def self.with_sns_data(auth, snscredential) user = User.where(id: snscredential.user_id).first unless user.present? user = User.new( nickname: auth.info.name, email: auth.info.email, ) end return {user: user} end def self.find_oauth(auth) uid = auth.uid provider = auth.provider snscredential = SnsCredential.where(uid: uid, provider: provider).first if snscredential.present? user = with_sns_data(auth, snscredential)[:user] sns = snscredential else user = without_sns_data(auth)[:user] sns = without_sns_data(auth)[:sns] end return { user: user ,sns: sns} end
⑦view
session/new.html.erb
<%= link_to user_registration_path(:facebook) %>
_link.html.erb
<%- if devise_mapping.omniauthable? %> <%- resource_class.omniauth_providers.each do |provider| %> <%= link_to "Sign in with #{OmniAuth::Utils.camelize(provider)}", omniauth_authorize_path(resource_name, provider) %><br /> <% end %> <% end %>
以上のコードは、リンク内容を参考にしています。
しかし、NoMethodError in Devise::Sessions#new
となってしまいます。
様々なサイトを見て試しても、解決できません。
ご教授のほどよろしくお願いいたします。
追記
回答1件
あなたの回答
tips
プレビュー