先にdeviseでログイン、ログアウト機能を実装後、追加でoauth-facebookをとりいれようとしたところ、止まってしまいました。
どなたか、ご教授いただけると幸いです。
前提・実現したいこと
omniauthを使ってfacebookログインを実装したいです。
発生している問題
facebookサインインのリンクをクリックすると、facebookのログインページへ遷移し、アカウント情報を入力できます。しかし、アカウント情報を入力して、いざ目的のページへ遷移するかと思いきや、
The page you were looking for doesn't exist. You may have mistyped the address or the page may have moved.
とブラウザに表示されてしまいます。
また、herokuのログを確認すると、
abstractController::ActionNotFound (The action 'facebook' could not be found for Devise::OmniauthCallbacksController):
このようなエラーが出ているのですが、facebookアクションはgithubのomniauth-overviewに規定された場所に書いているはずのなのに、なぜ action not found なのかわからず、八方ふさがりです。。。
実装にあたり、行ったこと
githubのomniauth:-overview(https://github.com/plataformatec/devise/wiki/omniauth:-overview)に従い、下記を実行しました。
gem インストール
devise.rbにconfig設定
user.rbにomniauthableの設定を足す
routes.rbにルート設定
omniauth_callbacks_controller.rbの作成
user.rbに def self.from_omniauth(auth)メソッドを追加しました。
また、facebook for developperのほうでも、ドメイン/users/auth/facebook/callback と、設定して有効なOAuthリダイレクトURIの設定は行っています。
ちなみにdeviseを使ったログイン、ログアウト自体は実装済みで、問題なく動いています。
環境
rails 5.2.2
ruby 2.5.5
gem 'devise'
gem 'omniauth'
gem 'omniauth-facebook'
cloud9
heroku
試したこと
- bundle update
-
devise.rb に token_params: { parse: :json }を追加
-
dbをリセットしてみたり
その他のエラーメッセージ
heroku logs -t で出てきたエラーメッセージっぽいものを下記に示します。
ログをそのまま貼り付けると、あまりよろしくないなんていう書き込みを見かけたので、そのままベタ張りしていません。
如何せん初心者なので、本当に下記がエラーメッセージなのかはわからず、申し訳ないです。
FATAL -- : [d2a59dc7-2032-4235-9eed-50f1883ca039] vendor/bundle/ruby/2.5.0/gems/actionpack-5.2.3/lib/abstract_controller/base.rb:129:in `process'
actionnotfoundエラーと上記のエラーが出てしばらくログがつずいた後にstatus=404になります。
該当のコード
routes.rb
Rails.application.routes.draw do devise_for :users, controllers: { omniauth_callbacks: 'users/omniauth_callbacks' } # For details on the DSL available within this file, see http://guides.rubyonrails.org/routing.html root 'posts#index' get '/users/:id', to: 'users#show', as: 'user' resources :posts, only: %i(index new create show destroy) do resources :photos, only: %i(create) resources :likes, only: %i(create destroy) resources :comments, only: %i(create destroy) end end
app/controllers/users/omniauth_callbacks_controller.rb
class Users::OmniauthCallbacksController < Devise::OmniauthCallbacksController def facebook # You need to implement the method below in your model (e.g. app/models/user.rb) @user = User.from_omniauth(request.env["omniauth.auth"]) if @user.persisted? sign_in_and_redirect @user, event: :authentication #this will throw if @user is not activated set_flash_message(:notice, :success, kind: "Facebook") if is_navigational_format? else session["devise.facebook_data"] = request.env["omniauth.auth"] redirect_to new_user_registration_url end end def failure redirect_to root_path end end
app/models/user.rb
class User < ApplicationRecord has_many :posts, dependent: :destroy has_many :likes has_many :comments # Include default devise modules. Others available are: # :confirmable, :lockable, :timeoutable, :trackable and :omniauthable devise :database_authenticatable, :registerable, :recoverable, :rememberable, :trackable, :validatable, :omniauthable validates :name, presence: true, length: { maximum: 50 } 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 def self.new_with_session(params, session) super.tap do |user| if data = session["devise.facebook_data"] && session["devise.facebook_data"]["extra"]["raw_info"] user.email = data["email"] if user.email.blank? end end end end
config/initializers/devise.rb 一部抜粋
config.omniauth :facebook, ENV['FACEBOOK_APP_ID'], ENV['FACEBOOK_APP_SECRET'], token_params: { parse: :json }
*appIDとシークレットはdotenvに保管して、herokuに直接環境変数を入力しています
見にくくて恐縮ですが、どなたか何かアドバイスをいただけるとありがたいです。
回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。