エラーの解消方法がわからず、是非皆様のお力を貸して頂けると幸いです。
現状
タイトルの通り、リンクを押下すると画面に『Not found. Authentication passthru.』と表示される
(画面:http://localhost:3000/users/auth/github)
dockerでのエラー内容
***-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)
どうしたいか
現状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
# 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
users/registrations_controller.rb
# 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
models/user.rb
# 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
views/users/sessions/new.html.erb
<%= 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 %>
config/initializers/devise.rb
config.omniauth :github, ENV['CLIENT_ID'], ENV['CLIENT_SECRET'],scope: "user,repo,gist"
routes.rb
# 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
*******_add_omniauth_to_users.rb
class AddOmniauthToUsers < ActiveRecord::Migration[7.0]
def change
add_column :users, :provider, :string
add_column :users, :uid, :string
end
end
Gemfile
gem 'devise'
gem 'dotenv-rails'
gem 'omniauth'
gem 'omniauth-github'
gem "omniauth-rails_csrf_protection"
.env
CLIENT_ID = '********'
CLIENT_SECRET = '*********'