rails初学者です。
omniauthとdeviseを使って、Githubアカウントでのログイン機能を実装している最中に、「Not found. Authentication passthru.」というエラーが表示されましたので、解決したいです。
以下の記事を参考に実装作業を進めていたのですが、自力ではこれ以上の解決ができそうになくお力を貸していただけると幸いです。
https://qiita.com/fa19940118/items/e081e99d6a199568810f
仮説
- omniauth_callbacks_controllerにpassthruメソッドの定義がないことが原因?
- ビューにおけるサインアップようのパスが誤っていることが原因?
検証内容
- omniauth_callbacks_controller内のメソッド名をgithub→passthruに変更
- ビューないのパスを、user_github_omniauth_authorize_path→user_github_omniauth_callbackに変更
環境
Rails 5.2.4.1
ruby 2.5.1
gem 'omniauth'
gem 'omniauth-github'
gem 'dotenv-rails'
gem "omniauth-rails_csrf_protection"
以下関連のコードです
app/views/oauth_test/index.html.haml
haml
1%p.oauth_index 2 = notice 3 = alert 4- if user_signed_in? 5 = link_to "ログアウト", destroy_user_session_path, method: :delete 6- else 7 = link_to 'githubでサインアップ', user_github_omniauth_authorize_path
app/controllers/users/omniauth_callbacks_controller.rb
class Users::OmniauthCallbacksController < Devise::OmniauthCallbacksController def github @user = User.find_for_github_oauth(request.env["omniauth.auth"], current_user) if @user.persisted? sign_in_and_redirect @user, event: :authentication set_flash_message(:notice, :success, :kind => "Github") if is_navigational_format? else session["devise.github_data"] = request.env["omniauth.auth"] redirect_to new_user_registration_url end end end
config/routes.rb
Rails.application.routes.draw do get 'oauth_test/index' devise_for :users, controllers: { registrations: "users/registrations", omniauth_callbacks: "users/omniauth_callbacks" } root to: 'posts#index' resources :users, only: [:index, :show] resources :posts, only: [:index, :show, :new, :create, :destroy] do resources :likes, only: [:create, :destroy] end end
app/models/user.rb
class User < ApplicationRecord # Include default devise modules. Others available are: # :confirmable, :lockable, :timeoutable, :trackable and :omniauthable devise :database_authenticatable, :registerable, :recoverable, :rememberable, :validatable, :omniauthable, omniauth_providers: %i(github) has_many :posts, dependent: :destroy has_many :likes, dependent: :destroy def self.create_unique_string SecureRandom.uuid end def self.find_for_github_oauth(auth, signed_in_resource=nil) user = User.find_by(provider: auth.provider, uid: auth.uid) unless user user = User.new(provider: auth.provider, uid: auth.uid, name: auth.info.name, email: User.dummy_email(auth), password: Devise.friendly_token[0, 20] ) end user.save user end def self.dummy_email(auth) "#{auth.uid}-#{auth.provider}@example.com" end end
rails routes
user_github_omniauth_authorize GET|POST /users/auth/github(.:format) users/omniauth_callbacks#passthru user_github_omniauth_callback GET|POST /users/auth/github/callback(.:format) users/omniauth_callbacks#github
回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。