95606


エラーの解消方法がわからず、是非皆様のお力を貸して頂けると幸いです。

### 現状

タイトルの通り、リンクを押下すると画面に『Not found. Authentication passthru.』と表示される  
（画面：http://localhost:3000/users/auth/github）

dockerでのエラー内容

[code]

    ***-web-1  | Started GET "/users/auth/github" for 172.19.0.1 at 2023-07-13 14:35:23 +0000
    ***-web-1  | Processing by Users::OmniauthCallbacksController#passthru as HTML
    ***-web-1  |   Rendering text template
    ***-web-1  |   Rendered text template (Duration: 0.0ms | Allocations: 4)
    ***-web-1  | Completed 404 Not Found in 2ms (Views: 1.3ms | ActiveRecord: 0.0ms | Allocations: 278)
    
[/code]

### どうしたいか

現状login画面で『GitHubでログイン/登録する』リンクを押下後、githubログインを行いたい

以下を参照して色々試してみたが上手くいかず。  
<https://github.com/heartcombo/devise/wiki/OmniAuth%3A-Overview>  
<https://www.takayasugiyama.com/entry/2019/11/09/092017>  
<https://fuga-ch85.hatenablog.com/entry/2021/04/10/075536>

### 補足

GitHub側での`Client ID`, `Client secrets`は設定かつ間違ってないかを確認済み。  
以下の設定  
Homepage URL : `http://localhost:3000/`  
Authorization callback URL :
`http://localhost:3000/users/auth/github/callback`

### 環境

ruby 3.0.2  
rails 7.0.4  
devise 4.9.2  
oauth2 2.0.9  
omniauth 2.1.1  
omniauth-oauth2 1.8.0  
omniauth-github 2.0.1  
omniauth-rails_csrf_protection 1.0.1

### ファイル

users/omniauth_callbacks_controller.rb

[code]

    # frozen_string_literal: true
    
    module Users
      class OmniauthCallbacksController < Devise::OmniauthCallbacksController
        skip_before_action :verify_authenticity_token, only: :github
    
        def github
          @user = User.from_omniauth(request.env['omniauth.auth'])
    
          if @user.present?
            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"].except(:extra)
            redirect_to new_user_registration_url
          end
        end
    
        def failure
          redirect_to root_path
        end
      end
    end
    
[/code]

users/registrations_controller.rb

[code]

    # frozen_string_literal: true
    
    module Users
      class RegistrationsController < Devise::RegistrationsController
        def build_resource(hash = {})
          hash[:uid] = User.create_unique_string
          super
        end
    
        protected
    
        def update_resource(resource, params)
          return super if params['password'].present?
    
          resource.update_without_password(params.except('current_password'))
        end
      end
    end
    
[/code]

models/user.rb

[code]

    # frozen_string_literal: true
    
    class User < ApplicationRecord
      # Include default devise modules. Others available are:
      # :confirmable, :lockable, :timeoutable, :trackable and :omniauthable
      devise :database_authenticatable, :registerable,
             :recoverable, :rememberable, :validatable, :confirmable, :omniauthable, omniauth_providers: %i[github]
    
      VALID_EMAIL_REGEX = /\A[\w+\-.]+@[a-z\d\-.]+\.[a-z]+\z/i
    
      validates :email, presence: true, uniqueness: true, format: { with: VALID_EMAIL_REGEX }
      validates :phone, presence: true, uniqueness: true
      validates :birthday, presence: true
      validates :uid, presence: true, uniqueness: { scope: :provider }
    
      def self.from_omniauth(auth)
        find_or_create_by(provider: auth.provider, uid: auth.uid) do |user|
          user.email = auth.info.email
          user.password = Devise.friendly_token[0,20]
        end
      end
    end
    
[/code]

views/users/sessions/new.html.erb

[code]

    <%= form_for(resource, as: resource_name, html: {'data-turbo' => "false"}, url: session_path(resource_name) ) do |f| %>
    
      <%= link_to "GitHubでログイン/登録する", user_github_omniauth_authorize_path, method: :post %>
    <% end %>
    
[/code]

config/initializers/devise.rb

[code]

     config.omniauth :github, ENV['CLIENT_ID'], ENV['CLIENT_SECRET'],scope: "user,repo,gist"
    
[/code]

routes.rb

[code]

    # frozen_string_literal: true
    
    Rails.application.routes.draw do
      mount LetterOpenerWeb::Engine, at: '/letter_opener' if Rails.env.development?
      devise_for :users, controllers: { omniauth_callbacks: 'users/omniauth_callbacks' }
      get '/mypage', to: 'mypage#show'
      resources :tasks
      # Define your application routes per the DSL in https://guides.rubyonrails.org/routing.html
    
      # Defines the root path route ("/")
      root 'mypage#show'
    end
    
    
[/code]

*******_add_omniauth_to_users.rb

[code]

    class AddOmniauthToUsers < ActiveRecord::Migration[7.0]
      def change
        add_column :users, :provider, :string
        add_column :users, :uid, :string
      end
    end
    
[/code]

Gemfile

[code]

    gem 'devise'
    
    gem 'dotenv-rails'
    
    gem 'omniauth'
    gem 'omniauth-github'
    gem "omniauth-rails_csrf_protection"
    
[/code]

.env

[code]

    CLIENT_ID = '********'
    CLIENT_SECRET = '*********'
    
[/code]


CC BY-SA 4.0
2023-07-13T15:38:19.607
95598
2023-07-14T05:28:47.347
56641
question
ruby-on-rails ruby oauth devise
devise + OAuth2で画面に『Not found. Authentication passthru.』と表示される


下記のサイトを参考にしOmniAuthがPOSTまたはGETメソッドのリクエストを受け入れるように設定をしたら無事に遷移しました。

<https://qiita.com/Sugizou0215/items/9fb838793dc8502b97d6>


CC BY-SA 4.0
2023-07-14T05:28:47.347
95606
2023-07-14T05:28:47.347
56641
95598
answer